/** * Adds a list of included path queries. */ public JsonSerializer include(final String... includes) { for (String include : includes) { include(include); } return this; }
@Test void testSimpleShallowWithListInMap() { JsonSerializer serializer = new JsonSerializer(); Map wrapper = new HashMap(); wrapper.put("name", "Joe Blow"); wrapper.put("people", people); String peopleJson = serializer.serialize(wrapper); assertFalse(peopleJson.contains("[")); serializer.include("people.*"); peopleJson = serializer.serialize(wrapper); assertTrue(peopleJson.contains("[")); }
@Test void testDeserializeWithIncludes() { JsonParsers.forEachParser(jsonParser -> { Person igor = creator.createJodder(); String json = new JsonSerializer().include("phones", "hobbies").serialize(igor); Person jsonIgor = jsonParser.parse(json, Person.class); assertEquals(2, jsonIgor.getPhones().size()); assertEquals(0, jsonIgor.getHobbies().size()); }); }
@Test void testSettersWithoutGettersAreMissing() { Friend friend = new Friend("Nugget", "Donkey Rider", "Slim"); String json = new JsonSerializer().include("*").serialize(friend); assertAttribute("nicknames", json); assertAttributeMissing("nicknamesAsArray", json); }
@Test void testAnnotations() { HashMap<String, TestClass3> map = new HashMap<>(); map.put("String1", new TestClass3()); TestClass2 testElement = new TestClass2(); testElement.setMapOfJustice(map); String json = new JsonSerializer().serialize(testElement); assertAttributeMissing("mapOfJustice", json); assertAttributeMissing("name", json); assertEquals(-1, json.indexOf("testName2")); json = new JsonSerializer().include("mapOfJustice").serialize(testElement); assertAttribute("mapOfJustice", json); // make sure the name property value is missing! assertAttributeMissing( "name", json ) // conflicts since mapOfJustice contains an object with name in it assertEquals(-1, json.indexOf("testName2")); }
@Test void testConvertPersonToMap2() { Person jodder = new DataCreator().createJodder(); final Map<String, Object> target = new HashMap<>(); JsonContext jsonContext = new JsonSerializer() .include("phones") .excludeTypes(Address.class) .createJsonContext(null); BeanSerializer beanSerializer = new BeanSerializer(jsonContext, jodder) { @Override protected void onSerializableProperty(String propertyName, Class propertyType, Object value) { target.put(propertyName, value); } }; beanSerializer.serialize(); assertEquals(5, target.size()); assertSame(jodder.getBirthdate(), target.get("birthdate")); assertSame(jodder.getFirstBaseBallGame(), target.get("firstBaseBallGame")); assertSame(jodder.getLastname(), target.get("lastname")); assertSame(jodder.getFirstname(), target.get("firstname")); assertSame(jodder.getPhones(), target.get("phones")); }
@Test void testWildcards() { JsonSerializer.Defaults.classMetadataName = "class"; JsonSerializer serializer = new JsonSerializer(); String json = serializer.include("phones").exclude("*.class").serialize(jodder); assertAttributeMissing("class", json); assertAttribute("phones", json); assertAttributeMissing("hobbies", json); }
@Test void testDeserializeInterfaces2() { JsonParsers.forEachParser(jsonParser -> { Hero superman = creator.createSuperman(); String json = new JsonSerializer().include("powers").withClassMetadata(true).serialize(superman); Hero jsonSuperMan = jsonParser.withClassMetadata(true).parse(json, Hero.class); assertNotNull(jsonSuperMan); assertEquals(4, jsonSuperMan.getPowers().size()); assertHeroHasSuperPowers(jsonSuperMan); }); }
@Test void testTransient() { TestClass2 testElement = new TestClass2(); String json = new JsonSerializer().serialize(testElement); assertAttributeMissing("description", json); json = new JsonSerializer().include("description").serialize(testElement); assertAttribute("description", json); }
@Test void testDeserializeInterfaces() { JsonParsers.forEachParser(jsonParser -> { Hero superman = creator.createSuperman(); String json = new JsonSerializer().include("powers").setClassMetadataName("class").serialize(superman); Hero jsonSuperMan = jsonParser.setClassMetadataName("class").parse(json, Hero.class); assertNotNull(jsonSuperMan); assertEquals(4, jsonSuperMan.getPowers().size()); assertHeroHasSuperPowers(jsonSuperMan); }); }
@Test void testNoHintsButClassesForCollection() { JsonParser.Defaults.classMetadataName = "class"; JsonSerializer.Defaults.classMetadataName = "class"; JsonParsers.forEachParser(jsonParser -> { Hero superman = creator.createSuperman(); String json = new JsonSerializer() .exclude("*.class") .include("powers.class") .serialize(superman); Hero jsonSuperMan = jsonParser.parse(json, Hero.class); assertHeroHasSuperPowers(jsonSuperMan); }); }
@Test void testDeepSerializationWithIncludeOverrides() { JsonSerializer serializer = new JsonSerializer(); String peopleJson = serializer.include("people.hobbies").deep(true).serialize(network); assertAttribute("firstname", peopleJson); assertStringValue("Igor", peopleJson); assertAttribute("hobbies", peopleJson); assertStringValue("read", peopleJson); assertStringValue("run", peopleJson); assertStringValue("code", peopleJson); }
@Test void testSubClassDeserialize() { JsonParsers.forEachParser(jsonParser -> { Employee dilbert = creator.createDilbert(); String json = new JsonSerializer().include("phones", "hobbies").serialize(dilbert); Person jsonDilbert = jsonParser.parse(json, Employee.class); assertNotNull(jsonDilbert); assertTrue(jsonDilbert instanceof Employee); assertEquals(dilbert.getCompany(), ((Employee) jsonDilbert).getCompany()); }); }
@Test void testExclude() { String json = new JsonSerializer().serialize(jodder); assertAttribute("firstname", json); assertAttributeMissing("number", json); assertAttributeMissing("exchange", json); assertAttributeMissing("areaCode", json); json = new JsonSerializer().include("phones").serialize(jodder); assertAttribute("firstname", json); assertAttribute("number", json); assertAttribute("exchange", json); assertAttribute("areaCode", json); json = new JsonSerializer().include("phones").exclude("phones.areaCode").serialize(jodder); assertAttribute("firstname", json); assertAttribute("number", json); assertAttribute("exchange", json); assertAttributeMissing("areaCode", json); }
@Test void testNoClassHintsForCollections() { JsonParser.Defaults.classMetadataName = "class"; JsonSerializer.Defaults.classMetadataName = "class"; JsonParsers.forEachParser(jsonParser -> { Hero superman = creator.createSuperman(); String json = new JsonSerializer() .include("powers") // redudant .include("powers.class") .withSerializer("powers.class", new SimpleClassnameTransformer()) .exclude("*.class") .serialize(superman); int count = StringUtil.count(json, "***"); assertEquals(4, count); json = StringUtil.remove(json, "***"); Hero jsonSuperMan = jsonParser .map("lair", SecretLair.class) .map("secretIdentity", SecretIdentity.class) .parse(json, Hero.class); assertEquals("Fortress of Solitude", jsonSuperMan.getLair().getName()); assertHeroHasSuperPowers(jsonSuperMan); }); }
@Test void testDeepIncludes() { JsonSerializer serializer = new JsonSerializer(); String peopleJson = serializer.include("people.hobbies").serialize(network); assertAttribute("name", peopleJson); assertStringValue("My Network", peopleJson); assertAttribute("firstname", peopleJson); assertStringValue("Igor", peopleJson); assertStringValue("Modesty", peopleJson); assertAttribute("lastname", peopleJson); assertStringValue("Spasic", peopleJson); assertAttribute("hobbies", peopleJson); assertStringValue("read", peopleJson); }
@Test void testMixedWildcards() { JsonSerializer serializer = new JsonSerializer(); serializer.include("firstname", "lastname").exclude("*"); String json = serializer.serialize(jodder); assertAttribute("firstname", json); assertStringValue("Igor", json); assertAttribute("lastname", json); assertStringValue("Spasic", json); assertAttributeMissing("class", json); assertAttributeMissing("phones", json); assertAttributeMissing("birthdate", json); serializer = new JsonSerializer(); serializer.include("firstname", "lastname", "phones.areaCode", "phones.exchange", "phones.number").exclude("*"); json = serializer.serialize(jodder); assertAttribute("firstname", json); assertStringValue("Igor", json); assertAttribute("lastname", json); assertStringValue("Spasic", json); assertAttributeMissing("class", json); assertAttribute("phones", json); assertAttributeMissing("birthdate", json); }
@Test void testSetIncludes() { JsonSerializer serializer = new JsonSerializer(); serializer.include("people.hobbies", "phones", "home", "people.resume"); assertEquals(4, serializer.rules.totalRules()); assertEquals("[people.hobbies]", serializer.rules.getRule(0).toString()); assertEquals("[phones]", serializer.rules.getRule(1).toString()); assertEquals("[home]", serializer.rules.getRule(2).toString()); assertEquals("[people.resume]", serializer.rules.getRule(3).toString()); }
@Test void testArrayType() { JsonParsers.forEachParser(jsonParser -> { Person igor = creator.createJodder(); Person modesty = creator.createModesty(); Group group = new Group("brothers", igor, modesty); String json = new JsonSerializer().include("people").exclude("*.class").serialize(group); Group bro = jsonParser.map(Group.class).parse(json); assertNotNull(bro); assertEquals("brothers", bro.getGroupName()); assertEquals(2, bro.getPeople().length); assertEquals("Igor", bro.getPeople()[0].getFirstname()); assertEquals("Modesty", bro.getPeople()[1].getFirstname()); }); }