Skip to content

Commit cd43705

Browse files
authored
Add new mode REPLACE_EMPTY_VALUE_WITH_NULL to the U.xmlToJson() method.
1 parent 2987dd3 commit cd43705

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/main/java/com/github/underscore/lodash/U.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public class U<T> extends com.github.underscore.U<T> {
9595

9696
public enum Mode {
9797
REPLACE_SELF_CLOSING_WITH_NULL,
98-
REPLACE_SELF_CLOSING_WITH_EMPTY
98+
REPLACE_SELF_CLOSING_WITH_EMPTY,
99+
REPLACE_EMPTY_VALUE_WITH_NULL
99100
}
100101

101102
public U(final Iterable<T> iterable) {
@@ -2267,6 +2268,8 @@ public static String xmlToJson(String xml, Json.JsonStringBuilder.Step identStep
22672268
result = Json.toJson(replaceSelfClosingWithNull((Map) object), identStep);
22682269
} else if (mode == Mode.REPLACE_SELF_CLOSING_WITH_EMPTY) {
22692270
result = Json.toJson(replaceSelfClosingWithEmpty((Map) object), identStep);
2271+
} else if (mode == Mode.REPLACE_EMPTY_VALUE_WITH_NULL) {
2272+
result = Json.toJson(replaceEmptyValueWithNull((Map) object), identStep);
22702273
} else {
22712274
result = Json.toJson((Map) object, identStep);
22722275
}
@@ -2416,6 +2419,36 @@ private static Object makeObjectSelfClose(Object value, String newValue) {
24162419
return result;
24172420
}
24182421

2422+
@SuppressWarnings("unchecked")
2423+
public static Map<String, Object> replaceEmptyValueWithNull(Map<String, Object> map) {
2424+
if (map.isEmpty()) {
2425+
return null;
2426+
}
2427+
Map<String, Object> outMap = newLinkedHashMap();
2428+
for (Map.Entry<String, Object> entry : map.entrySet()) {
2429+
outMap.put(String.valueOf(entry.getKey()),
2430+
makeObjectEmptyValue(entry.getValue()));
2431+
}
2432+
return outMap;
2433+
}
2434+
2435+
@SuppressWarnings("unchecked")
2436+
private static Object makeObjectEmptyValue(Object value) {
2437+
final Object result;
2438+
if (value instanceof List) {
2439+
List<Object> values = newArrayList();
2440+
for (Object item : (List) value) {
2441+
values.add(item instanceof Map ? replaceEmptyValueWithNull((Map) item) : item);
2442+
}
2443+
result = values;
2444+
} else if (value instanceof Map) {
2445+
result = replaceEmptyValueWithNull((Map) value);
2446+
} else {
2447+
result = value;
2448+
}
2449+
return result;
2450+
}
2451+
24192452
public static long gcd(long value1, long value2) {
24202453
if (value1 == 0) {
24212454
return value2;

src/test/java/com/github/underscore/lodash/LodashTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,21 @@ public void xmlToJson() {
602602
list.add(U.newArrayList());
603603
map2.put("list", list);
604604
U.replaceSelfClosingWithNull(map2);
605+
assertEquals("{\n"
606+
+ " \"a\": {\n"
607+
+ " \"b\": [\n"
608+
+ " null,\n"
609+
+ " null\n"
610+
+ " ]\n"
611+
+ " },\n"
612+
+ " \"#omit-xml-declaration\": \"yes\"\n"
613+
+ "}",
614+
U.xmlToJson("<a><b></b><b></b></a>", U.Mode.REPLACE_EMPTY_VALUE_WITH_NULL));
615+
Map<String, Object> map3 = U.newLinkedHashMap();
616+
List<Object> list2 = U.newArrayList();
617+
list2.add(U.newArrayList());
618+
map3.put("list", list2);
619+
U.replaceEmptyValueWithNull(map3);
605620
}
606621

607622
@Test

0 commit comments

Comments
 (0)