Skip to content

Commit

Permalink
Added new mode XmlToJsonMode.REPLACE_EMPTY_TAG_WITH_NULL_AND_MINUS_WI…
Browse files Browse the repository at this point in the history
…TH_AT
  • Loading branch information
javadev committed Feb 11, 2024
1 parent d2ecc79 commit df2eb85
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/main/java/com/github/underscore/U.java
Expand Up @@ -141,7 +141,8 @@ public enum XmlToJsonMode {
REPLACE_EMPTY_TAG_WITH_STRING,
REMOVE_FIRST_LEVEL,
WITHOUT_NAMESPACES,
REPLACE_MINUS_WITH_AT
REPLACE_MINUS_WITH_AT,
REPLACE_EMPTY_TAG_WITH_NULL_AND_MINUS_WITH_AT
}

public enum JsonToXmlMode {
Expand Down Expand Up @@ -2701,6 +2702,13 @@ public static String xmlToJson(
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 @@ -2978,6 +2986,9 @@ private static Object makeObjectSelfClose(Object value, String newValue) {
}

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(
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/com/github/underscore/LodashTest.java
Expand Up @@ -981,6 +981,10 @@ void xmpToJson3() {
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
Expand Down

0 comments on commit df2eb85

Please sign in to comment.