diff --git a/src/main/java/com/github/underscore/Json.java b/src/main/java/com/github/underscore/Json.java index 5f46598..ff67747 100644 --- a/src/main/java/com/github/underscore/Json.java +++ b/src/main/java/com/github/underscore/Json.java @@ -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' diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index b4b7f49..5d88af4 100644 --- a/src/main/java/com/github/underscore/U.java +++ b/src/main/java/com/github/underscore/U.java @@ -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 { @@ -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( @@ -2974,6 +2985,38 @@ private static Object makeObjectSelfClose(Object value, String newValue) { return result; } + public static Map replaceMinusWithAt(Map map) { + if (map == null) { + return null; + } + Map outMap = new LinkedHashMap<>(); + for (Map.Entry 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 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 replaceEmptyValueWithNull(Map map) { if (map == null || map.isEmpty()) { return null; diff --git a/src/main/java/com/github/underscore/Xml.java b/src/main/java/com/github/underscore/Xml.java index a7d5ea0..efc4ccd 100644 --- a/src/main/java/com/github/underscore/Xml.java +++ b/src/main/java/com/github/underscore/Xml.java @@ -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: diff --git a/src/test/java/com/github/underscore/LodashTest.java b/src/test/java/com/github/underscore/LodashTest.java index 1a09288..46ba9f6 100644 --- a/src/test/java/com/github/underscore/LodashTest.java +++ b/src/test/java/com/github/underscore/LodashTest.java @@ -954,6 +954,39 @@ void xmlToJson() { U.replaceEmptyValueWithEmptyString(map4); } + @Test + void xmpToJson3() { + Map map2 = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new ArrayList()); + 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( + "", U.XmlToJsonMode.REPLACE_MINUS_WITH_AT)); + Map map3 = new LinkedHashMap<>(); + List list2 = new ArrayList<>(); + list2.add(new ArrayList()); + map3.put("list", list2); + U.replaceMinusWithAt(map3); + U.replaceMinusWithAt(null); + U.xmlToJson( + "", + U.XmlToJsonMode.REPLACE_EMPTY_TAG_WITH_NULL_AND_MINUS_WITH_AT); + } + @Test void xmlToJson2() { assertEquals(