Parser.BodyReference, Parser.Builder, Parser.Callback<T>, Parser.Context, Parser.ParamReference<T>| Constructor and Description |
|---|
BeanParser(boolean allowNulls) |
| Modifier and Type | Method and Description |
|---|---|
Object |
parse(com.google.inject.TypeLiteral<?> type,
Parser.Context ctx)
Parse one or more values to the required type.
|
String |
toString() |
public Object parse(com.google.inject.TypeLiteral<?> type, Parser.Context ctx) throws Throwable
Parser
Parse one or more values to the required type. If the parser doesn't support the required type
a call to Parser.Context.next(TypeLiteral, Object) must be done.
Parser converter = (type, ctx) -> {
if (type.getRawType() == MyType.class) {
// convert to MyType
return ctx.param(values -> new MyType(values.get(0)));
}
// no luck! move next
return next.next();
}
It's also possible to create generic/parameterized types too:
public class MyContainerType<T> {}
ParamConverter converter = (type, ctx) -> {
if (type.getRawType() == MyContainerType.class) {
// Creates a new type from current generic type
TypeLiterale<?> paramType = TypeLiteral
.get(((ParameterizedType) toType.getType()).getActualTypeArguments()[0]);
// Ask param converter to resolve the new/next type.
Object result = next.next(paramType);
return new MyType(result);
}
// no luck! move next
return ctx.next();
}
Copyright © 2017. All rights reserved.