This commit is contained in:
Looly 2022-12-27 16:55:09 +08:00
parent f013446279
commit 803f1b313c
3 changed files with 42 additions and 4 deletions

View File

@ -87,13 +87,13 @@ public class JSONXMLSerializer {
} else if (value instanceof JSONArray) {
for (final Object val : (JSONArray) value) {
if (val instanceof JSONArray) {
sb.append(wrapWithTag(toXml(val), key));
sb.append(wrapWithTag(toXml(val, null, contentKeys), key));
} else {
sb.append(toXml(val, key));
sb.append(toXml(val, key, contentKeys));
}
}
} else {
sb.append(toXml(value, key));
sb.append(toXml(value, key, contentKeys));
}
});
@ -111,7 +111,7 @@ public class JSONXMLSerializer {
// XML does not have good support for arrays. If an array
// appears in a place where XML is lacking, synthesize an
// <array> element.
sb.append(toXml(val, tagName == null ? "array" : tagName));
sb.append(toXml(val, tagName == null ? "array" : tagName, contentKeys));
}
return sb.toString();
}

View File

@ -0,0 +1,19 @@
package cn.hutool.json;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.json.xml.JSONXMLSerializer;
import org.junit.Assert;
import org.junit.Test;
import javax.xml.xpath.XPathConstants;
public class IssueI676ITTest {
@Test
public void parseXMLTest() {
final JSONObject jsonObject = JSONUtil.parseObj(ResourceUtil.readUtf8Str("issueI676IT.json"));
final String xmlStr = JSONXMLSerializer.toXml(jsonObject, null, (String) null);
final String content = String.valueOf(XmlUtil.getByXPath("/page/orderItems[1]/content", XmlUtil.readXML(xmlStr), XPathConstants.STRING));
Assert.assertEquals(content, "bar1");
}
}

View File

@ -0,0 +1,19 @@
{
"page": {
"pageSize": 33,
"currentPage": 81,
"totalSize": 49,
"orderItems": [
{
"asc": true,
"columnName": "foo",
"content": "bar1"
},
{
"asc": false,
"columnName": "foo",
"content": "bar2"
}
]
}
}