Skip to content

Commit 9472ad0

Browse files
committed
Improve unescape xml.
1 parent 99e2499 commit 9472ad0

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ public static String toXml(Map map) {
996996
}
997997

998998
@SuppressWarnings("unchecked")
999-
private static Object getValue(final Object value, final FromType fromType) {
999+
private static Object getValue(final String name, final Object value, final FromType fromType) {
10001000
final Object localValue;
10011001
if (value instanceof Map && ((Map<String, Object>) value).entrySet().size() == 1) {
10021002
final Map.Entry<String, Object> entry = ((Map<String, Object>) value).entrySet().iterator().next();
@@ -1009,7 +1009,8 @@ private static Object getValue(final Object value, final FromType fromType) {
10091009
} else {
10101010
localValue = value;
10111011
}
1012-
return localValue instanceof String ? XmlValue.unescape((String) localValue) : localValue;
1012+
return localValue instanceof String && name.startsWith("-")
1013+
? XmlValue.unescape((String) localValue) : localValue;
10131014
}
10141015

10151016
public static Object stringToNumber(String number) {
@@ -1105,9 +1106,9 @@ private static Object checkArray(final Map<String, Object> map, final String nam
11051106
localMap4.remove(ARRAY);
11061107
localMap4.remove(SELF_CLOSING);
11071108
object = name.equals(XmlValue.getMapKey(localMap4))
1108-
? U.newArrayList(Collections.singletonList(getValue(XmlValue.getMapValue(localMap4),
1109+
? U.newArrayList(Collections.singletonList(getValue(name, XmlValue.getMapValue(localMap4),
11091110
FromType.FOR_CONVERT)))
1110-
: U.newArrayList(Collections.singletonList(getValue(localMap4, FromType.FOR_CONVERT)));
1111+
: U.newArrayList(Collections.singletonList(getValue(name, localMap4, FromType.FOR_CONVERT)));
11111112
} else {
11121113
object = localMap;
11131114
}
@@ -1256,13 +1257,13 @@ private static void addNodeValue(final Map<String, Object> map, final String nam
12561257
final String elementName = unescapeName(elementMapper.apply(name, namespaces));
12571258
if (map.containsKey(elementName)) {
12581259
if (TEXT.equals(elementName)) {
1259-
map.put(elementName + uniqueIds[0], nodeMapper.apply(getValue(value, fromType)));
1260+
map.put(elementName + uniqueIds[0], nodeMapper.apply(getValue(name, value, fromType)));
12601261
uniqueIds[0] += 1;
12611262
} else if (COMMENT.equals(elementName)) {
1262-
map.put(elementName + uniqueIds[1], nodeMapper.apply(getValue(value, fromType)));
1263+
map.put(elementName + uniqueIds[1], nodeMapper.apply(getValue(name, value, fromType)));
12631264
uniqueIds[1] += 1;
12641265
} else if (CDATA.equals(elementName)) {
1265-
map.put(elementName + uniqueIds[2], nodeMapper.apply(getValue(value, fromType)));
1266+
map.put(elementName + uniqueIds[2], nodeMapper.apply(getValue(name, value, fromType)));
12661267
uniqueIds[2] += 1;
12671268
} else {
12681269
final Object object = map.get(elementName);
@@ -1277,7 +1278,7 @@ private static void addNodeValue(final Map<String, Object> map, final String nam
12771278
}
12781279
} else {
12791280
if (elementName != null) {
1280-
map.put(elementName, nodeMapper.apply(getValue(value, fromType)));
1281+
map.put(elementName, nodeMapper.apply(getValue(name, value, fromType)));
12811282
}
12821283
}
12831284
}
@@ -1298,7 +1299,7 @@ private static void addText(final Map<String, Object> map, final String name, fi
12981299
objects.add(index, item);
12991300
lastIndex -= 1;
13001301
}
1301-
final Object newValue = getValue(value, fromType);
1302+
final Object newValue = getValue(name, value, fromType);
13021303
if (newValue instanceof List) {
13031304
objects.add(((List) newValue).get(0));
13041305
} else {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,13 @@ public void xmlToJson() {
620620
U.replaceEmptyValueWithNull(map3);
621621
}
622622

623+
@Test
624+
public void xmlToJson2() {
625+
assertEquals("{\n"
626+
+ " \"debug\": \"&amp;\"\n"
627+
+ "}", U.xmlToJson("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<debug>&amp;amp;</debug>"));
628+
}
629+
623630
@Test
624631
public void removeMapKey() {
625632
Map<String, Object> map = U.newLinkedHashMap();

0 commit comments

Comments
 (0)