@Test public void testGenericDecoding() { Pojo original = new Pojo(); original.value = "test"; String json = Json.encode(Collections.singletonList(original)); List<Pojo> correct; correct = Json.decodeValue(json, new TypeReference<List<Pojo>>() {}); assertTrue(((List)correct).get(0) instanceof Pojo); assertEquals(original.value, correct.get(0).value); // same must apply if instead of string we use a buffer correct = Json.decodeValue(Buffer.buffer(json, "UTF8"), new TypeReference<List<Pojo>>() {}); assertTrue(((List)correct).get(0) instanceof Pojo); assertEquals(original.value, correct.get(0).value); List incorrect = Json.decodeValue(json, List.class); assertFalse(incorrect.get(0) instanceof Pojo); assertTrue(incorrect.get(0) instanceof Map); assertEquals(original.value, ((Map)(incorrect.get(0))).get("value")); }
@Test public void testGenericDecoding() { Pojo original = new Pojo(); original.value = "test"; String json = Json.encode(Collections.singletonList(original)); List<Pojo> correct; correct = Json.decodeValue(json, new TypeReference<List<Pojo>>() {}); assertTrue(((List)correct).get(0) instanceof Pojo); assertEquals(original.value, correct.get(0).value); // same must apply if instead of string we use a buffer correct = Json.decodeValue(Buffer.buffer(json, "UTF8"), new TypeReference<List<Pojo>>() {}); assertTrue(((List)correct).get(0) instanceof Pojo); assertEquals(original.value, correct.get(0).value); List incorrect = Json.decodeValue(json, List.class); assertFalse(incorrect.get(0) instanceof Pojo); assertTrue(incorrect.get(0) instanceof Map); assertEquals(original.value, ((Map)(incorrect.get(0))).get("value")); }
@Test public void testNullBytesDecoding() { Pojo original = new Pojo(); Pojo decoded = Json.decodeValue("{\"bytes\":null}", Pojo.class); assertEquals(original.bytes, decoded.bytes); }
@Test public void testNullInstantDecoding() { Pojo original = new Pojo(); Pojo decoded = Json.decodeValue("{\"instant\":null}", Pojo.class); assertEquals(original.instant, decoded.instant); }
@Test public void testBytesDecoding() { Pojo original = new Pojo(); original.bytes = TestUtils.randomByteArray(12); Pojo decoded = Json.decodeValue("{\"bytes\":\"" + Base64.getEncoder().encodeToString(original.bytes) + "\"}", Pojo.class); assertArrayEquals(original.bytes, decoded.bytes); }
@Test public void testInstantDecoding() { Pojo original = new Pojo(); original.instant = Instant.from(ISO_INSTANT.parse("2018-06-20T07:25:38.397Z")); Pojo decoded = Json.decodeValue("{\"instant\":\"2018-06-20T07:25:38.397Z\"}", Pojo.class); assertEquals(original.instant, decoded.instant); }
@Test public void testBytesDecoding() { Pojo original = new Pojo(); original.bytes = TestUtils.randomByteArray(12); Pojo decoded = Json.decodeValue("{\"bytes\":\"" + Base64.getEncoder().encodeToString(original.bytes) + "\"}", Pojo.class); assertArrayEquals(original.bytes, decoded.bytes); }
@Test public void testNullInstantDecoding() { Pojo original = new Pojo(); Pojo decoded = Json.decodeValue("{\"instant\":null}", Pojo.class); assertEquals(original.instant, decoded.instant); }
@Test public void testInstantDecoding() { Pojo original = new Pojo(); original.instant = Instant.from(ISO_INSTANT.parse("2018-06-20T07:25:38.397Z")); Pojo decoded = Json.decodeValue("{\"instant\":\"2018-06-20T07:25:38.397Z\"}", Pojo.class); assertEquals(original.instant, decoded.instant); }
@Test public void testNullBytesDecoding() { Pojo original = new Pojo(); Pojo decoded = Json.decodeValue("{\"bytes\":null}", Pojo.class); assertEquals(original.bytes, decoded.bytes); }