From b2534ed7f607e44b38abbcb1d7bf102f5929b4c4 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 3 May 2026 12:59:49 +0300 Subject: [PATCH 1/2] Improved toXml --- src/main/java/com/github/underscore/Xml.java | 4 +++- .../java/com/github/underscore/LodashTest.java | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/underscore/Xml.java b/src/main/java/com/github/underscore/Xml.java index bcc40854..aedb9f45 100644 --- a/src/main/java/com/github/underscore/Xml.java +++ b/src/main/java/com/github/underscore/Xml.java @@ -1193,7 +1193,9 @@ private static void checkLocalMap( || localMap2.size() != 1 || XmlValue.getMapKey(localMap2).startsWith("-") || XmlValue.getMapValue(localMap2) instanceof List) { - if (ROOT.equals(XmlValue.getMapKey(localMap2)) + if (localMap2 != null + && localMap2.size() == 1 + && ROOT.equals(XmlValue.getMapKey(localMap2)) && XmlValue.getMapValue(localMap2) instanceof List) { writeArray((List) XmlValue.getMapValue(localMap2), builder, arrayTrue); } else { diff --git a/src/test/java/com/github/underscore/LodashTest.java b/src/test/java/com/github/underscore/LodashTest.java index cef5094c..e57d4313 100644 --- a/src/test/java/com/github/underscore/LodashTest.java +++ b/src/test/java/com/github/underscore/LodashTest.java @@ -1129,6 +1129,21 @@ void xmpToJson6() { + " \n" + " yes\n" + "")); + assertEquals("{\n" + + " \"root\": [\n" + + " {\n" + + " \"a\": 1\n" + + " }\n" + + " ],\n" + + " \"omit-xml-declaration\": \"yes\"\n" + + "}", + U.xmlToJson("\n" + + "\n" + + " \n" + + " 1\n" + + " \n" + + " yes\n" + + "")); } @Test From 83087a0bdb4348c99fd35b7dc367020785bb859f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 3 May 2026 13:14:35 +0300 Subject: [PATCH 2/2] Improved toXml --- src/main/java/com/github/underscore/Xml.java | 24 ++----- .../com/github/underscore/LodashTest.java | 72 ++++++++++--------- 2 files changed, 44 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/github/underscore/Xml.java b/src/main/java/com/github/underscore/Xml.java index aedb9f45..793da57c 100644 --- a/src/main/java/com/github/underscore/Xml.java +++ b/src/main/java/com/github/underscore/Xml.java @@ -1189,25 +1189,11 @@ private static void checkLocalMap( } else { localMap2 = localMap; } - if (localMap2 == null - || localMap2.size() != 1 - || XmlValue.getMapKey(localMap2).startsWith("-") - || XmlValue.getMapValue(localMap2) instanceof List) { - if (localMap2 != null - && localMap2.size() == 1 - && ROOT.equals(XmlValue.getMapKey(localMap2)) - && XmlValue.getMapValue(localMap2) instanceof List) { - writeArray((List) XmlValue.getMapValue(localMap2), builder, arrayTrue); - } else { - XmlObject.writeXml( - localMap2, - getRootName(localMap2, newRootName), - builder, - false, - new LinkedHashSet<>(), - false, - arrayTrue); - } + if (localMap2 != null + && localMap2.size() == 1 + && ROOT.equals(XmlValue.getMapKey(localMap2)) + && XmlValue.getMapValue(localMap2) instanceof List) { + writeArray((List) XmlValue.getMapValue(localMap2), builder, arrayTrue); } else { XmlObject.writeXml( localMap2, diff --git a/src/test/java/com/github/underscore/LodashTest.java b/src/test/java/com/github/underscore/LodashTest.java index e57d4313..bf1fbe9b 100644 --- a/src/test/java/com/github/underscore/LodashTest.java +++ b/src/test/java/com/github/underscore/LodashTest.java @@ -698,7 +698,7 @@ void fetchGetHttps() { U.FetchResponse result = U.fetch( "https://raw.githubusercontent.com/javadev/underscore-java/refs/heads/" - + "main/src/test/resources/example.json"); + + "main/src/test/resources/example.json"); assertEquals( "{\n" + " \"fruit\": \"Apple\",\n" @@ -793,7 +793,7 @@ public int read(byte[] b) throws IOException { @Test void testSuccessfulReadOnFirstAttempt() throws IOException { - java.io.InputStream inputStream = new TestInputStream(new int[]{100}, null); + java.io.InputStream inputStream = new TestInputStream(new int[] {100}, null); byte[] buffer = new byte[1024]; int result = U.readWithRetry(inputStream, buffer); assertEquals(100, result); @@ -801,10 +801,10 @@ void testSuccessfulReadOnFirstAttempt() throws IOException { @Test void testSuccessfulReadOnSecondAttempt() throws IOException { - java.io.InputStream inputStream = new TestInputStream( - new int[]{0, 50}, - new IOException[]{new IOException("First failed"), null} - ); + java.io.InputStream inputStream = + new TestInputStream( + new int[] {0, 50}, + new IOException[] {new IOException("First failed"), null}); byte[] buffer = new byte[1024]; int result = U.readWithRetry(inputStream, buffer); assertEquals(50, result); @@ -812,23 +812,26 @@ void testSuccessfulReadOnSecondAttempt() throws IOException { @Test void testBothAttemptsFailWithIOException() { - java.io.InputStream inputStream = new TestInputStream( - null, - new IOException[]{ - new IOException("First attempt failed"), - new IOException("Second attempt failed") - } - ); + java.io.InputStream inputStream = + new TestInputStream( + null, + new IOException[] { + new IOException("First attempt failed"), + new IOException("Second attempt failed") + }); byte[] buffer = new byte[1024]; - IOException thrown = assertThrows(IOException.class, () -> { - U.readWithRetry(inputStream, buffer); - }); + IOException thrown = + assertThrows( + IOException.class, + () -> { + U.readWithRetry(inputStream, buffer); + }); assertEquals("Second attempt failed", thrown.getMessage()); } @Test void testReadReturnsMinusOne() throws IOException { - java.io.InputStream inputStream = new TestInputStream(new int[]{-1}, null); + java.io.InputStream inputStream = new TestInputStream(new int[] {-1}, null); byte[] buffer = new byte[1024]; int result = U.readWithRetry(inputStream, buffer); assertEquals(-1, result); @@ -836,7 +839,7 @@ void testReadReturnsMinusOne() throws IOException { @Test void testReadReturnsZero() throws IOException { - java.io.InputStream inputStream = new TestInputStream(new int[]{0}, null); + java.io.InputStream inputStream = new TestInputStream(new int[] {0}, null); byte[] buffer = new byte[1024]; int result = U.readWithRetry(inputStream, buffer); assertEquals(0, result); @@ -1122,14 +1125,16 @@ void xmpToJson6() { + " },\n" + " \"omit-xml-declaration\": \"yes\"\n" + "}", - U.xmlToJson("\n" - + "\n" - + " \n" - + " 1\n" - + " \n" - + " yes\n" - + "")); - assertEquals("{\n" + U.xmlToJson( + "\n" + + "\n" + + " \n" + + " 1\n" + + " \n" + + " yes\n" + + "")); + assertEquals( + "{\n" + " \"root\": [\n" + " {\n" + " \"a\": 1\n" @@ -1137,13 +1142,14 @@ void xmpToJson6() { + " ],\n" + " \"omit-xml-declaration\": \"yes\"\n" + "}", - U.xmlToJson("\n" - + "\n" - + " \n" - + " 1\n" - + " \n" - + " yes\n" - + "")); + U.xmlToJson( + "\n" + + "\n" + + " \n" + + " 1\n" + + " \n" + + " yes\n" + + "")); } @Test