Skip to content

Commit 9115239

Browse files
committed
Improve json to xml convertion.
1 parent b50767f commit 9115239

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/main/java/com/github/underscore/lodash/U.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,10 @@ public static void writeXml(Map map, String name, XmlStringBuilder builder, bool
20952095
}
20962096
}
20972097
if (name != null) {
2098-
builder.fillSpaces().append("<").append(name).append(U.join(attrs, "")).append(">").incIdent();
2098+
if (!parentTextFound) {
2099+
builder.fillSpaces();
2100+
}
2101+
builder.append("<").append(name).append(U.join(attrs, "")).append(">").incIdent();
20992102
if (!textFoundSave && !map.isEmpty()) {
21002103
builder.newLine();
21012104
}

src/test/java/com/github/underscore/lodash/StringTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,17 @@ public void toXmlFromJson6() {
15871587
U.toXml((Map<String, Object>) U.fromJson(json)));
15881588
}
15891589

1590+
@SuppressWarnings("unchecked")
1591+
@Test
1592+
public void toXmlFromJson7() {
1593+
final String json = "{\n \"widget\": {\n \"debug\": \"on\",\n \"#text\": \"выапвыап\\n пвыапыв\",\n"
1594+
+ " \"image\": {\n \"alignment\": \"center\"\n }\n }\n}";
1595+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1596+
+ "<widget>\n <debug>on</debug>выапвыап\n пвыапыв<image>\n"
1597+
+ " <alignment>center</alignment>\n </image>\n</widget>",
1598+
U.toXml((Map<String, Object>) U.fromJson(json)));
1599+
}
1600+
15901601
@SuppressWarnings("unchecked")
15911602
@Test
15921603
public void toXml() {

0 commit comments

Comments
 (0)