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