private void assertSerializedTo(String original, String expected) { JsonSerializer serializer = new JsonSerializer(); String json = serializer.serialize(original); assertEquals(expected, json); }
/** * Serializes object into provided appendable. */ public void serialize(final Object source, final Appendable target) { JsonContext jsonContext = createJsonContext(target); jsonContext.serialize(source); }
@Test void testClassMetaData() { String json = JsonSerializer .create() .withClassMetadata(true) .serialize(new Foo()); assertTrue(json.contains("\"__class\":\"" + Foo.class.getName() + "\"")); json = JsonSerializer.create().withClassMetadata(false).serialize(123); assertEquals("123", json); json = JsonSerializer.create().withClassMetadata(true).serialize(123); assertEquals("123", json); }
@Test void testDeepSerializationCycles() { JsonSerializer serializer = new JsonSerializer(); String json = serializer.deep(true).serialize(people); assertAttribute("zipcode", json); assertEquals(2, occurs(pedroZip.getZipcode(), json)); assertAttributeMissing("person", json); }
@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 testExcludeAll() { JsonSerializer serializer = new JsonSerializer(); String json = serializer.exclude("*").serialize(jodder); assertEquals("{}", json); }
@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 testDeepSerializationWithExcludes() { JsonSerializer serializer = new JsonSerializer(); String peopleJson = serializer.exclude("people.work").deep(true).serialize(network); assertAttribute("firstname", peopleJson); assertStringValue("Igor", peopleJson); assertAttributeMissing("work", peopleJson); assertStringValue("173 Hackers Drive", peopleJson); assertAttribute("home", peopleJson); assertAttribute("phones", peopleJson); }
@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 testJsonValueContextArray2() { JsonParsers.forEachParser(jsonParser -> { Object[] array = new Object[]{new Surfer(), "two", "three"}; JsonSerializer jsonSerializer = new JsonSerializer() .withSerializer(String.class, new MyTypeJsonSerializer2()); String json = jsonSerializer.serialize(array); List<String> list = jsonParser.parse(json); assertEquals(3, list.size()); assertEquals("TWO", list.get(1)); }); }
@Test void testAnnNameWithClass() { JsonParsers.forEachParser(jsonParser -> { Location location = new Location(); location.setLatitude(65); location.setLongitude(12); String json = new JsonSerializer().setClassMetadataName("class").serialize(location); assertEquals("{\"class\":\"" + Location.class.getName() + "\",\"lat\":65,\"lng\":12}", json); Location jsonLocation = jsonParser.setClassMetadataName("class").parse(json, Location.class); assertEquals(location.getLatitude(), jsonLocation.getLatitude()); assertEquals(location.getLongitude(), jsonLocation.getLongitude()); }); }
@Test void testSerializeStringEscapes() { JsonParsers.forEachParser(jsonParser -> { String path = "/foo/bar"; String json = JsonSerializer .create() .strictStringEncoding(true) .serialize(path); assertEquals("\"\\/foo\\/bar\"", json); String path2 = jsonParser.parse(json); assertEquals(path, path2); }); }
@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 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 testEscapeChars() { JsonParsers.forEachParser(jsonParser -> { String json = "\"1\\\" 2\\\\ 3\\/ 4\\b 5\\f 6\\n 7\\r 8\\t\""; String str = jsonParser.parse(json); assertEquals("1\" 2\\ 3/ 4\b 5\f 6\n 7\r 8\t", str); String jsonStr = new JsonSerializer().strictStringEncoding(true).serialize(str); assertEquals(json, jsonStr); }); }
/** * Serializes the object, but returns the {@link CharSequence}. */ public CharSequence serializeToCharSequence(final Object source) { FastCharBuffer fastCharBuffer = new FastCharBuffer(); serialize(source, fastCharBuffer); return fastCharBuffer; }
@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")); }