@Test public void testSimpleClass() throws Exception { JsonCodec<Person> jsonCodec = new JsonCodecBuilder().prettyPrint().build(Person.class); Person expected = new Person().setName("dain").setRocks(true); String json = jsonCodec.toJson(expected); Person actual = jsonCodec.fromJson(json); Assert.assertEquals(actual, expected); }
@Test public void testMetadataInputStream() throws Exception { MetaDataInputStream mis = new MetaDataInputStream( getClass() .getClassLoader() .getResourceAsStream("META-INF/services/org.brutusin.json.spi.JsonCodec"), null, null, Long.MIN_VALUE); TestClass instance = new TestClass(); String json = JsonCodec.getInstance().transform(instance); assertTrue(!json.contains("\"metaDataInputStream\"")); instance.setMetaDataInputStream(mis); json = JsonCodec.getInstance().transform(instance); assertTrue(json.contains("\"metaDataInputStream\":\"#1#")); JsonNode node = JsonCodec.getInstance().toJsonNode(instance); assertEquals(node.get("metaDataInputStream").asStream(), mis); TestClass instance2 = JsonCodec.getInstance().load(node, TestClass.class); assertEquals(instance.getMetaDataInputStream(), instance2.getMetaDataInputStream()); Map<String, InputStream> streams = new HashMap(); streams.put(node.get("metaDataInputStream").asString(), mis); JsonNode node2 = JsonCodec.getInstance().parse(node.toString(), streams); assertEquals(node2.get("metaDataInputStream").asStream(), mis); JsonNode node3 = JsonCodec.getInstance().parse(node.toString(), streams); TestClass instance3 = JsonCodec.getInstance().load(node3, TestClass.class); assertEquals(instance.getInputStream(), instance3.getInputStream()); assertTrue( JsonCodec.getInstance() .getReferencedStreamCount(node, JsonCodec.getInstance().getSchema(TestClass.class)) .equals(1)); }
@Test public void testJsonPropertyAnnotationSupport() throws Exception { TestClass instance = new TestClass(); instance.setAint(3); instance.setBolArr(new boolean[] {true, false}); instance.setString("aaa"); instance.setBooleanMap(new HashMap()); instance.getBooleanMap().put("1", Boolean.TRUE); String json = JsonCodec.getInstance().transform(instance); TestClass instance2 = JsonCodec.getInstance().parse(json, TestClass.class); String json2 = JsonCodec.getInstance().transform(instance2); assertEquals(json, json2); assertEquals(instance.getAint(), instance2.getAint()); }
@Test public void testTransientNonSerialization() throws Exception { TestClass instance = new TestClass(); String json = JsonCodec.getInstance().transform(instance); System.out.println(json); assert (!json.contains("nonSerializable")); }