diff --git a/src/main/java/com/github/underscore/lodash/U.java b/src/main/java/com/github/underscore/lodash/U.java index 308ef3b..3840f24 100644 --- a/src/main/java/com/github/underscore/lodash/U.java +++ b/src/main/java/com/github/underscore/lodash/U.java @@ -99,7 +99,8 @@ public enum Mode { REPLACE_EMPTY_VALUE_WITH_NULL, FORCE_ATTRIBUTE_USAGE, DEFINE_ROOT_NAME, - FORCE_ATTRIBUTE_USAGE_AND_DEFINE_ROOT_NAME + FORCE_ATTRIBUTE_USAGE_AND_DEFINE_ROOT_NAME, + REPLACE_NULL_WITH_EMPTY_VALUE } public U(final Iterable iterable) { @@ -2253,6 +2254,8 @@ public static String jsonToXml(String json, Xml.XmlStringBuilder.Step identStep, result = Xml.toXml((Map) object, identStep, newRootName); } else if (mode == Mode.FORCE_ATTRIBUTE_USAGE_AND_DEFINE_ROOT_NAME) { result = Xml.toXml(forceAttributeUsage((Map) object), identStep, newRootName); + } else if (mode == Mode.REPLACE_NULL_WITH_EMPTY_VALUE) { + result = Xml.toXml(replaceNullWithEmptyValue((Map) object), identStep, newRootName); } else { result = Xml.toXml((Map) object, identStep); } @@ -2496,6 +2499,33 @@ private static Object makeAttributeUsage(Object value) { return result; } + @SuppressWarnings("unchecked") + public static Map replaceNullWithEmptyValue(Map map) { + Map outMap = newLinkedHashMap(); + for (Map.Entry entry : map.entrySet()) { + outMap.put(entry.getKey(), entry.getValue() == null ? newLinkedHashMap() : + makeReplaceNullValue(entry.getValue())); + } + return outMap; + } + + @SuppressWarnings("unchecked") + private static Object makeReplaceNullValue(Object value) { + final Object result; + if (value instanceof List) { + List values = newArrayList(); + for (Object item : (List) value) { + values.add(item instanceof Map ? replaceNullWithEmptyValue((Map) item) : item); + } + result = values; + } else if (value instanceof Map) { + result = replaceNullWithEmptyValue((Map) value); + } else { + result = value; + } + return result; + } + public static long gcd(long value1, long value2) { if (value1 == 0) { return value2; diff --git a/src/test/java/com/github/underscore/lodash/LodashTest.java b/src/test/java/com/github/underscore/lodash/LodashTest.java index 22196a2..5966dba 100644 --- a/src/test/java/com/github/underscore/lodash/LodashTest.java +++ b/src/test/java/com/github/underscore/lodash/LodashTest.java @@ -762,6 +762,10 @@ public void formatXml() { U.formatXml("\n \n \n", Xml.XmlStringBuilder.Step.TABS)); assertEquals("1.00", U.formatXml("1.00")); assertEquals("2.01", U.formatXml("2.01")); + } + + @Test + public void forceAttributeUsage() { assertEquals("\n" + "\n" + " \n" @@ -793,6 +797,34 @@ public void formatXml() { U.forceAttributeUsage(map2); } + @Test + public void replaceNullWithEmptyValue() { + assertEquals("\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + "", U.jsonToXml("{\n" + + " \"RootElm\": {\n" + + " \"author\": {\n" + + " \"DOB\": null,\n" + + " \"value\": \"\"\n" + + " }\n" + + " }\n" + + "}", U.Mode.REPLACE_NULL_WITH_EMPTY_VALUE)); + Map map = U.newLinkedHashMap(); + List list = U.newArrayList(); + list.add(U.newLinkedHashMap()); + map.put("list", list); + U.replaceNullWithEmptyValue(map); + Map map2 = U.newLinkedHashMap(); + List list2 = U.newArrayList(); + list2.add(U.newArrayList()); + map2.put("list", list2); + U.replaceNullWithEmptyValue(map2); + } + @Test public void changeXmlEncoding() { assertEquals("\nTest",