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