Search in sources :

Example 26 with ObjectDeserializer

use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project fastjson by alibaba.

the class JSONFieldTest_0 method test_0.

public void test_0() throws Exception {
    VO vo = new VO();
    vo.setF0(100);
    vo.setF1(101);
    vo.setF2(102);
    String text = JSON.toJSONString(vo);
    System.out.println(text);
    Assert.assertEquals("{\"f2\":102,\"f1\":101,\"f0\":100}", text);
    VO vo_decoded = JSON.parseObject(text, VO.class);
    Assert.assertEquals(vo.f0, vo_decoded.f0);
    Assert.assertEquals(vo.f1, vo_decoded.f1);
    Assert.assertEquals(vo.f2, vo_decoded.f2);
    JavaBeanDeserializer javaBeanDeser = null;
    ObjectDeserializer deser = ParserConfig.getGlobalInstance().getDeserializer(VO.class);
    javaBeanDeser = (JavaBeanDeserializer) deser;
    Field field = JavaBeanDeserializer.class.getDeclaredField("sortedFieldDeserializers");
    field.setAccessible(true);
    FieldDeserializer[] fieldDeserList = (FieldDeserializer[]) field.get(javaBeanDeser);
    Assert.assertEquals(3, fieldDeserList.length);
    Assert.assertEquals("f2", fieldDeserList[0].fieldInfo.name);
    Assert.assertEquals("f1", fieldDeserList[1].fieldInfo.name);
    Assert.assertEquals("f0", fieldDeserList[2].fieldInfo.name);
}
Also used : FieldDeserializer(com.alibaba.fastjson.parser.deserializer.FieldDeserializer) JSONField(com.alibaba.fastjson.annotation.JSONField) Field(java.lang.reflect.Field) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer) JavaBeanDeserializer(com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer)

Example 27 with ObjectDeserializer

use of com.alibaba.fastjson.parser.deserializer.ObjectDeserializer in project fastjson by alibaba.

the class Issue1584 method test_for_issue.

public void test_for_issue() throws Exception {
    ParserConfig config = new ParserConfig();
    String json = "{\"k\":1,\"v\":\"A\"}";
    {
        Map.Entry entry = JSON.parseObject(json, Map.Entry.class, config);
        Object key = entry.getKey();
        Object value = entry.getValue();
        assertTrue(key.equals("v") || key.equals("k"));
        if (key.equals("v")) {
            assertEquals("A", value);
        } else {
            assertEquals(1, value);
        }
    }
    config.putDeserializer(Map.Entry.class, new ObjectDeserializer() {

        public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
            JSONObject object = parser.parseObject();
            Object k = object.get("k");
            Object v = object.get("v");
            return (T) Collections.singletonMap(k, v).entrySet().iterator().next();
        }

        public int getFastMatchToken() {
            return 0;
        }
    });
    Map.Entry entry = JSON.parseObject(json, Map.Entry.class, config);
    assertEquals(1, entry.getKey());
    assertEquals("A", entry.getValue());
}
Also used : Type(java.lang.reflect.Type) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser) Map(java.util.Map) ParserConfig(com.alibaba.fastjson.parser.ParserConfig) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)

Aggregations

ObjectDeserializer (com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)27 JSONObject (com.alibaba.fastjson.JSONObject)12 JSONException (com.alibaba.fastjson.JSONException)10 JavaBeanDeserializer (com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer)10 Type (java.lang.reflect.Type)9 FieldDeserializer (com.alibaba.fastjson.parser.deserializer.FieldDeserializer)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 ConcurrentMap (java.util.concurrent.ConcurrentMap)7 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)5 ParameterizedType (java.lang.reflect.ParameterizedType)4 WildcardType (java.lang.reflect.WildcardType)4 BigInteger (java.math.BigInteger)4 AccessControlException (java.security.AccessControlException)4 ArrayList (java.util.ArrayList)4 IdentityHashMap (java.util.IdentityHashMap)4 ParserConfig (com.alibaba.fastjson.parser.ParserConfig)3 ParseException (java.text.ParseException)3 List (java.util.List)3