From ae77ee0fdcf6d272c58faa2f7b23281ecea6ccbf Mon Sep 17 00:00:00 2001 From: Aidan Follestad Date: Sun, 26 Feb 2017 21:30:23 -0600 Subject: [PATCH] 1.4.2 --- README.md | 8 ++--- build.gradle | 4 +-- src/main/java/com/afollestad/ason/Ason.java | 2 +- .../com/afollestad/ason/AsonSerializer.java | 10 ++++-- src/main/java/com/afollestad/ason/Util.java | 6 ++++ .../afollestad/ason/AsonSerializeTest.java | 36 +++++++++++++++++++ .../java/com/afollestad/ason/AsonTest.java | 12 +++++++ 7 files changed, 68 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e633e15..ea4e71f 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ The dependency is available via jCenter. ```Gradle dependencies { ... - compile 'com.afollestad:ason:1.4.1' + compile 'com.afollestad:ason:1.4.2' } ``` @@ -64,7 +64,7 @@ Since Android includes `org.json` classes, you'll want to exclude the copies pro ```Gradle dependencies { ... - compile('com.afollestad:ason:1.4.1') { + compile('com.afollestad:ason:1.4.2') { exclude group: 'org.json', module: 'json' } } @@ -78,7 +78,7 @@ Android, make sure you also exclude org.json as shown in the section above.* ```Gradle dependencies { ... - compile('com.afollestad:ason:1.4.1') { + compile('com.afollestad:ason:1.4.2') { exclude group: 'com.intellij', module: 'annotations' } } @@ -90,7 +90,7 @@ dependencies { com.afollestad ason - 1.4.1 + 1.4.2 pom ``` diff --git a/build.gradle b/build.gradle index 5d245b9..d5ae58c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'com.afollestad' -version '1.4.1' +version '1.4.2' apply plugin: 'java' apply plugin: 'idea' @@ -32,7 +32,7 @@ publish { userOrg = 'drummer-aidan' groupId = 'com.afollestad' artifactId = 'ason' - publishVersion = '1.4.1' + publishVersion = '1.4.2' website = 'https://github.com/afollestad/ason' } diff --git a/src/main/java/com/afollestad/ason/Ason.java b/src/main/java/com/afollestad/ason/Ason.java index 9588a7f..b57437a 100644 --- a/src/main/java/com/afollestad/ason/Ason.java +++ b/src/main/java/com/afollestad/ason/Ason.java @@ -70,7 +70,7 @@ private Ason putInternal(JSONArray intoArray, } else if (value instanceof AsonArray) { putInternal(intoArray, intoObject, key, ((AsonArray) value).toStockJson()); } else if (value.getClass().isArray()) { - putInternal(intoArray, intoObject, key, serializer.serializeArray((Object[]) value)); + putInternal(intoArray, intoObject, key, serializer.serializeArray(value)); } else if (isList(value.getClass())) { putInternal(intoArray, intoObject, key, serializer.serializeList((List) value)); } else { diff --git a/src/main/java/com/afollestad/ason/AsonSerializer.java b/src/main/java/com/afollestad/ason/AsonSerializer.java index 83038a9..85335bf 100644 --- a/src/main/java/com/afollestad/ason/AsonSerializer.java +++ b/src/main/java/com/afollestad/ason/AsonSerializer.java @@ -1,5 +1,6 @@ package com.afollestad.ason; +import org.jetbrains.annotations.Nullable; import org.json.JSONArray; import org.json.JSONObject; @@ -94,9 +95,9 @@ public AsonArray serializeArray(Object arrayObject) { return result; } - public AsonArray serializeList(List list) { + @Nullable public AsonArray serializeList(List list) { if (list == null || list.isEmpty()) { - return new AsonArray<>(); + return null; } Class componentType = list.get(0).getClass(); Object array = Array.newInstance(componentType, list.size()); @@ -171,7 +172,8 @@ public T deserialize(Ason ason, Class cls) { setFieldValue(field, newObject, deserializeArray(asonArray, type.getComponentType())); } else if (isList(type)) { AsonArray asonArray = ason.get(name); - setFieldValue(field, newObject, deserializeList(asonArray, type)); + Class listItemType = listGenericType(field); + setFieldValue(field, newObject, deserializeList(asonArray, listItemType)); } else { Object value = ason.get(name); if (value instanceof Ason) { @@ -242,6 +244,8 @@ public List deserializeList(AsonArray json, Class cls) { return null; } else if (cls == null) { throw new IllegalArgumentException("Class parameter is required."); + } else if(json.isEmpty()) { + return new ArrayList<>(0); } Class arrayType = Array.newInstance(cls, 0).getClass(); diff --git a/src/main/java/com/afollestad/ason/Util.java b/src/main/java/com/afollestad/ason/Util.java index 829202e..31faeae 100644 --- a/src/main/java/com/afollestad/ason/Util.java +++ b/src/main/java/com/afollestad/ason/Util.java @@ -6,6 +6,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -268,4 +269,9 @@ static boolean isJsonArray(String json) { } return false; } + + static Class listGenericType(Field field) { + ParameterizedType stringListType = (ParameterizedType) field.getGenericType(); + return (Class) stringListType.getActualTypeArguments()[0]; + } } diff --git a/src/test/java/com/afollestad/ason/AsonSerializeTest.java b/src/test/java/com/afollestad/ason/AsonSerializeTest.java index 306607b..15c6969 100644 --- a/src/test/java/com/afollestad/ason/AsonSerializeTest.java +++ b/src/test/java/com/afollestad/ason/AsonSerializeTest.java @@ -31,6 +31,21 @@ public Person() { } } + static class Person2 { + + @AsonName(name = "_id") int id; + List family; + + public Person2() { + family = new ArrayList<>(0); + } + + public Person2(int id) { + this(); + this.id = id; + } + } + // ////// SERIALIZE // @@ -110,6 +125,18 @@ public Person() { assertEquals("[1,2,3,4]", array.toString()); } + @Test public void test_serialize_with_list() { + Person2 person = new Person2(1); + person.family.add(new Person2(2)); + person.family.add(new Person2(3)); + person.family.add(new Person2(4)); + + Ason ason = Ason.serialize(person); + AsonArray array = ason.get("family"); + assertNotNull(array); + assertEquals(array.size(), 3); + } + // ////// DESERIALIZE // @@ -207,6 +234,15 @@ public Person() { assertEquals(4, primitive[3]); } + @Test public void test_deserialize_with_list() { + String input = "{\"_id\":1,\"family\":[{\"_id\":2},{\"_id\":3},{\"_id\":4}]}"; + Person2 result = Ason.deserialize(input, Person2.class); + assertEquals(result.family.size(), 3); + assertEquals(2, result.family.get(0).id); + assertEquals(3, result.family.get(1).id); + assertEquals(4, result.family.get(2).id); + } + // ////// TEST FOR ISSUE #10 // diff --git a/src/test/java/com/afollestad/ason/AsonTest.java b/src/test/java/com/afollestad/ason/AsonTest.java index 0226bb1..d900676 100644 --- a/src/test/java/com/afollestad/ason/AsonTest.java +++ b/src/test/java/com/afollestad/ason/AsonTest.java @@ -2,10 +2,14 @@ import org.junit.Test; +import java.lang.reflect.Field; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import static com.afollestad.ason.Util.isNumber; +import static com.afollestad.ason.Util.listGenericType; import static org.junit.Assert.*; public class AsonTest { @@ -18,6 +22,14 @@ public class AsonTest { } } + @SuppressWarnings({"FieldCanBeLocal", "unused"}) private List listField; + + @Test public void generic_list_type_test() throws Exception { + listField = new ArrayList<>(0); + Field field = AsonTest.class.getDeclaredField("listField"); + assertEquals(Ason.class, listGenericType(field)); + } + @Test public void test_is_number_true() { assertTrue(isNumber("1234")); assertTrue(isNumber("67891023231"));