From d01d0dd0aeea4a0a7e4f4b758fc2ae5c3e0220ff Mon Sep 17 00:00:00 2001 From: Aidan Follestad Date: Sat, 4 Feb 2017 18:58:46 -0600 Subject: [PATCH] Added more tests --- .../com/afollestad/json/AsonSerializer.java | 8 +- .../afollestad/json/AsonPrimitivesTest.java | 94 ++++++++++++++++--- 2 files changed, 90 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/afollestad/json/AsonSerializer.java b/src/main/java/com/afollestad/json/AsonSerializer.java index baac3a7..bacd597 100644 --- a/src/main/java/com/afollestad/json/AsonSerializer.java +++ b/src/main/java/com/afollestad/json/AsonSerializer.java @@ -190,7 +190,13 @@ public T deserializeArray(AsonArray json, Class cls) { for (int i = 0; i < json.size(); i++) { if (isPrimitive(component)) { - Array.set(newArray, i, json.get(i)); + Object value = json.get(i); + if (component == char.class || component == Character.class) { + value = ((String) value).charAt(0); + } else if (component == short.class || component == Short.class) { + value = Short.parseShort(Integer.toString((int) value)); + } + Array.set(newArray, i, value); } else if (component.isArray()) { AsonArray subArray = (AsonArray) json.get(i); Class arrayComponent = component.getComponentType(); diff --git a/src/test/java/com/afollestad/json/AsonPrimitivesTest.java b/src/test/java/com/afollestad/json/AsonPrimitivesTest.java index 87adcce..34af896 100644 --- a/src/test/java/com/afollestad/json/AsonPrimitivesTest.java +++ b/src/test/java/com/afollestad/json/AsonPrimitivesTest.java @@ -6,6 +6,18 @@ public class AsonPrimitivesTest { + @Test public void short_test() { + AsonArray result = Ason.serializeArray(new short[]{1, 2, 3, 4}); + String output = result.toString(); + assertEquals("[1,2,3,4]", output); + + short[] parsed = Ason.deserialize(output, short[].class); + assertEquals(1, parsed[0]); + assertEquals(2, parsed[1]); + assertEquals(3, parsed[2]); + assertEquals(4, parsed[3]); + } + @Test public void int_test() { AsonArray result = Ason.serializeArray(new int[]{1, 2, 3, 4}); String output = result.toString(); @@ -18,15 +30,75 @@ public class AsonPrimitivesTest { assertEquals(4, parsed[3]); } -// @Test public void char_test() { -// AsonArray result = AsonArray.serialize(new char[]{'a', 'b', 'c', 'd'}); -// String output = result.toString(); -// assertEquals("[a,b,c,d]", output); -// -// char[] parsed = AsonArray.deserializeArray(new AsonArray(output)); -// assertEquals(1, parsed[0]); -// assertEquals(2, parsed[1]); -// assertEquals(3, parsed[2]); -// assertEquals(4, parsed[3]); -// } + @Test public void long_test() { + AsonArray result = Ason.serializeArray(new long[]{1L, 2L, 3L, 4L}); + String output = result.toString(); + assertEquals("[1,2,3,4]", output); + + long[] parsed = Ason.deserialize(output, long[].class); + assertEquals(1L, parsed[0]); + assertEquals(2L, parsed[1]); + assertEquals(3L, parsed[2]); + assertEquals(4L, parsed[3]); + } + + @Test public void float_test() { + AsonArray result = Ason.serializeArray(new float[]{1f, 2f, 3f, 4f}); + String output = result.toString(); + assertEquals("[1,2,3,4]", output); + + float[] parsed = Ason.deserialize(output, float[].class); + assertEquals(1f, parsed[0], 1f); + assertEquals(2f, parsed[1], 1f); + assertEquals(3f, parsed[2], 1f); + assertEquals(4f, parsed[3], 1f); + } + + @Test public void double_test() { + AsonArray result = Ason.serializeArray(new double[]{1d, 2d, 3d, 4d}); + String output = result.toString(); + assertEquals("[1,2,3,4]", output); + + double[] parsed = Ason.deserialize(output, double[].class); + assertEquals(1d, parsed[0], 1d); + assertEquals(2d, parsed[1], 1d); + assertEquals(3d, parsed[2], 1d); + assertEquals(4d, parsed[3], 1d); + } + + @Test public void char_test() { + AsonArray result = Ason.serializeArray(new char[]{'a', 'b', 'c', 'd'}); + String output = result.toString(); + assertEquals("[\"a\",\"b\",\"c\",\"d\"]", output); + + char[] parsed = Ason.deserialize(output, char[].class); + assertEquals('a', parsed[0]); + assertEquals('b', parsed[1]); + assertEquals('c', parsed[2]); + assertEquals('d', parsed[3]); + } + + @Test public void byte_test() { + AsonArray result = Ason.serializeArray(new byte[]{1, 2, 3, 4}); + String output = result.toString(); + assertEquals("[1,2,3,4]", output); + + int[] parsed = Ason.deserialize(output, int[].class); + assertEquals(1, parsed[0]); + assertEquals(2, parsed[1]); + assertEquals(3, parsed[2]); + assertEquals(4, parsed[3]); + } + + @Test public void boolean_test() { + AsonArray result = Ason.serializeArray(new boolean[]{true, false, true, false}); + String output = result.toString(); + assertEquals("[true,false,true,false]", output); + + boolean[] parsed = Ason.deserialize(output, boolean[].class); + assertEquals(true, parsed[0]); + assertEquals(false, parsed[1]); + assertEquals(true, parsed[2]); + assertEquals(false, parsed[3]); + } } \ No newline at end of file