Search in sources :

Example 1 with JavaBeanSerializer

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

the class JSON method toJSON.

@SuppressWarnings("unchecked")
public static Object toJSON(Object javaObject, SerializeConfig config) {
    if (javaObject == null) {
        return null;
    }
    if (javaObject instanceof JSON) {
        return javaObject;
    }
    if (javaObject instanceof Map) {
        Map<Object, Object> map = (Map<Object, Object>) javaObject;
        JSONObject json = new JSONObject(map.size());
        for (Map.Entry<Object, Object> entry : map.entrySet()) {
            Object key = entry.getKey();
            String jsonKey = TypeUtils.castToString(key);
            Object jsonValue = toJSON(entry.getValue());
            json.put(jsonKey, jsonValue);
        }
        return json;
    }
    if (javaObject instanceof Collection) {
        Collection<Object> collection = (Collection<Object>) javaObject;
        JSONArray array = new JSONArray(collection.size());
        for (Object item : collection) {
            Object jsonValue = toJSON(item);
            array.add(jsonValue);
        }
        return array;
    }
    Class<?> clazz = javaObject.getClass();
    if (clazz.isEnum()) {
        return ((Enum<?>) javaObject).name();
    }
    if (clazz.isArray()) {
        int len = Array.getLength(javaObject);
        JSONArray array = new JSONArray(len);
        for (int i = 0; i < len; ++i) {
            Object item = Array.get(javaObject, i);
            Object jsonValue = toJSON(item);
            array.add(jsonValue);
        }
        return array;
    }
    if (ParserConfig.isPrimitive2(clazz)) {
        return javaObject;
    }
    ObjectSerializer serializer = config.getObjectWriter(clazz);
    if (serializer instanceof JavaBeanSerializer) {
        JavaBeanSerializer javaBeanSerializer = (JavaBeanSerializer) serializer;
        JSONObject json = new JSONObject();
        try {
            Map<String, Object> values = javaBeanSerializer.getFieldValuesMap(javaObject);
            for (Map.Entry<String, Object> entry : values.entrySet()) {
                json.put(entry.getKey(), toJSON(entry.getValue()));
            }
        } catch (Exception e) {
            throw new JSONException("toJSON error", e);
        }
        return json;
    }
    String text = JSON.toJSONString(javaObject);
    return JSON.parse(text);
}
Also used : JavaBeanSerializer(com.alibaba.fastjson.serializer.JavaBeanSerializer) IOException(java.io.IOException) Collection(java.util.Collection) ObjectSerializer(com.alibaba.fastjson.serializer.ObjectSerializer) Map(java.util.Map)

Example 2 with JavaBeanSerializer

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

the class JSONPath method getPropertyValues.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected Collection<Object> getPropertyValues(final Object currentObject) {
    final Class<?> currentClass = currentObject.getClass();
    JavaBeanSerializer beanSerializer = getJavaBeanSerializer(currentClass);
    if (beanSerializer != null) {
        try {
            return beanSerializer.getFieldValues(currentObject);
        } catch (Exception e) {
            throw new JSONPathException("jsonpath error, path " + path, e);
        }
    }
    if (currentObject instanceof Map) {
        Map map = (Map) currentObject;
        return map.values();
    }
    throw new UnsupportedOperationException();
}
Also used : JavaBeanSerializer(com.alibaba.fastjson.serializer.JavaBeanSerializer) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with JavaBeanSerializer

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

the class JSON_toJSONStringTest method test_1.

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

Example 4 with JavaBeanSerializer

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

the class TypeUtilsTest_castToJavaBean method test_bean_2.

public void test_bean_2() 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", "ID")));
        serializer.write(vo);
        Assert.assertEquals("{\"ID\":123}", 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 5 with JavaBeanSerializer

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

the class JavaBeanSerializerTest method test_0_s.

public void test_0_s() throws Exception {
    SerializeWriter out = new SerializeWriter();
    A a = new A();
    a.getL0().add("A");
    a.getL0().add("B");
    JavaBeanSerializer serializer = new JavaBeanSerializer(A.class);
    serializer.write(new JSONSerializer(out), a, null, null, 0);
    Assert.assertEquals("{\"l0\":[\"A\",\"B\"]}", 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