Search in sources :

Example 1 with JavaBeanDeserializer

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

the class DefaultJSONParser method parseObject.

public void parseObject(Object object) {
    Class<?> clazz = object.getClass();
    JavaBeanDeserializer beanDeser = null;
    ObjectDeserializer deserizer = config.getDeserializer(clazz);
    if (deserizer instanceof JavaBeanDeserializer) {
        beanDeser = (JavaBeanDeserializer) deserizer;
    }
    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error, expect {, actual " + lexer.tokenName());
    }
    for (; ; ) {
        // lexer.scanSymbol
        String key = lexer.scanSymbol(symbolTable);
        if (key == null) {
            if (lexer.token() == JSONToken.RBRACE) {
                lexer.nextToken(JSONToken.COMMA);
                break;
            }
            if (lexer.token() == JSONToken.COMMA) {
                if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
                    continue;
                }
            }
        }
        FieldDeserializer fieldDeser = null;
        if (beanDeser != null) {
            fieldDeser = beanDeser.getFieldDeserializer(key);
        }
        if (fieldDeser == null) {
            if (!lexer.isEnabled(Feature.IgnoreNotMatch)) {
                throw new JSONException("setter not found, class " + clazz.getName() + ", property " + key);
            }
            lexer.nextTokenWithColon();
            // skip
            parse();
            if (lexer.token() == JSONToken.RBRACE) {
                lexer.nextToken();
                return;
            }
            continue;
        } else {
            Class<?> fieldClass = fieldDeser.fieldInfo.fieldClass;
            Type fieldType = fieldDeser.fieldInfo.fieldType;
            Object fieldValue;
            if (fieldClass == int.class) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
                fieldValue = IntegerCodec.instance.deserialze(this, fieldType, null);
            } else if (fieldClass == String.class) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
                fieldValue = StringCodec.deserialze(this);
            } else if (fieldClass == long.class) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
                fieldValue = LongCodec.instance.deserialze(this, fieldType, null);
            } else {
                ObjectDeserializer fieldValueDeserializer = config.getDeserializer(fieldClass, fieldType);
                lexer.nextTokenWithColon(fieldValueDeserializer.getFastMatchToken());
                fieldValue = fieldValueDeserializer.deserialze(this, fieldType, null);
            }
            fieldDeser.setValue(object, fieldValue);
        }
        if (lexer.token() == JSONToken.COMMA) {
            continue;
        }
        if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken(JSONToken.COMMA);
            return;
        }
    }
}
Also used : FieldDeserializer(com.alibaba.fastjson.parser.deserializer.FieldDeserializer) ResolveFieldDeserializer(com.alibaba.fastjson.parser.deserializer.ResolveFieldDeserializer) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer) JavaBeanDeserializer(com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer)

Example 2 with JavaBeanDeserializer

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

the class JSONPath method removePropertyValue.

@SuppressWarnings({ "rawtypes" })
protected boolean removePropertyValue(Object parent, String name) {
    if (parent instanceof Map) {
        Object origin = ((Map) parent).remove(name);
        return origin != null;
    }
    ObjectDeserializer derializer = parserConfig.getDeserializer(parent.getClass());
    JavaBeanDeserializer beanDerializer = null;
    if (derializer instanceof JavaBeanDeserializer) {
        beanDerializer = (JavaBeanDeserializer) derializer;
    }
    if (beanDerializer != null) {
        FieldDeserializer fieldDeserializer = beanDerializer.getFieldDeserializer(name);
        if (fieldDeserializer == null) {
            return false;
        }
        fieldDeserializer.setValue(parent, null);
        return true;
    }
    throw new UnsupportedOperationException();
}
Also used : FieldDeserializer(com.alibaba.fastjson.parser.deserializer.FieldDeserializer) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer) JavaBeanDeserializer(com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer)

Example 3 with JavaBeanDeserializer

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

the class TypeUtils method castToJavaBean.

