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

Commit

Permalink
Increased AsonSerializer coverage by a lot.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidan Follestad committed Mar 2, 2017
1 parent 8fd251d commit 7d2548f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/main/java/com/afollestad/ason/AsonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static AsonSerializer get() {

@Nullable
public Ason serialize(@Nullable Object object) {
if (object == null) {
if (Util.isNull(object)) {
return null;
} else if (object instanceof Ason
|| object instanceof AsonArray
Expand Down Expand Up @@ -76,7 +76,7 @@ public Ason serialize(@Nullable Object object) {

@Nullable
public AsonArray serializeArray(@Nullable Object arrayObject) {
if (arrayObject == null) {
if (isNull(arrayObject)) {
return null;
}

Expand Down Expand Up @@ -109,9 +109,12 @@ public AsonArray serializeArray(@Nullable Object arrayObject) {

@Nullable
public AsonArray serializeList(@Nullable List list) {
if (list == null || list.isEmpty()) {
if (isNull(list)) {
return null;
} else if (list.isEmpty()) {
return new AsonArray();
}

Class<?> componentType = list.get(0).getClass();
Object array = Array.newInstance(componentType, list.size());
for (int i = 0; i < list.size(); i++) {
Expand All @@ -128,7 +131,7 @@ Object serializeField(final Field field, final Object object) {
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
if (fieldValue == null) {
if (isNull(fieldValue)) {
return null;
}
if (isPrimitive(fieldValue)
Expand All @@ -152,7 +155,7 @@ Object serializeField(final Field field, final Object object) {

@Nullable
public <T> T deserialize(@Nullable Ason ason, @NotNull Class<T> cls) {
if (ason == null) {
if (isNull(ason)) {
return null;
} else if (isPrimitive(cls)) {
throw new IllegalArgumentException(
Expand All @@ -167,7 +170,7 @@ public <T> T deserialize(@Nullable Ason ason, @NotNull Class<T> cls) {
}

ClassCacheEntry<T> cacheEntry = classCache.get(cls.getName());
if (cacheEntry == null) {
if (isNull(cacheEntry)) {
cacheEntry = new ClassCacheEntry<>(cls);
classCache.put(cls.getName(), cacheEntry);
}
Expand Down Expand Up @@ -205,7 +208,7 @@ public <T> T deserialize(@Nullable Ason ason, @NotNull Class<T> cls) {

@Nullable
public <T> T deserializeArray(@Nullable AsonArray json, @NotNull Class<T> cls) {
if (json == null) {
if (isNull(json)) {
return null;
} else if (!cls.isArray() && cls != Object.class) {
if (isList(cls)) {
Expand Down Expand Up @@ -266,17 +269,14 @@ public <T> T deserializeArray(@Nullable AsonArray json, @NotNull Class<T> cls) {

@Nullable
public <T> List<T> deserializeList(@Nullable AsonArray json, @NotNull Class<T> cls) {
if (json == null) {
if (isNull(json)) {
return null;
} else if (json.isEmpty()) {
return new ArrayList<>(0);
}

Class<?> arrayType = Array.newInstance(cls, 0).getClass();
Object array = deserializeArray(json, arrayType);
if (array == null) {
return null;
}

int length = Array.getLength(array);
List<T> result = new ArrayList<>();
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/com/afollestad/ason/AsonSerializeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,60 @@ public void test_deserialize_array_of_arrays() {
assertArrayEquals(two, matrix[1]);
}

@SuppressWarnings("unchecked")
@Test
public void test_deserialize_array_of_lists() {
List<Integer> one = new ArrayList<>(4);
one.add(1);
one.add(2);
one.add(3);
one.add(4);

List<Integer> two = new ArrayList<>(4);
two.add(5);
two.add(6);
two.add(7);
two.add(8);

AsonArray<List<Integer>> jsonArray = new AsonArray<List<Integer>>().add(one, two);
List<Integer>[] result = AsonSerializer.get().deserializeArray(jsonArray, List[].class);
assertNotNull(result);

one = result[0];
assertEquals(1, one.get(0).intValue());
assertEquals(2, one.get(1).intValue());
assertEquals(3, one.get(2).intValue());
assertEquals(4, one.get(3).intValue());

two = result[1];
assertEquals(5, two.get(0).intValue());
assertEquals(6, two.get(1).intValue());
assertEquals(7, two.get(2).intValue());
assertEquals(8, two.get(3).intValue());
}

@Test
public void test_deserialize_array_of_empty_lists() {
List<Integer> one = new ArrayList<>(0);
List<Integer> two = new ArrayList<>(0);

AsonArray<List<Integer>> jsonArray = new AsonArray<List<Integer>>().add(one, two);
List<Integer>[] result = AsonSerializer.get().deserializeArray(jsonArray, List[].class);
assertNotNull(result);
assertEquals(0, result[0].size());
assertEquals(0, result[1].size());
}

@SuppressWarnings("unchecked")
@Test
public void test_deserialize_array_of_null_lists() {
AsonArray<List<Integer>> jsonArray = new AsonArray<List<Integer>>().addNull().addNull();
List<Integer>[] result = AsonSerializer.get().deserializeArray(jsonArray, List[].class);
assertNotNull(result);
assertNull(result[0]);
assertNull(result[1]);
}

//
////// TEST FOR ISSUE #10
//
Expand Down

0 comments on commit 7d2548f

Please sign in to comment.