Skip to content

Commit 3da4c80

Browse files
committed
Sync with underscore-java
1 parent 8baa95a commit 3da4c80

File tree

4 files changed

+102
-74
lines changed

4 files changed

+102
-74
lines changed

checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
-->
113113
<module name="AbbreviationAsWordInName">
114114
<property name="ignoreFinal" value="false"/>
115-
<property name="allowedAbbreviationLength" value="1"/>
115+
<property name="allowedAbbreviationLength" value="4"/>
116116
</module>
117117
<!--
118118
<module name="OverloadMethodsDeclarationOrder"/>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ private Xml() {}
7272
private static final String DOCTYPE_TEXT = "!DOCTYPE";
7373
private static final String ROOT = "root";
7474
private static final String DOCTYPE_HEADER = "<" + DOCTYPE_TEXT + " ";
75+
private static final Set<Character> SKIPPED_CHARS = Set.of(' ', '\n', '\r');
7576
private static final Map<String, String> XML_UNESCAPE = new HashMap<>();
7677
private static final org.w3c.dom.Document DOCUMENT = Document.createDocument();
7778

@@ -1533,7 +1534,7 @@ static Map<String, String> parseAttributes(final String source) {
15331534
equalFound = false;
15341535
}
15351536
quoteFound = !quoteFound;
1536-
} else if (quoteFound || source.charAt(index) == ' ') {
1537+
} else if (quoteFound || SKIPPED_CHARS.contains(source.charAt(index))) {
15371538
if (quoteFound) {
15381539
value.append(source.charAt(index));
15391540
}

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

Lines changed: 38 additions & 11 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"
@@ -954,6 +954,14 @@ void xmlToJson() {
954954
U.replaceEmptyValueWithEmptyString(map4);
955955
}
956956

957+
@Test
958+
void xmlToJson2() {
959+
assertEquals(
960+
"{\n \"debug\": \"&amp;\"\n}",
961+
U.xmlToJson(
962+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<debug>&amp;amp;</debug>"));
963+
}
964+
957965
@Test
958966
void xmpToJson3() {
959967
Map<String, Object> map2 = new LinkedHashMap<>();
@@ -988,11 +996,30 @@ void xmpToJson3() {
988996
}
989997

990998
@Test
991-
void xmlToJson2() {
999+
void xmpToJson4() {
9921000
assertEquals(
993-
"{\n" + " \"debug\": \"&amp;\"\n" + "}",
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+
+ "}",
9941014
U.xmlToJson(
995-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<debug>&amp;amp;</debug>"));
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>"));
9961023
}
9971024

9981025
@Test
@@ -1010,7 +1037,7 @@ void xmlOrJsonToJson() {
10101037
U.xmlOrJsonToJson(
10111038
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
10121039
+ "<root empty-array=\"true\"></root>"));
1013-
assertEquals("{\n" + " \"a\": 1\n" + "}", U.xmlOrJsonToJson("{\"a\":1}"));
1040+
assertEquals("{\n \"a\": 1\n}", U.xmlOrJsonToJson("{\"a\":1}"));
10141041
assertEquals("[\n]", U.xmlOrJsonToJson("[]"));
10151042
assertEquals("", U.xmlOrJsonToJson(""));
10161043
}
@@ -1025,7 +1052,7 @@ void xmlOrJsonToXml() {
10251052
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
10261053
+ "<root empty-array=\"true\"></root>"));
10271054
assertEquals(
1028-
"<?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>",
10291056
U.xmlOrJsonToXml("{\"a\":1}"));
10301057
assertEquals(
10311058
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
@@ -1074,7 +1101,7 @@ void renameMapKey() {
10741101
map.put("-self-closing", "false");
10751102
U.rename(map, "test", "test1");
10761103
Map<String, Object> newMap = U.rename(map, "-self-closing", "-self-closing1");
1077-
assertEquals("{\n" + " \"-self-closing1\": \"false\"\n" + "}", U.toJson(newMap));
1104+
assertEquals("{\n \"-self-closing1\": \"false\"\n}", U.toJson(newMap));
10781105
Map<String, Object> map2 = new LinkedHashMap<>();
10791106
List<Object> list = new ArrayList<>();
10801107
list.add(new ArrayList<Object>());
@@ -1225,7 +1252,7 @@ void updateMapKey() {
12251252
map.put("-self-closing", "false");
12261253
U.rename(map, "test", "test1");
12271254
Map<String, Object> newMap = U.update(map, map);
1228-
assertEquals("{\n" + " \"-self-closing\": \"false\"\n" + "}", U.toJson(newMap));
1255+
assertEquals("{\n \"-self-closing\": \"false\"\n}", U.toJson(newMap));
12291256
Map<String, Object> map2 = new LinkedHashMap<>();
12301257
List<Object> list = new ArrayList<>();
12311258
list.add(new ArrayList<Object>());
@@ -1247,7 +1274,7 @@ void setValue() {
12471274
map.put("-self-closing", "false");
12481275
U.setValue(map, "test", "test1");
12491276
Map<String, Object> newMap = U.setValue(map, "-self-closing", "true");
1250-
assertEquals("{\n" + " \"-self-closing\": \"true\"\n" + "}", U.toJson(newMap));
1277+
assertEquals("{\n \"-self-closing\": \"true\"\n}", U.toJson(newMap));
12511278
Map<String, Object> map2 = new LinkedHashMap<>();
12521279
List<Object> list = new ArrayList<>();
12531280
list.add(new ArrayList<Object>());
@@ -2142,7 +2169,7 @@ class Customer {
21422169
void issue306() {
21432170
String json =
21442171
U.objectBuilder().add("firstName", "John").add("lastName", (Object) null).toJson();
2145-
assertEquals("{\n \"firstName\": \"John\",\n" + " \"lastName\": null\n" + "}", json);
2172+
assertEquals("{\n \"firstName\": \"John\",\n \"lastName\": null\n}", json);
21462173
}
21472174

21482175
@Test

0 commit comments

Comments
 (0)