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);
}
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);
}
}
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);
}
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;
}
Aggregations