@SuppressWarnings({ "unchecked" })
public static <T> T castToJavaBean(Map<String, Object> map, Class<T> clazz, ParserConfig config) {
    try {
        if (clazz == StackTraceElement.class) {
            String declaringClass = (String) map.get("className");
            String methodName = (String) map.get("methodName");
            String fileName = (String) map.get("fileName");
            int lineNumber;
            {
                Number value = (Number) map.get("lineNumber");
                if (value == null) {
                    lineNumber = 0;
                } else {
                    lineNumber = value.intValue();
                }
            }
            return (T) new StackTraceElement(declaringClass, methodName, fileName, lineNumber);
        }
        {
            Object iClassObject = map.get(JSON.DEFAULT_TYPE_KEY);
            if (iClassObject instanceof String) {
                String className = (String) iClassObject;
                Class<?> loadClazz;
                if (config == null) {
                    config = ParserConfig.global;
                }
                loadClazz = config.checkAutoType(className, null);
                if (loadClazz == null) {
                    throw new ClassNotFoundException(className + " not found");
                }
                if (!loadClazz.equals(clazz)) {
                    return (T) castToJavaBean(map, loadClazz, config);
                }
            }
        }
        if (clazz.isInterface()) {
            JSONObject object;
            if (map instanceof JSONObject) {
                object = (JSONObject) map;
            } else {
                object = new JSONObject(map);
            }
            return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { clazz }, object);
        }
        if (clazz == Locale.class) {
            Object arg0 = map.get("language");
            Object arg1 = map.get("country");
            if (arg0 instanceof String) {
                String language = (String) arg0;
                if (arg1 instanceof String) {
                    String country = (String) arg1;
                    return (T) new Locale(language, country);
                } else if (arg1 == null) {
                    return (T) new Locale(language);
                }
            }
        }
        if (config == null) {
            config = ParserConfig.getGlobalInstance();
        }
        JavaBeanDeserializer javaBeanDeser = null;
        ObjectDeserializer deserizer = config.getDeserializer(clazz);
        if (deserizer instanceof JavaBeanDeserializer) {
            javaBeanDeser = (JavaBeanDeserializer) deserizer;
        }
        if (javaBeanDeser == null) {
            throw new JSONException("can not get javaBeanDeserializer. " + clazz.getName());
        }
        return (T) javaBeanDeser.createInstance(map, config);
    } catch (Exception e) {
        throw new JSONException(e.getMessage(), e);
    }
}
Also used : JSONException(com.alibaba.fastjson.JSONException) ParseException(java.text.ParseException) JSONException(com.alibaba.fastjson.JSONException) AccessControlException(java.security.AccessControlException) JSONObject(com.alibaba.fastjson.JSONObject) AccessibleObject(java.lang.reflect.AccessibleObject) JSONObject(com.alibaba.fastjson.JSONObject) ObjectDeserializer(com.alibaba.fastjson.parser.deserializer.ObjectDeserializer) JavaBeanDeserializer(com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer)

Example 4 with JavaBeanDeserializer

use of com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer 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 5 with JavaBeanDeserializer

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

the class JSONFieldTest_1 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)

Aggregations

JavaBeanDeserializer (com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer)9 ObjectDeserializer (com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)7 FieldDeserializer (com.alibaba.fastjson.parser.deserializer.FieldDeserializer)6 ParameterizedType (java.lang.reflect.ParameterizedType)3 Type (java.lang.reflect.Type)3 HashMap (java.util.HashMap)3 JSONField (com.alibaba.fastjson.annotation.JSONField)2 Field (java.lang.reflect.Field)2 WildcardType (java.lang.reflect.WildcardType)2 IdentityHashMap (java.util.IdentityHashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 JSONException (com.alibaba.fastjson.JSONException)1 JSONObject (com.alibaba.fastjson.JSONObject)1 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)1 ParseContext (com.alibaba.fastjson.parser.ParseContext)1 ContextObjectDeserializer (com.alibaba.fastjson.parser.deserializer.ContextObjectDeserializer)1 ResolveFieldDeserializer (com.alibaba.fastjson.parser.deserializer.ResolveFieldDeserializer)1 AccessibleObject (java.lang.reflect.AccessibleObject)1