Example #1
0
 @Test
 public void checkType() throws IOException {
   List<Type> types =
       Arrays.asList(
           int.class,
           int[].class,
           JsonFull.class,
           JsonFull[].class,
           new Generic<List<Integer>>() {}.type,
           new Generic<Integer[]>() {}.type,
           new Generic<List<JsonFull>>() {}.type,
           new Generic<JsonFull[]>() {}.type);
   DslJson<Object> json = new DslJson<Object>();
   for (Type t : types) {
     Assert.assertTrue(json.canSerialize(t));
     Assert.assertTrue(json.canDeserialize(t));
   }
   Assert.assertTrue(json.canSerialize(JsonPartial.class));
   Assert.assertTrue(json.canSerialize(new Generic<List<JsonPartial>>() {}.type));
   Assert.assertFalse(json.canDeserialize(JsonPartial.class));
   Assert.assertFalse(json.canDeserialize(new Generic<List<JsonPartial>>() {}.type));
   // TODO: not supported yet
   Assert.assertFalse(json.canSerialize(int[][].class));
   Assert.assertFalse(json.canDeserialize(int[][].class));
 }
Example #2
0
 @Test
 public void testPrimitiveArray() throws IOException {
   DslJson<Object> json = new DslJson<Object>();
   JsonWriter writer = new JsonWriter();
   int[] items = new int[] {1, 2};
   json.serialize(writer, items);
   String result = writer.toString();
   Assert.assertEquals("[1,2]", result);
   int[] deserialized = json.deserialize(int[].class, writer.getByteBuffer(), writer.size());
   Assert.assertArrayEquals(items, deserialized);
 }
Example #3
0
 @Ignore("not supported yet")
 @Test
 public void testNestedArray() throws IOException {
   DslJson<Object> json = new DslJson<Object>();
   JsonWriter writer = new JsonWriter();
   int[][] items = new int[2][];
   items[0] = new int[] {1, 2};
   items[1] = new int[] {3, 4, 5};
   json.serialize(writer, items);
   String result = writer.toString();
   Assert.assertEquals("[[1,2],[3,4,5]]", result);
   int[][] deserialized = json.deserialize(int[][].class, writer.getByteBuffer(), writer.size());
   Assert.assertArrayEquals(items, deserialized);
 }
Example #4
0
 @Test
 public void testListGenerics() throws IOException {
   DslJson<Object> json = new DslJson<Object>();
   JsonWriter writer = new JsonWriter();
   List<Integer> items = Arrays.asList(1, 2);
   json.serialize(writer, items);
   String result = writer.toString();
   Assert.assertEquals("[1,2]", result);
   List<Integer> deserialized =
       (List)
           json.deserialize(
               new Generic<List<Integer>>() {}.type, writer.getByteBuffer(), writer.size());
   Assert.assertEquals(items, deserialized);
 }
Example #5
0
 @Test
 public void testReaderOnInterface() throws IOException {
   DslJson<Object> dslJson = new DslJson<Object>();
   dslJson.registerReader(
       Implementation.class,
       new JsonReader.ReadObject<Implementation>() {
         @Override
         public Implementation read(JsonReader reader) throws IOException {
           return null;
         }
       });
   JsonReader.ReadObject<?> reader1 = dslJson.tryFindReader(Interface.class);
   Assert.assertNull(reader1);
   JsonReader.ReadObject<?> reader2 = dslJson.tryFindReader(Implementation.class);
   Assert.assertNotNull(reader2);
   dslJson.registerReader(Interface.class, dslJson.tryFindReader(Implementation.class));
   JsonReader.ReadObject<?> reader3 = dslJson.tryFindReader(Interface.class);
   Assert.assertNotNull(reader3);
 }