diff --git a/.idea/modules/ason.iml b/.idea/modules/ason.iml index d747dc3..2038642 100644 --- a/.idea/modules/ason.iml +++ b/.idea/modules/ason.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/ason_main.iml b/.idea/modules/ason_main.iml index 627e145..75cfad8 100644 --- a/.idea/modules/ason_main.iml +++ b/.idea/modules/ason_main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/ason_test.iml b/.idea/modules/ason_test.iml index 71e369b..753dc26 100644 --- a/.idea/modules/ason_test.iml +++ b/.idea/modules/ason_test.iml @@ -1,5 +1,5 @@ - + diff --git a/README.md b/README.md index b57113e..866033a 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.3' + compile 'com.afollestad:ason:1.4.4' } ``` @@ -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.3') { + compile('com.afollestad:ason:1.4.4') { 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.3') { + compile('com.afollestad:ason:1.4.4') { exclude group: 'com.intellij', module: 'annotations' } } @@ -90,7 +90,7 @@ dependencies { com.afollestad ason - 1.4.3 + 1.4.4 pom ``` diff --git a/build.gradle b/build.gradle index 20465c3..c27c44e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'com.afollestad' -version '1.4.3' +version '1.4.4' apply plugin: 'java' apply plugin: 'idea' @@ -34,7 +34,7 @@ publish { userOrg = 'drummer-aidan' groupId = 'com.afollestad' artifactId = 'ason' - publishVersion = '1.4.3' + publishVersion = '1.4.4' 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 b57437a..bf21da8 100644 --- a/src/main/java/com/afollestad/ason/Ason.java +++ b/src/main/java/com/afollestad/ason/Ason.java @@ -55,6 +55,10 @@ private Ason putInternal(JSONArray intoArray, try { if (value == null) { return this; + } else if (JSONObject.NULL.equals(value) + || JSONObject.NULL == value) { + json.put(key, JSONObject.NULL); + return this; } else if (isPrimitive(value) || value instanceof JSONObject || value instanceof JSONArray) { @@ -82,10 +86,14 @@ private Ason putInternal(JSONArray intoArray, return this; } + public Ason putNull(@NotNull String key) { + return put(key, JSONObject.NULL); + } + public Ason put(@NotNull String key, Object... values) { Object insertObject; if (values == null || values.length == 1) { - insertObject = values != null ? values[0] : null; + insertObject = values != null ? values[0] : JSONObject.NULL; } else { JSONArray newArray = new JSONArray(); for (Object value : values) { @@ -146,6 +154,9 @@ public Ason remove(@NotNull String key) { } if (result == null) { return defaultValue; + } else if (JSONObject.NULL.equals(result) + || JSONObject.NULL == result) { + return defaultValue; } else if (result instanceof JSONObject) { result = new Ason((JSONObject) result); } else if (result instanceof JSONArray) { @@ -278,7 +289,10 @@ public boolean equal(@NotNull String key, @Nullable Object value) { } public boolean isNull(String key) { - return get(key) == null; + Object value = get(key); + return value == null + || JSONObject.NULL.equals(value) + || JSONObject.NULL == value; } @Override public int hashCode() { diff --git a/src/test/java/com/afollestad/ason/AsonTest.java b/src/test/java/com/afollestad/ason/AsonTest.java index d900676..ccc8ea7 100644 --- a/src/test/java/com/afollestad/ason/AsonTest.java +++ b/src/test/java/com/afollestad/ason/AsonTest.java @@ -1,6 +1,8 @@ package com.afollestad.ason; +import org.json.JSONObject; import org.junit.Test; +import sun.text.resources.no.JavaTimeSupplementary_no; import java.lang.reflect.Field; import java.util.ArrayList; @@ -22,7 +24,7 @@ public class AsonTest { } } - @SuppressWarnings({"FieldCanBeLocal", "unused"}) private List listField; + @SuppressWarnings({"FieldCanBeLocal", "unused", "MismatchedQueryAndUpdateOfCollection"}) private List listField; @Test public void generic_list_type_test() throws Exception { listField = new ArrayList<>(0); @@ -30,6 +32,19 @@ public class AsonTest { assertEquals(Ason.class, listGenericType(field)); } + @Test public void json_null_test() { + Ason ason = new Ason() + .putNull("test") + .put("test2", null); + assertTrue(ason.isNull("test")); + assertTrue(ason.isNull("test2")); + assertEquals(null, ason.get("test")); + assertEquals(null, ason.get("test2")); + JSONObject stock = ason.toStockJson(); + assertEquals(JSONObject.NULL, stock.get("test")); + assertEquals(JSONObject.NULL, stock.get("test2")); + } + @Test public void test_is_number_true() { assertTrue(isNumber("1234")); assertTrue(isNumber("67891023231"));