Skip to content
This repository has been archived by the owner on Jun 16, 2019. It is now read-only.

Commit

Permalink
1.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
afollestad committed Feb 27, 2017
1 parent 66fa22a commit ae77ee0
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand All @@ -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'
}
}
Expand All @@ -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'
}
}
Expand All @@ -90,7 +90,7 @@ dependencies {
<dependency>
<groupId>com.afollestad</groupId>
<artifactId>ason</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
<type>pom</type>
</dependency>
```
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.afollestad'
version '1.4.1'
version '1.4.2'

apply plugin: 'java'
apply plugin: 'idea'
Expand Down Expand Up @@ -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'
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/afollestad/ason/Ason.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/afollestad/ason/AsonSerializer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.afollestad.ason;

import org.jetbrains.annotations.Nullable;
import org.json.JSONArray;
import org.json.JSONObject;

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -171,7 +172,8 @@ public <T> T deserialize(Ason ason, Class<T> 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) {
Expand Down Expand Up @@ -242,6 +244,8 @@ public <T> List<T> deserializeList(AsonArray json, Class<T> cls) {
return null;
} else if (cls == null) {
throw new IllegalArgumentException("Class<T> parameter is required.");
} else if(json.isEmpty()) {
return new ArrayList<>(0);
}

Class<?> arrayType = Array.newInstance(cls, 0).getClass();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/afollestad/ason/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}
}
36 changes: 36 additions & 0 deletions src/test/java/com/afollestad/ason/AsonSerializeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ public Person() {
}
}

static class Person2 {

@AsonName(name = "_id") int id;
List<Person2> family;

public Person2() {
family = new ArrayList<>(0);
}

public Person2(int id) {
this();
this.id = id;
}
}

//
////// SERIALIZE
//
Expand Down Expand Up @@ -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<Person2> array = ason.get("family");
assertNotNull(array);
assertEquals(array.size(), 3);
}

//
////// DESERIALIZE
//
Expand Down Expand Up @@ -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
//
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/afollestad/ason/AsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -18,6 +22,14 @@ public class AsonTest {
}
}

@SuppressWarnings({"FieldCanBeLocal", "unused"}) private List<Ason> 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"));
Expand Down

0 comments on commit ae77ee0

Please sign in to comment.