Skip to content

Commit 896c70e

Browse files
authored
Synchronize with underscore-java
1 parent 5083967 commit 896c70e

File tree

6 files changed

+180
-78
lines changed

6 files changed

+180
-78
lines changed

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

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ public enum XmlToJsonMode {
140140
REPLACE_EMPTY_TAG_WITH_NULL,
141141
REPLACE_EMPTY_TAG_WITH_STRING,
142142
REMOVE_FIRST_LEVEL,
143-
WITHOUT_NAMESPACES
143+
WITHOUT_NAMESPACES,
144+
REPLACE_MINUS_WITH_AT,
145+
REPLACE_EMPTY_TAG_WITH_NULL_AND_MINUS_WITH_AT
144146
}
145147

146148
public enum JsonToXmlMode {
@@ -2698,6 +2700,15 @@ public static String xmlToJson(
26982700
result = Json.toJson(replaceSelfClosingWithEmpty((Map) object), identStep);
26992701
} else if (mode == XmlToJsonMode.REPLACE_EMPTY_VALUE_WITH_NULL) {
27002702
result = Json.toJson(replaceEmptyValueWithNull((Map) object), identStep);
2703+
} else if (mode == XmlToJsonMode.REPLACE_MINUS_WITH_AT) {
2704+
result = Json.toJson(replaceMinusWithAt((Map) object), identStep);
2705+
} else if (mode == XmlToJsonMode.REPLACE_EMPTY_TAG_WITH_NULL_AND_MINUS_WITH_AT) {
2706+
result =
2707+
Json.toJson(
2708+
replaceMinusWithAt(
2709+
replaceEmptyValueWithNull(
2710+
replaceSelfClosingWithNull((Map) object))),
2711+
identStep);
27012712
} else if (mode == XmlToJsonMode.REPLACE_EMPTY_TAG_WITH_NULL) {
27022713
result =
27032714
Json.toJson(
@@ -2974,6 +2985,38 @@ private static Object makeObjectSelfClose(Object value, String newValue) {
29742985
return result;
29752986
}
29762987

2988+
public static Map<String, Object> replaceMinusWithAt(Map<String, Object> map) {
2989+
if (map == null) {
2990+
return null;
2991+
}
2992+
Map<String, Object> outMap = new LinkedHashMap<>();
2993+
for (Map.Entry<String, Object> entry : map.entrySet()) {
2994+
outMap.put(
2995+
String.valueOf(entry.getKey()).startsWith("-")
2996+
? "@" + String.valueOf(entry.getKey()).substring(1)
2997+
: String.valueOf(entry.getKey()),
2998+
replaceMinusWithAtValue(entry.getValue()));
2999+
}
3000+
return outMap;
3001+
}
3002+
3003+
@SuppressWarnings("unchecked")
3004+
private static Object replaceMinusWithAtValue(Object value) {
3005+
final Object result;
3006+
if (value instanceof List) {
3007+
List<Object> values = new ArrayList<>();
3008+
for (Object item : (List) value) {
3009+
values.add(item instanceof Map ? replaceMinusWithAt((Map) item) : item);
3010+
}
3011+
result = values;
3012+
} else if (value instanceof Map) {
3013+
result = replaceMinusWithAt((Map) value);
3014+
} else {
3015+
result = value;
3016+
}
3017+
return result;
3018+
}
3019+
29773020
public static Map<String, Object> replaceEmptyValueWithNull(Map<String, Object> map) {
29783021
if (map == null || map.isEmpty()) {
29793022
return null;
@@ -3499,15 +3542,15 @@ public static Map<String, Object> propertiesToMap(Properties properties) {
34993542
if (properties != null && !properties.isEmpty()) {
35003543
Enumeration<?> enumProperties = properties.propertyNames();
35013544
while (enumProperties.hasMoreElements()) {
3502-
String name = (String)enumProperties.nextElement();
3545+
String name = (String) enumProperties.nextElement();
35033546
map.put(name, properties.getProperty(name));
35043547
}
35053548
}
35063549
return map;
35073550
}
35083551

35093552
public static Properties mapToProperties(Map<String, Object> map) {
3510-
Properties properties = new Properties();
3553+
Properties properties = new Properties();
35113554
if (map != null) {
35123555
for (final Map.Entry<String, Object> entry : map.entrySet()) {
35133556
if (!isNull(entry.getValue())) {

src/main/java/com/github/underscore/Xml.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ object Xml {
7575
private const val DOCTYPE_TEXT = "!DOCTYPE"
7676
private const val ROOT = "root"
7777
private const val DOCTYPE_HEADER = "<$DOCTYPE_TEXT "
78+
private val SKIPPED_CHARS = setOf(' ', '\n', '\r')
7879
private val XML_UNESCAPE: MutableMap<String, String> = HashMap()
7980
private val DOCUMENT = Document.createDocument()
8081

@@ -502,7 +503,7 @@ object Xml {
502503
equalFound = false
503504
}
504505
quoteFound = !quoteFound
505-
} else if (quoteFound || source[index] == ' ') {
506+
} else if (quoteFound || SKIPPED_CHARS.contains(source[index])) {
506507
if (quoteFound) {
507508
value.append(source[index])
508509
}

src/test/java/com/github/underscore/ArraysTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,7 @@ void interpose() {
384384
assertEquals("[a, b]", new Underscore<>(singletonList("a, b")).interpose(null).toString());
385385
assertEquals("[a]", Underscore.chain(singletonList("a")).interpose("interpose").toString());
386386
assertEquals(
387-
"[]",
388-
Underscore.chain(new ArrayList<Object>()).interpose("interpose").toString());
387+
"[]", Underscore.chain(new ArrayList<Object>()).interpose("interpose").toString());
389388
assertEquals(
390389
"[a, b, c]", Underscore.chain(asList("a", "b", "c")).interpose(null).toString());
391390
assertEquals(

src/test/java/com/github/underscore/CollectionsTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,8 +1688,7 @@ void toCardinalityMap() {
16881688
assertEquals(
16891689
"{a=2, b=1, c=2}",
16901690
new Underscore<>(asList("a", "a", "b", "c", "c")).toCardinalityMap().toString());
1691-
assertEquals(
1692-
"{}", new Underscore<>(new ArrayList<Object>()).toCardinalityMap().toString());
1691+
assertEquals("{}", new Underscore<>(new ArrayList<Object>()).toCardinalityMap().toString());
16931692
}
16941693

16951694
/*

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

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ void xmlToJson() {
871871
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
872872
+ "<root empty-array=\"true\"></root>"));
873873
assertEquals(
874-
"{\n" + " \"a\": null,\n" + " \"#omit-xml-declaration\": \"yes\"\n" + "}",
874+
"{\n \"a\": null,\n \"#omit-xml-declaration\": \"yes\"\n}",
875875
U.xmlToJson("<a/>", U.XmlToJsonMode.REPLACE_SELF_CLOSING_WITH_NULL));
876876
assertEquals(
877877
"{\n"
@@ -897,7 +897,7 @@ void xmlToJson() {
897897
"<c><b></b><b></b><a/></c>",
898898
U.XmlToJsonMode.REPLACE_EMPTY_TAG_WITH_STRING));
899899
assertEquals(
900-
"{\n" + " \"a\": \"\",\n" + " \"#omit-xml-declaration\": \"yes\"\n" + "}",
900+
"{\n \"a\": \"\",\n \"#omit-xml-declaration\": \"yes\"\n}",
901901
U.xmlToJson("<a/>", U.XmlToJsonMode.REPLACE_SELF_CLOSING_WITH_STRING));
902902
assertEquals(
903903
"{\n"
@@ -957,11 +957,71 @@ void xmlToJson() {
957957
@Test
958958
void xmlToJson2() {
959959
assertEquals(
960-
"{\n" + " \"debug\": \"&amp;\"\n" + "}",
960+
"{\n \"debug\": \"&amp;\"\n}",
961961
U.xmlToJson(
962962
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<debug>&amp;amp;</debug>"));
963963
}
964964

965+
@Test
966+
void xmpToJson3() {
967+
Map<String, Object> map2 = new LinkedHashMap<>();
968+
List<Object> list = new ArrayList<>();
969+
list.add(new ArrayList<Object>());
970+
map2.put("list", list);
971+
U.replaceMinusWithAt(map2);
972+
assertEquals(
973+
"{\n"
974+
+ " \"a\": {\n"
975+
+ " \"@c\": \"1\",\n"
976+
+ " \"b\": [\n"
977+
+ " {\n"
978+
+ " },\n"
979+
+ " {\n"
980+
+ " }\n"
981+
+ " ]\n"
982+
+ " },\n"
983+
+ " \"#omit-xml-declaration\": \"yes\"\n"
984+
+ "}",
985+
U.xmlToJson(
986+
"<a c=\"1\"><b></b><b></b></a>", U.XmlToJsonMode.REPLACE_MINUS_WITH_AT));
987+
Map<String, Object> map3 = new LinkedHashMap<>();
988+
List<Object> list2 = new ArrayList<>();
989+
list2.add(new ArrayList<Object>());
990+
map3.put("list", list2);
991+
U.replaceMinusWithAt(map3);
992+
U.replaceMinusWithAt(null);
993+
U.xmlToJson(
994+
"<a c=\"1\"><b></b><b></b></a>",
995+
U.XmlToJsonMode.REPLACE_EMPTY_TAG_WITH_NULL_AND_MINUS_WITH_AT);
996+
}
997+
998+
@Test
999+
void xmpToJson4() {
1000+
assertEquals(
1001+
"{\n"
1002+
+ " \"z:catalog\": {\n"
1003+
+ " \"-xmlns:xsi\": \"http://www.w3.org/2001/XMLSchema-instance\",\n"
1004+
+ " \"-xmlns:z\": \"www.microsoft.com/zzz\",\n"
1005+
+ " \"book\": {\n"
1006+
+ " \"-xsi:noNamespaceSchemaLocation\": \"http://www.example.com/MyData.xsd\",\n"
1007+
+ " \"-id\": \"bk101\",\n"
1008+
+ " \"title\": \"Presenting XML\",\n"
1009+
+ " \"author\": \"Richard Light\"\n"
1010+
+ " }\n"
1011+
+ " },\n"
1012+
+ " \"#omit-xml-declaration\": \"yes\"\n"
1013+
+ "}",
1014+
U.xmlToJson(
1015+
"<z:catalog xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
1016+
+ "xmlns:z=\"www.microsoft.com/zzz\">\n"
1017+
+ " <book xsi:noNamespaceSchemaLocation=\"http://www.example.com/MyData.xsd\"\r\n"
1018+
+ " id=\"bk101\">\n"
1019+
+ " <title>Presenting XML</title>\n"
1020+
+ " <author>Richard Light</author>\n"
1021+
+ " </book>\n"
1022+
+ "</z:catalog>"));
1023+
}
1024+
9651025
@Test
9661026
void xmlOrJsonToJson() {
9671027
assertEquals(
@@ -977,7 +1037,7 @@ void xmlOrJsonToJson() {
9771037
U.xmlOrJsonToJson(
9781038
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
9791039
+ "<root empty-array=\"true\"></root>"));
980-
assertEquals("{\n" + " \"a\": 1\n" + "}", U.xmlOrJsonToJson("{\"a\":1}"));
1040+
assertEquals("{\n \"a\": 1\n}", U.xmlOrJsonToJson("{\"a\":1}"));
9811041
assertEquals("[\n]", U.xmlOrJsonToJson("[]"));
9821042
assertEquals("", U.xmlOrJsonToJson(""));
9831043
}
@@ -992,7 +1052,7 @@ void xmlOrJsonToXml() {
9921052
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
9931053
+ "<root empty-array=\"true\"></root>"));
9941054
assertEquals(
995-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<a number=\"true\">1</a>",
1055+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<a number=\"true\">1</a>",
9961056
U.xmlOrJsonToXml("{\"a\":1}"));
9971057
assertEquals(
9981058
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
@@ -1041,7 +1101,7 @@ void renameMapKey() {
10411101
map.put("-self-closing", "false");
10421102
U.rename(map, "test", "test1");
10431103
Map<String, Object> newMap = U.rename(map, "-self-closing", "-self-closing1");
1044-
assertEquals("{\n" + " \"-self-closing1\": \"false\"\n" + "}", U.toJson(newMap));
1104+
assertEquals("{\n \"-self-closing1\": \"false\"\n}", U.toJson(newMap));
10451105
Map<String, Object> map2 = new LinkedHashMap<>();
10461106
List<Object> list = new ArrayList<>();
10471107
list.add(new ArrayList<Object>());
@@ -1192,7 +1252,7 @@ void updateMapKey() {
11921252
map.put("-self-closing", "false");
11931253
U.rename(map, "test", "test1");
11941254
Map<String, Object> newMap = U.update(map, map);
1195-
assertEquals("{\n" + " \"-self-closing\": \"false\"\n" + "}", U.toJson(newMap));
1255+
assertEquals("{\n \"-self-closing\": \"false\"\n}", U.toJson(newMap));
11961256
Map<String, Object> map2 = new LinkedHashMap<>();
11971257
List<Object> list = new ArrayList<>();
11981258
list.add(new ArrayList<Object>());
@@ -1214,7 +1274,7 @@ void setValue() {
12141274
map.put("-self-closing", "false");
12151275
U.setValue(map, "test", "test1");
12161276
Map<String, Object> newMap = U.setValue(map, "-self-closing", "true");
1217-
assertEquals("{\n" + " \"-self-closing\": \"true\"\n" + "}", U.toJson(newMap));
1277+
assertEquals("{\n \"-self-closing\": \"true\"\n}", U.toJson(newMap));
12181278
Map<String, Object> map2 = new LinkedHashMap<>();
12191279
List<Object> list = new ArrayList<>();
12201280
list.add(new ArrayList<Object>());
@@ -2109,7 +2169,7 @@ class Customer {
21092169
void issue306() {
21102170
String json =
21112171
U.objectBuilder().add("firstName", "John").add("lastName", (Object) null).toJson();
2112-
assertEquals("{\n \"firstName\": \"John\",\n" + " \"lastName\": null\n" + "}", json);
2172+
assertEquals("{\n \"firstName\": \"John\",\n \"lastName\": null\n}", json);
21132173
}
21142174

21152175
@Test

0 commit comments

Comments
 (0)