Skip to content

Commit e07d484

Browse files
authored
Improved toXml
1 parent 747e783 commit e07d484

2 files changed

Lines changed: 51 additions & 42 deletions

File tree

src/main/java/com/github/underscore/Xml.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,23 +1189,11 @@ private static void checkLocalMap(
11891189
} else {
11901190
localMap2 = localMap;
11911191
}
1192-
if (localMap2 == null
1193-
|| localMap2.size() != 1
1194-
|| XmlValue.getMapKey(localMap2).startsWith("-")
1195-
|| XmlValue.getMapValue(localMap2) instanceof List) {
1196-
if (ROOT.equals(XmlValue.getMapKey(localMap2))
1197-
&& XmlValue.getMapValue(localMap2) instanceof List) {
1198-
writeArray((List) XmlValue.getMapValue(localMap2), builder, arrayTrue);
1199-
} else {
1200-
XmlObject.writeXml(
1201-
localMap2,
1202-
getRootName(localMap2, newRootName),
1203-
builder,
1204-
false,
1205-
new LinkedHashSet<>(),
1206-
false,
1207-
arrayTrue);
1208-
}
1192+
if (localMap2 != null
1193+
&& localMap2.size() == 1
1194+
&& ROOT.equals(XmlValue.getMapKey(localMap2))
1195+
&& XmlValue.getMapValue(localMap2) instanceof List) {
1196+
writeArray((List) XmlValue.getMapValue(localMap2), builder, arrayTrue);
12091197
} else {
12101198
XmlObject.writeXml(
12111199
localMap2,

src/test/java/com/github/underscore/LodashTest.java

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ void fetchGetHttps() {
698698
U.FetchResponse result =
699699
U.fetch(
700700
"https://raw.githubusercontent.com/javadev/underscore-java/refs/heads/"
701-
+ "main/src/test/resources/example.json");
701+
+ "main/src/test/resources/example.json");
702702
assertEquals(
703703
"{\n"
704704
+ " \"fruit\": \"Apple\",\n"
@@ -793,50 +793,53 @@ public int read(byte[] b) throws IOException {
793793

794794
@Test
795795
void testSuccessfulReadOnFirstAttempt() throws IOException {
796-
java.io.InputStream inputStream = new TestInputStream(new int[]{100}, null);
796+
java.io.InputStream inputStream = new TestInputStream(new int[] {100}, null);
797797
byte[] buffer = new byte[1024];
798798
int result = U.readWithRetry(inputStream, buffer);
799799
assertEquals(100, result);
800800
}
801801

802802
@Test
803803
void testSuccessfulReadOnSecondAttempt() throws IOException {
804-
java.io.InputStream inputStream = new TestInputStream(
805-
new int[]{0, 50},
806-
new IOException[]{new IOException("First failed"), null}
807-
);
804+
java.io.InputStream inputStream =
805+
new TestInputStream(
806+
new int[] {0, 50},
807+
new IOException[] {new IOException("First failed"), null});
808808
byte[] buffer = new byte[1024];
809809
int result = U.readWithRetry(inputStream, buffer);
810810
assertEquals(50, result);
811811
}
812812

813813
@Test
814814
void testBothAttemptsFailWithIOException() {
815-
java.io.InputStream inputStream = new TestInputStream(
816-
null,
817-
new IOException[]{
818-
new IOException("First attempt failed"),
819-
new IOException("Second attempt failed")
820-
}
821-
);
815+
java.io.InputStream inputStream =
816+
new TestInputStream(
817+
null,
818+
new IOException[] {
819+
new IOException("First attempt failed"),
820+
new IOException("Second attempt failed")
821+
});
822822
byte[] buffer = new byte[1024];
823-
IOException thrown = assertThrows(IOException.class, () -> {
824-
U.readWithRetry(inputStream, buffer);
825-
});
823+
IOException thrown =
824+
assertThrows(
825+
IOException.class,
826+
() -> {
827+
U.readWithRetry(inputStream, buffer);
828+
});
826829
assertEquals("Second attempt failed", thrown.getMessage());
827830
}
828831

829832
@Test
830833
void testReadReturnsMinusOne() throws IOException {
831-
java.io.InputStream inputStream = new TestInputStream(new int[]{-1}, null);
834+
java.io.InputStream inputStream = new TestInputStream(new int[] {-1}, null);
832835
byte[] buffer = new byte[1024];
833836
int result = U.readWithRetry(inputStream, buffer);
834837
assertEquals(-1, result);
835838
}
836839

837840
@Test
838841
void testReadReturnsZero() throws IOException {
839-
java.io.InputStream inputStream = new TestInputStream(new int[]{0}, null);
842+
java.io.InputStream inputStream = new TestInputStream(new int[] {0}, null);
840843
byte[] buffer = new byte[1024];
841844
int result = U.readWithRetry(inputStream, buffer);
842845
assertEquals(0, result);
@@ -1122,13 +1125,31 @@ void xmpToJson6() {
11221125
+ " },\n"
11231126
+ " \"omit-xml-declaration\": \"yes\"\n"
11241127
+ "}",
1125-
U.xmlToJson("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1126-
+ "<root>\n"
1127-
+ " <root>\n"
1128-
+ " <a number=\"true\">1</a>\n"
1129-
+ " </root>\n"
1130-
+ " <omit-xml-declaration>yes</omit-xml-declaration>\n"
1131-
+ "</root>"));
1128+
U.xmlToJson(
1129+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1130+
+ "<root>\n"
1131+
+ " <root>\n"
1132+
+ " <a number=\"true\">1</a>\n"
1133+
+ " </root>\n"
1134+
+ " <omit-xml-declaration>yes</omit-xml-declaration>\n"
1135+
+ "</root>"));
1136+
assertEquals(
1137+
"{\n"
1138+
+ " \"root\": [\n"
1139+
+ " {\n"
1140+
+ " \"a\": 1\n"
1141+
+ " }\n"
1142+
+ " ],\n"
1143+
+ " \"omit-xml-declaration\": \"yes\"\n"
1144+
+ "}",
1145+
U.xmlToJson(
1146+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1147+
+ "<root>\n"
1148+
+ " <root array=\"true\">\n"
1149+
+ " <a number=\"true\">1</a>\n"
1150+
+ " </root>\n"
1151+
+ " <omit-xml-declaration>yes</omit-xml-declaration>\n"
1152+
+ "</root>"));
11321153
}
11331154

11341155
@Test

0 commit comments

Comments
 (0)