Skip to content

Commit

Permalink
Added new mode XmlToJsonMode.REPLACE_MINUS_WITH_AT
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Feb 8, 2024
1 parent db72394 commit d2ecc79
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
-->
<module name="AbbreviationAsWordInName">
<property name="ignoreFinal" value="false"/>
<property name="allowedAbbreviationLength" value="1"/>
<property name="allowedAbbreviationLength" value="4"/>
</module>
<!--
<module name="OverloadMethodsDeclarationOrder"/>
Expand Down
34 changes: 33 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,8 @@ public enum XmlToJsonMode {
REPLACE_EMPTY_TAG_WITH_NULL,
REPLACE_EMPTY_TAG_WITH_STRING,
REMOVE_FIRST_LEVEL,
WITHOUT_NAMESPACES
WITHOUT_NAMESPACES,
REPLACE_MINUS_WITH_AT
}

public enum JsonToXmlMode {
Expand Down Expand Up @@ -2698,6 +2699,8 @@ 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) {
result =
Json.toJson(
Expand Down Expand Up @@ -2974,6 +2977,35 @@ private static Object makeObjectSelfClose(Object value, String newValue) {
return result;
}

public static Map<String, Object> replaceMinusWithAt(Map<String, Object> map) {
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
29 changes: 29 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,35 @@ 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);
}

@Test
void xmlToJson2() {
assertEquals(
Expand Down

0 comments on commit d2ecc79

Please sign in to comment.