Skip to content

Commit

Permalink
Sync with underscore-java.
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev committed Apr 1, 2021
1 parent d42967c commit 8b952f7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/main/java/com/github/underscore/lodash/U.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> iterable) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -2496,6 +2499,33 @@ private static Object makeAttributeUsage(Object value) {
return result;
}

@SuppressWarnings("unchecked")
public static Map<String, Object> replaceNullWithEmptyValue(Map<String, Object> map) {
Map<String, Object> outMap = newLinkedHashMap();
for (Map.Entry<String, Object> 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<Object> 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;
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/com/github/underscore/lodash/LodashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,10 @@ public void formatXml() {
U.formatXml("<a>\n <b></b>\n <b></b>\n</a>", Xml.XmlStringBuilder.Step.TABS));
assertEquals("<a number=\"true\">1.00</a>", U.formatXml("<a number=\"true\">1.00</a>"));
assertEquals("<a number=\"true\">2.01</a>", U.formatXml("<a number=\"true\">2.01</a>"));
}

@Test
public void forceAttributeUsage() {
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<RootElm>\n"
+ " <author DOB=\"\" EMailID=\"\" PlaceId=\"\" SSN=\"\">\n"
Expand Down Expand Up @@ -793,6 +797,34 @@ public void formatXml() {
U.forceAttributeUsage(map2);
}

@Test
public void replaceNullWithEmptyValue() {
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<RootElm>\n"
+ " <author>\n"
+ " <DOB></DOB>\n"
+ " <value string=\"true\"/>\n"
+ " </author>\n"
+ "</RootElm>", U.jsonToXml("{\n"
+ " \"RootElm\": {\n"
+ " \"author\": {\n"
+ " \"DOB\": null,\n"
+ " \"value\": \"\"\n"
+ " }\n"
+ " }\n"
+ "}", U.Mode.REPLACE_NULL_WITH_EMPTY_VALUE));
Map<String, Object> map = U.newLinkedHashMap();
List<Object> list = U.newArrayList();
list.add(U.newLinkedHashMap());
map.put("list", list);
U.replaceNullWithEmptyValue(map);
Map<String, Object> map2 = U.newLinkedHashMap();
List<Object> list2 = U.newArrayList();
list2.add(U.newArrayList());
map2.put("list", list2);
U.replaceNullWithEmptyValue(map2);
}

@Test
public void changeXmlEncoding() {
assertEquals("<?xml version=\"1.0\" encoding=\"windows-1251\"?>\n<a>Test</a>",
Expand Down

0 comments on commit 8b952f7

Please sign in to comment.