Search in sources :

Example 11 with JavaBeanSerializer

use of com.alibaba.fastjson.serializer.JavaBeanSerializer in project fastjson by alibaba.

the class JSONPath method paths.

@SuppressWarnings("rawtypes")
private static void paths(Map<Object, String> paths, String parent, Object javaObject, SerializeConfig config) {
    if (javaObject == null) {
        return;
    }
    if (paths.containsKey(javaObject)) {
        return;
    }
    paths.put(javaObject, parent);
    if (javaObject instanceof Map) {
        Map map = (Map) javaObject;
        for (Object entryObj : map.entrySet()) {
            Map.Entry entry = (Map.Entry) entryObj;
            Object key = entry.getKey();
            if (key instanceof String) {
                String path = parent.equals("/") ? "/" + key : parent + "/" + key;
                paths(paths, path, entry.getValue(), config);
            }
        }
        return;
    }
    if (javaObject instanceof Collection) {
        Collection collection = (Collection) javaObject;
        int i = 0;
        for (Object item : collection) {
            String path = parent.equals("/") ? "/" + i : parent + "/" + i;
            paths(paths, path, item, config);
            ++i;
        }
        return;
    }
    Class<?> clazz = javaObject.getClass();
    if (clazz.isArray()) {
        int len = Array.getLength(javaObject);
        for (int i = 0; i < len; ++i) {
            Object item = Array.get(javaObject, i);
            String path = parent.equals("/") ? "/" + i : parent + "/" + i;
            paths(paths, path, item, config);
        }
        return;
    }
    if (ParserConfig.isPrimitive2(clazz) || clazz.isEnum()) {
        return;
    }
    ObjectSerializer serializer = config.getObjectWriter(clazz);
    if (serializer instanceof JavaBeanSerializer) {
        JavaBeanSerializer javaBeanSerializer = (JavaBeanSerializer) serializer;
        try {
            Map<String, Object> fieldValues = javaBeanSerializer.getFieldValuesMap(javaObject);
            for (Map.Entry<String, Object> entry : fieldValues.entrySet()) {
                String key = entry.getKey();
                if (key instanceof String) {
                    String path = parent.equals("/") ? "/" + key : parent + "/" + key;
                    paths(paths, path, entry.getValue(), config);
                }
            }
        } catch (Exception e) {
            throw new JSONException("toJSON error", e);
        }
        return;
    }
    return;
}
Also used : JavaBeanSerializer(com.alibaba.fastjson.serializer.JavaBeanSerializer) InvocationTargetException(java.lang.reflect.InvocationTargetException) Collection(java.util.Collection) ObjectSerializer(com.alibaba.fastjson.serializer.ObjectSerializer) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 12 with JavaBeanSerializer

use of com.alibaba.fastjson.serializer.JavaBeanSerializer in project fastjson by alibaba.

the class JSON_toJSONStringTest method test_0.

public void test_0() throws Exception {
    User user = new User();
    user.setId(123);
    user.setName("毛头");
    SerializeConfig mapping = new SerializeConfig();
    mapping.put(User.class, new JavaBeanSerializer(User.class, "id"));
    JSONSerializer serializer = new JSONSerializer(mapping);
    serializer.write(user);
    String jsonString = serializer.toString();
    Assert.assertEquals("{\"id\":123}", jsonString);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) JavaBeanSerializer(com.alibaba.fastjson.serializer.JavaBeanSerializer) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 13 with JavaBeanSerializer

use of com.alibaba.fastjson.serializer.JavaBeanSerializer in project fastjson by alibaba.

the class TypeUtilsTest_castToJavaBean method test_bean_3.

public void test_bean_3() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", 123);
    PO vo = TypeUtils.castToJavaBean(map, PO.class);
    Assert.assertEquals(123, vo.id);
    SerializeWriter out = new SerializeWriter();
    try {
        SerializeConfig config = new SerializeConfig();
        JSONSerializer serializer = new JSONSerializer(out, config);
        config.put(PO.class, new JavaBeanSerializer(PO.class, Collections.singletonMap("id", (String) null)));
        serializer.write(vo);
        Assert.assertEquals("{}", out.toString());
    } finally {
        out.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) HashMap(java.util.HashMap) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) JavaBeanSerializer(com.alibaba.fastjson.serializer.JavaBeanSerializer) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 14 with JavaBeanSerializer

use of com.alibaba.fastjson.serializer.JavaBeanSerializer in project fastjson by alibaba.

the class JavaBeanSerializerTest method test_error_s.

public void test_error_s() throws Exception {
    JSONException error = null;
    try {
        SerializeWriter out = new SerializeWriter();
        JavaBeanSerializer serializer = new JavaBeanSerializer(C.class);
        serializer.write(new JSONSerializer(out), new C(), null, null, 0);
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONException(com.alibaba.fastjson.JSONException) JavaBeanSerializer(com.alibaba.fastjson.serializer.JavaBeanSerializer) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 15 with JavaBeanSerializer

use of com.alibaba.fastjson.serializer.JavaBeanSerializer in project fastjson by alibaba.

the class JavaBeanSerializerTest method test_2_s.

public void test_2_s() throws Exception {
    SerializeWriter out = new SerializeWriter();
    JavaBeanSerializer serializer = new JavaBeanSerializer(F.class);
    serializer.write(new JSONSerializer(out), new F(new E(123)), null, null, 0);
    Assert.assertEquals("{\"e\":{\"id\":123}}", out.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JavaBeanSerializer(com.alibaba.fastjson.serializer.JavaBeanSerializer) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Aggregations

JavaBeanSerializer (com.alibaba.fastjson.serializer.JavaBeanSerializer)15 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)10 SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)8 HashMap (java.util.HashMap)6 Map (java.util.Map)5 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 IdentityHashMap (java.util.IdentityHashMap)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 Collection (java.util.Collection)3 JSONException (com.alibaba.fastjson.JSONException)2 ObjectSerializer (com.alibaba.fastjson.serializer.ObjectSerializer)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 FieldSerializer (com.alibaba.fastjson.serializer.FieldSerializer)1 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1