Search in sources :

Example 1 with ObjectSerializer

use of com.alibaba.fastjson.serializer.ObjectSerializer 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 ObjectSerializer

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

the class ErrorTest method test_error.

public void test_error() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.put(A.class, new ObjectSerializer() {

        public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
            throw new IOException();
        }
    });
    JSONSerializer ser = new JSONSerializer(config);
    {
        Exception error = null;
        try {
            ser.write(new A());
        } catch (JSONException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        Exception error = null;
        try {
            B b = new B();
            b.setId(new A());
            ser.write(b);
        } catch (JSONException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
}
Also used : Type(java.lang.reflect.Type) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) JSONException(com.alibaba.fastjson.JSONException) ObjectSerializer(com.alibaba.fastjson.serializer.ObjectSerializer) IOException(java.io.IOException) JSONException(com.alibaba.fastjson.JSONException) IOException(java.io.IOException) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 3 with ObjectSerializer

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

the class ObjectWriteTest method test_objectWriteTest.

public void test_objectWriteTest() throws Exception {
    ObjectSerializer serializer = SerializeConfig.getGlobalInstance().getObjectWriter(Model.class);
    JSONSerializer jsonSerializer = new JSONSerializer();
    serializer.write(jsonSerializer, null, "a", Model.class, 0);
    String text = jsonSerializer.out.toString();
    assertEquals("null", text);
}
Also used : ObjectSerializer(com.alibaba.fastjson.serializer.ObjectSerializer) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 4 with ObjectSerializer

use of com.alibaba.fastjson.serializer.ObjectSerializer 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)

Aggregations

ObjectSerializer (com.alibaba.fastjson.serializer.ObjectSerializer)4 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)2 JavaBeanSerializer (com.alibaba.fastjson.serializer.JavaBeanSerializer)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Map (java.util.Map)2 JSONException (com.alibaba.fastjson.JSONException)1 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1