use of com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider in project fastjson by alibaba.
the class JSON method parseObject.
@SuppressWarnings("unchecked")
public static <T> T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) {
if (input == null) {
return null;
}
if (features != null) {
for (Feature feature : features) {
featureValues |= feature.mask;
}
}
DefaultJSONParser parser = new DefaultJSONParser(input, config, featureValues);
if (processor != null) {
if (processor instanceof ExtraTypeProvider) {
parser.getExtraTypeProviders().add((ExtraTypeProvider) processor);
}
if (processor instanceof ExtraProcessor) {
parser.getExtraProcessors().add((ExtraProcessor) processor);
}
if (processor instanceof FieldTypeResolver) {
parser.setFieldTypeResolver((FieldTypeResolver) processor);
}
}
T value = (T) parser.parseObject(clazz, null);
parser.handleResovleTask(value);
parser.close();
return (T) value;
}
use of com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider in project fastjson by alibaba.
the class RedundantTest method test_extraWithType.
public void test_extraWithType() throws Exception {
class MyExtraProcessor implements ExtraProcessor, ExtraTypeProvider {
public void processExtra(Object object, String key, Object value) {
VO vo = (VO) object;
vo.getAttributes().put(key, value);
}
public Type getExtraType(Object object, String key) {
if ("value".equals(key)) {
return int.class;
}
return null;
}
}
;
ExtraProcessor processor = new MyExtraProcessor();
VO vo = JSON.parseObject("{\"id\":123,\"value\":\"123456\"}", VO.class, processor);
Assert.assertEquals(123, vo.getId());
Assert.assertEquals(123456, vo.getAttributes().get("value"));
}
use of com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider in project fastjson by alibaba.
the class DefaultJSONParser method parseExtra.
public void parseExtra(Object object, String key) {
// xxx
final JSONLexer lexer = this.lexer;
lexer.nextTokenWithColon();
Type type = null;
if (extraTypeProviders != null) {
for (ExtraTypeProvider extraProvider : extraTypeProviders) {
type = extraProvider.getExtraType(object, key);
}
}
Object value = //
type == null ? // skip
parse() : parseObject(type);
if (object instanceof ExtraProcessable) {
ExtraProcessable extraProcessable = ((ExtraProcessable) object);
extraProcessable.processExtra(key, value);
return;
}
if (extraProcessors != null) {
for (ExtraProcessor process : extraProcessors) {
process.processExtra(object, key, value);
}
}
if (resolveStatus == NeedToResolve) {
resolveStatus = NONE;
}
}
Aggregations