Skip to content

Commit

Permalink
Sync with underscore-java
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev committed Feb 15, 2024
1 parent 8c98771 commit 8baa95a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/github/underscore/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ private static void escape(String s, StringBuilder sb) {
case '\t':
sb.append("\\t");
break;
case '':
sb.append('');
case '\u20AC':
sb.append('\u20AC');
break;
default:
if (ch <= '\u001F'
Expand Down
45 changes: 44 additions & 1 deletion src/main/java/com/github/underscore/U.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ public enum XmlToJsonMode {
REPLACE_EMPTY_TAG_WITH_NULL,
REPLACE_EMPTY_TAG_WITH_STRING,
REMOVE_FIRST_LEVEL,
WITHOUT_NAMESPACES
WITHOUT_NAMESPACES,
REPLACE_MINUS_WITH_AT,
REPLACE_EMPTY_TAG_WITH_NULL_AND_MINUS_WITH_AT
}

public enum JsonToXmlMode {
Expand Down Expand Up @@ -2698,6 +2700,15 @@ public static String xmlToJson(
result = Json.toJson(replaceSelfClosingWithEmpty((Map) object), identStep);
} else if (mode == XmlToJsonMode.REPLACE_EMPTY_VALUE_WITH_NULL) {
result = Json.toJson(replaceEmptyValueWithNull((Map) object), identStep);
} else if (mode == XmlToJsonMode.REPLACE_MINUS_WITH_AT) {
result = Json.toJson(replaceMinusWithAt((Map) object), identStep);
} else if (mode == XmlToJsonMode.REPLACE_EMPTY_TAG_WITH_NULL_AND_MINUS_WITH_AT) {
result =
Json.toJson(
replaceMinusWithAt(
replaceEmptyValueWithNull(
replaceSelfClosingWithNull((Map) object))),
identStep);
} else if (mode == XmlToJsonMode.REPLACE_EMPTY_TAG_WITH_NULL) {
result =
Json.toJson(
Expand Down Expand Up @@ -2974,6 +2985,38 @@ private static Object makeObjectSelfClose(Object value, String newValue) {
return result;
}

public static Map<String, Object> replaceMinusWithAt(Map<String, Object> map) {
if (map == null) {
return null;
}
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
outMap.put(
String.valueOf(entry.getKey()).startsWith("-")
? "@" + String.valueOf(entry.getKey()).substring(1)
: String.valueOf(entry.getKey()),
replaceMinusWithAtValue(entry.getValue()));
}
return outMap;
}

@SuppressWarnings("unchecked")
private static Object replaceMinusWithAtValue(Object value) {
final Object result;
if (value instanceof List) {
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
values.add(item instanceof Map ? replaceMinusWithAt((Map) item) : item);
}
result = values;
} else if (value instanceof Map) {
result = replaceMinusWithAt((Map) value);
} else {
result = value;
}
return result;
}

public static Map<String, Object> replaceEmptyValueWithNull(Map<String, Object> map) {
if (map == null || map.isEmpty()) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/underscore/Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ private static void escape(String s, StringBuilder sb) {
case '\t':
sb.append("\t");
break;
case '':
case '\u20AC':
sb.append("€");
break;
default:
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/com/github/underscore/LodashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,39 @@ void xmlToJson() {
U.replaceEmptyValueWithEmptyString(map4);
}

@Test
void xmpToJson3() {
Map<String, Object> map2 = new LinkedHashMap<>();
List<Object> list = new ArrayList<>();
list.add(new ArrayList<Object>());
map2.put("list", list);
U.replaceMinusWithAt(map2);
assertEquals(
"{\n"
+ " \"a\": {\n"
+ " \"@c\": \"1\",\n"
+ " \"b\": [\n"
+ " {\n"
+ " },\n"
+ " {\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " \"#omit-xml-declaration\": \"yes\"\n"
+ "}",
U.xmlToJson(
"<a c=\"1\"><b></b><b></b></a>", U.XmlToJsonMode.REPLACE_MINUS_WITH_AT));
Map<String, Object> map3 = new LinkedHashMap<>();
List<Object> list2 = new ArrayList<>();
list2.add(new ArrayList<Object>());
map3.put("list", list2);
U.replaceMinusWithAt(map3);
U.replaceMinusWithAt(null);
U.xmlToJson(
"<a c=\"1\"><b></b><b></b></a>",
U.XmlToJsonMode.REPLACE_EMPTY_TAG_WITH_NULL_AND_MINUS_WITH_AT);
}

@Test
void xmlToJson2() {
assertEquals(
Expand Down

0 comments on commit 8baa95a

Please sign in to comment.