Skip to content

Commit 2f6b387

Browse files
authored
Added method U.replaceNilWithNull(map).
1 parent 55f2a9e commit 2f6b387

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public class U<T> extends Underscore<T> {
6767
private static String upper = "[A-Z\\xc0-\\xd6\\xd8-\\xde\\u0400-\\u04FF]";
6868
private static String lower = "[a-z\\xdf-\\xf6\\xf8-\\xff]+";
6969
private static String selfClosing = "-self-closing";
70+
private static String nilKey = "-nil";
71+
private static String nilKeyNs = ":nil";
7072
private static java.util.regex.Pattern reWords =
7173
java.util.regex.Pattern.compile(
7274
upper
@@ -2847,6 +2849,40 @@ private static Object makeReplaceFirstLevel(Object value, int level) {
28472849
return result;
28482850
}
28492851

2852+
public static Map<String, Object> replaceNilWithNull(Map<String, Object> map) {
2853+
Map<String, Object> outMap = newLinkedHashMap();
2854+
for (Map.Entry<String, Object> entry : map.entrySet()) {
2855+
Object outValue = makeReplaceNilWithNull(entry.getValue());
2856+
if (outValue instanceof Map
2857+
&& (nilKey.equals(Xml.XmlValue.getMapKey(outValue))
2858+
|| Xml.XmlValue.getMapKey(outValue).endsWith(nilKeyNs))
2859+
&& "true".equals(Xml.XmlValue.getMapValue(outValue))
2860+
&& ((Map) outValue).containsKey(selfClosing)
2861+
&& "true".equals(((Map) outValue).get(selfClosing))) {
2862+
outValue = null;
2863+
}
2864+
outMap.put(entry.getKey(), outValue);
2865+
}
2866+
return outMap;
2867+
}
2868+
2869+
@SuppressWarnings("unchecked")
2870+
private static Object makeReplaceNilWithNull(Object value) {
2871+
final Object result;
2872+
if (value instanceof List) {
2873+
List<Object> values = newArrayList();
2874+
for (Object item : (List) value) {
2875+
values.add(item instanceof Map ? replaceNilWithNull((Map) item) : item);
2876+
}
2877+
result = values;
2878+
} else if (value instanceof Map) {
2879+
result = replaceNilWithNull((Map) value);
2880+
} else {
2881+
result = value;
2882+
}
2883+
return result;
2884+
}
2885+
28502886
public static Map<String, Object> deepCopyMap(Map<String, Object> map) {
28512887
Map<String, Object> outMap = newLinkedHashMap();
28522888
for (Map.Entry<String, Object> entry : map.entrySet()) {

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

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,7 @@ public void formatXml() {
908908
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root><element>1</element>"
909909
+ "<element>2</element></root>"));
910910
assertEquals(
911-
"<a>\n <b></b>\n <b></b>\n</a>",
912-
U.formatXml("<a>\n <b></b>\n <b></b>\n</a>"));
911+
"<a>\n <b></b>\n <b></b>\n</a>", U.formatXml("<a>\n <b></b>\n <b></b>\n</a>"));
913912
assertEquals(
914913
"<a>\n <b></b>\n <b></b>\n</a>",
915914
U.formatXml(
@@ -998,6 +997,66 @@ public void replaceNullWithEmptyValue() {
998997
U.replaceNullWithEmptyValue(map2);
999998
}
1000999

1000+
@Test
1001+
public void replaceNilWithNull() {
1002+
assertEquals(
1003+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1004+
+ "<RootElm>\n"
1005+
+ " <author>\n"
1006+
+ " <DOB nil=\"true\"></DOB>\n"
1007+
+ " <value null=\"true\"/>\n"
1008+
+ " </author>\n"
1009+
+ "</RootElm>",
1010+
U.toXml(
1011+
U.replaceNilWithNull(
1012+
U.fromXmlMap(
1013+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1014+
+ "<RootElm>\n"
1015+
+ " <author>\n"
1016+
+ " <DOB nil=\"true\"></DOB>\n"
1017+
+ " <value nil=\"true\"/>\n"
1018+
+ " </author>\n"
1019+
+ "</RootElm>"))));
1020+
assertEquals(
1021+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1022+
+ "<RootElm xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
1023+
+ " <author>\n"
1024+
+ " <DOB xsi:nil=\"true\"></DOB>\n"
1025+
+ " <value null=\"true\"/>\n"
1026+
+ " <DOB2 xsi:nil=\"true1\"></DOB2>\n"
1027+
+ " <value2 xsi:nil=\"true1\"/>\n"
1028+
+ " </author>\n"
1029+
+ "</RootElm>",
1030+
U.toXml(
1031+
U.replaceNilWithNull(
1032+
U.fromXmlMap(
1033+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1034+
+ "<RootElm xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
1035+
+ " <author>\n"
1036+
+ " <DOB xsi:nil=\"true\"></DOB>\n"
1037+
+ " <value xsi:nil=\"true\"/>\n"
1038+
+ " <DOB2 xsi:nil=\"true1\"></DOB2>\n"
1039+
+ " <value2 xsi:nil=\"true1\"/>\n"
1040+
+ " </author>\n"
1041+
+ "</RootElm>"))));
1042+
Map<String, Object> map = U.newLinkedHashMap();
1043+
List<Object> list = U.newArrayList();
1044+
list.add(U.newLinkedHashMap());
1045+
map.put("list", list);
1046+
U.replaceNilWithNull(map);
1047+
Map<String, Object> map2 = U.newLinkedHashMap();
1048+
List<Object> list2 = U.newArrayList();
1049+
list2.add(U.newArrayList());
1050+
map2.put("list", list2);
1051+
U.replaceNilWithNull(map2);
1052+
Map<String, Object> map3 = U.newLinkedHashMap();
1053+
map3.put("-nil", "true");
1054+
map3.put("-self-closing", "true1");
1055+
Map<String, Object> map4 = U.newLinkedHashMap();
1056+
map4.put("map", map3);
1057+
U.replaceNilWithNull(map4);
1058+
}
1059+
10011060
@Test
10021061
public void replaceEmptyStringWithEmptyValue() {
10031062
assertEquals(

0 commit comments

Comments
 (0)