public interface ObjectDeserializer
Interface representing a custom deserializer for Json. You should write a custom
deserializer, if you are not happy with the default deserialization done by Gson. You will
also need to register this deserializer through
ParserConfig.putDeserializer(Type, ObjectDeserializer)
.
public static enum OrderActionEnum { FAIL(1), SUCC(0); private int code; OrderActionEnum(int code){ this.code = code; } } public static class OrderActionEnumDeser implements ObjectDeserializer { public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { Integer intValue = parser.parseObject(int.class); if (intValue == 1) { return (T) OrderActionEnum.FAIL; } else if (intValue == 0) { return (T) OrderActionEnum.SUCC; } throw new IllegalStateException(); } public int getFastMatchToken() { return JSONToken.LITERAL_INT; } }
You will also need to register OrderActionEnumDeser
to ParserConfig:
ParserConfig.getGlobalInstance().putDeserializer(OrderActionEnum.class, new OrderActionEnumDeser());
Modifier and Type | Method and Description |
---|---|
<T> T |
deserialze(DefaultJSONParser parser,
Type type,
Object fieldName)
fastjson invokes this call-back method during deserialization when it encounters a field of the
specified type.
|
int |
getFastMatchToken() |
<T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName)
In the implementation of this call-back method, you should consider invoking
JSON.parseObject(String, Type, Feature[])
method to create objects
for any non-trivial field of the returned object.
parser
- context DefaultJSONParser being deserializedtype
- The type of the Object to deserialize tofieldName
- parent object field nameT
int getFastMatchToken()
Copyright © 2012–2022 Alibaba Group. All rights reserved.