@Test public void testDecode_Reference() { String json = "[{#class:org.jtester.json.encoder.beans.test.User@19762f,id:{#class:Integer,'#value':1},name:{#class:string,'#value':'darui.wu'},age:{#class:Integer,'#value':0},salary:{#class:Double,'#value':0},isFemale:{#class:Boolean,'#value':false}},{#refer:@19762f}]"; User[] users = JSON.toObject(json, User[].class); want.array(users).sizeEq(2); want.object(users[0]).same(users[1]); }
/** 当属性是泛型时 */ @Test(groups = "json") public void testDecodePoJo_PropIsGeneric() { String json = "{first:'wu', addresses: [{street:'凤起路',name:'杭州'}]}"; User user = JSON.toObject(json, User.class); want.object(user) .reflectionEq( new User().setFirst("wu").setAddresses(Arrays.asList(new Address("凤起路", null, "杭州"))), EqMode.IGNORE_DEFAULTS); }
@Test public void testJsonMap() { Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("id", 123); map.put("name", "张三"); String text = JSON.toJSON(map, JSONFeature.UnMarkClassFlag); want.string(text).isEqualTo("{\"id\":123,\"name\":\"张三\"}"); }
@Test public void testDecode_ArrayRef() { String json = "{'key1':{#class:'[I@123',#value:[1,2,3]},'key2':{#refer:@123}}"; Map<String, Integer[]> map = JSON.toObject(json); want.map(map).sizeEq(2).hasKeys("key1", "key2"); Integer[] ia1 = map.get("key1"); Integer[] ia2 = map.get("key2"); want.object(ia1).reflectionEq(new int[] {1, 2, 3}).same(ia2); }
@Test public void testDecode() { String json = "{#class:org.jtester.json.encoder.beans.test.User@19762f,id:{#class:Integer,'#value':1},name:{#class:string,'#value':'darui.wu'},age:{#class:Integer,'#value':0},salary:{#class:Double,'#value':0},isFemale:{#class:Boolean,'#value':false}}"; User[] users = JSON.toObject("[" + json + "," + json + "]", User[].class); want.array(users).sizeEq(2); want.object(users[0]).reflectionEq(users[1]); }
@Test(groups = "json") public void testParseFromJSONMap() { String json = "{id:1,first:'wu',last:'darui'}"; User user = JSON.toObject(json, User.class); want.object(user) .reflectionEqMap( new DataMap() { { this.put("id", 1); this.put("first", "wu"); this.put("last", "darui"); } }); }
@Test(groups = "json") public void testParseFromJSON_Primitive() { String json = "{_integer:1, _boolean:true, _double:4.5d, _float:5.4F}"; PoJo pojo = JSON.toObject(json, PoJo.class); want.object(pojo) .reflectionEq( new PoJo() { { _integer = 1; _boolean = true; _double = 4.5; _float = 5.4F; } }); }
@Test(groups = "json") public void testDecodePoJoArray() { String json = "[{id:1,first:'wu',last:'darui'},{id:2,first:'wu',last:'darui'}]"; User[] users = JSON.toObject(json, User[].class); want.list(users) .reflectionEqMap( 2, new DataMap() { { this.put("id", 1, 2); this.put("first", "wu"); this.put("last", "darui"); } }); }
@Test public void testJsonPojo() { Product p = new Product(); String jsonString = JSON.toJSON(p, JSONFeature.UnMarkClassFlag); want.string(jsonString).isEqualTo("{name:\"myname\",id:100,price:1333}"); }
@Test public void testFromJson() { String json = "{\"name\": \"Banana\",\"id\": 123,\"price\": 23.0}"; Product product = JSON.toObject(json, Product.class); want.object(product).propertyEq("name", "Banana").propertyEq("id", 123); }
@Test public void testParseFromJSONArray() { String json = "['1','2','3']"; Integer[] ints = JSON.toObject(json, int[].class); want.array(ints).sizeEq(3).reflectionEq(new int[] {1, 2, 3}); }