use of com.alibaba.fastjson.parser.deserializer.ExtraProcessor 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.ExtraProcessor in project uavstack by uavorg.
the class JSON method parseObject.
@SuppressWarnings("unchecked")
public static final <T> T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) {
if (input == null) {
return null;
}
for (Feature featrue : features) {
featureValues = Feature.config(featureValues, featrue, true);
}
DefaultJSONParser parser = new DefaultJSONParser(input, config, featureValues);
if (processor instanceof ExtraTypeProvider) {
parser.getExtraTypeProviders().add((ExtraTypeProvider) processor);
}
if (processor instanceof ExtraProcessor) {
parser.getExtraProcessors().add((ExtraProcessor) processor);
}
T value = (T) parser.parseObject(clazz);
parser.handleResovleTask(value);
parser.close();
return (T) value;
}
use of com.alibaba.fastjson.parser.deserializer.ExtraProcessor in project fastjson by alibaba.
the class JSONTest3 method test_json.
public void test_json() throws Exception {
ExtraProcessor extraProcessor = new ExtraProcessor() {
public void processExtra(Object object, String key, Object value) {
Model model = (Model) object;
model.attributes.put(key, value);
}
};
Model model = JSON.parseObject("{\"id\":1001}", (Type) Model.class, extraProcessor);
Assert.assertEquals(1, model.attributes.size());
Assert.assertEquals(1001, model.attributes.get("id"));
}
use of com.alibaba.fastjson.parser.deserializer.ExtraProcessor in project fastjson by alibaba.
the class RedundantTest method test_extra.
public void test_extra() throws Exception {
ExtraProcessor processor = new ExtraProcessor() {
public void processExtra(Object object, String key, Object value) {
VO vo = (VO) object;
vo.getAttributes().put(key, value);
}
};
VO vo = JSON.parseObject("{\"id\":123,\"name\":\"abc\"}", VO.class, processor);
Assert.assertEquals(123, vo.getId());
Assert.assertEquals("abc", vo.getAttributes().get("name"));
}
use of com.alibaba.fastjson.parser.deserializer.ExtraProcessor 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"));
}
Aggregations