use of org.nutz.json.meta.JENObj in project nutz by nutzam.
the class JsonEntityTest method test_entity_field_maker.
@Test
public void test_entity_field_maker() {
Json.clearEntityCache();
JENObj obj = new JENObj();
obj.setAge(100);
obj.setName("name");
obj.setObjId(9l);
Json.setDefaultFieldMaker(new AbstractJsonEntityFieldMaker() {
@Override
public JsonEntityField make(Mirror<?> mirror, Method method) {
return null;
}
@Override
public JsonEntityField make(Mirror<?> mirror, Field field) {
return null;
}
});
assertEquals("{}", Json.toJson(obj, JsonFormat.compact()));
Json.clearEntityCache();
Json.setDefaultFieldMaker(new AbstractJsonEntityFieldMaker() {
@Override
public JsonEntityField make(Mirror<?> mirror, Method method) {
if (method.getName().equals("setName")) {
String fn = Strings.lowerFirst(method.getName().substring(3));
return JsonEntityField.eval("another_name", method.getParameterTypes()[0], mirror.getEjecting(fn), new InjectBySetter(method));
}
return null;
}
@Override
public JsonEntityField make(Mirror<?> mirror, Field field) {
return JsonEntityField.eval("test_" + field.getName(), field.getType(), mirror.getEjecting(field), mirror.getInjecting(field.getName()));
}
});
String json = Json.toJson(obj, JsonFormat.compact());
assertTrue(json.contains("\"test_objId\":9"));
assertTrue(json.contains("\"test_name\":\"name\""));
assertTrue(json.contains("\"test_age\":100"));
assertTrue(json.contains("\"another_name\":\"name\""));
Json.clearEntityCache();
Json.setDefaultFieldMaker(new JsonEntityFieldMakerImpl());
}
use of org.nutz.json.meta.JENObj in project nutz by nutzam.
the class JsonEntityTest method test_simple_JENObj.
@Test
public void test_simple_JENObj() {
String str = "{id:9999999999, name:'abc', age:10}";
JENObj obj = Json.fromJson(JENObj.class, str);
assertEquals(9999999999L, obj.getObjId());
assertEquals("abc", obj.getName());
assertEquals(10, obj.getAge());
str = Json.toJson(obj);
obj = Json.fromJson(JENObj.class, str);
assertEquals(9999999999L, obj.getObjId());
assertEquals("abc", obj.getName());
assertEquals(10, obj.getAge());
}
use of org.nutz.json.meta.JENObj in project nutz by nutzam.
the class JsonEntityTest method test_JENObj_output.
@SuppressWarnings("unchecked")
@Test
public void test_JENObj_output() {
JENObj obj = new JENObj();
String str = Json.toJson(obj, JsonFormat.nice().setIgnoreNull(false));
Map<String, Object> map = (Map<String, Object>) Json.fromJson(str);
String[] keys = map.keySet().toArray(new String[map.size()]);
Arrays.sort(keys);
String keyStr = Lang.concat(",", keys).toString();
assertEquals("age,id,name", keyStr);
}
use of org.nutz.json.meta.JENObj in project nutz by nutzam.
the class JsonTest method test_setting.
// zozoh@2012-09-14:去掉,让 Json 更轻薄一些
// @Test
// public void test_by() {
// TestBy b = new TestBy();
// b.setId(1000);
// Map<String, Object> map = Json.fromJson(Map.class, Json.toJson(b));
// assertEquals(1000, map.get("id"));
// assertEquals("I am OK", map.get("obj"));
// assertEquals("Wendal", map.get("obj2"));
// }
// TODO @Test <- zozoh: 这个用例是不对的,下次如果我看到这个函数,我将删掉它
// #184
public void test_setting() {
String j = "{name2:'abc'}";
JENObj jj = Json.fromJson(JENObj.class, j);
assertEquals("abc", jj.getName());
}
Aggregations