Search in sources :

Example 1 with JENObj

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());
}
Also used : JsonEntityField(org.nutz.json.entity.JsonEntityField) Field(java.lang.reflect.Field) JENObj(org.nutz.json.meta.JENObj) JsonEntityField(org.nutz.json.entity.JsonEntityField) InjectBySetter(org.nutz.lang.inject.InjectBySetter) JsonEntityFieldMakerImpl(org.nutz.json.impl.JsonEntityFieldMakerImpl) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 2 with JENObj

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());
}
Also used : JENObj(org.nutz.json.meta.JENObj) Test(org.junit.Test)

Example 3 with JENObj

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);
}
Also used : JENObj(org.nutz.json.meta.JENObj) Map(java.util.Map) Test(org.junit.Test)

Example 4 with JENObj

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());
}
Also used : JENObj(org.nutz.json.meta.JENObj)

Aggregations

JENObj (org.nutz.json.meta.JENObj)4 Test (org.junit.Test)3 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 JsonEntityField (org.nutz.json.entity.JsonEntityField)1 JsonEntityFieldMakerImpl (org.nutz.json.impl.JsonEntityFieldMakerImpl)1 InjectBySetter (org.nutz.lang.inject.InjectBySetter)1