Skip to content

Commit 141c6b9

Browse files
committed
Added method U.streamJsonToXml(jsonInputStream, xmlOutputStream, identStep)
1 parent 41c513b commit 141c6b9

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,6 +2850,30 @@ public static void fileJsonToXml(String jsonFileName, String xmlFileName) throws
28502850
fileJsonToXml(jsonFileName, xmlFileName, Xml.XmlStringBuilder.Step.TWO_SPACES);
28512851
}
28522852

2853+
public static void streamJsonToXml(
2854+
InputStream jsonInputStream,
2855+
OutputStream xmlOutputStream,
2856+
Xml.XmlStringBuilder.Step identStep)
2857+
throws IOException {
2858+
byte[] bytes = jsonInputStream.readAllBytes();
2859+
String jsonText = new String(removeBom(bytes), detectEncoding(bytes));
2860+
Object jsonObject = U.fromJson(jsonText);
2861+
String lineSeparator = System.lineSeparator();
2862+
String xml;
2863+
if (jsonObject instanceof Map) {
2864+
xml = formatString(Xml.toXml((Map<?, ?>) jsonObject, identStep), lineSeparator);
2865+
if (((Map) jsonObject).containsKey("#encoding")) {
2866+
String encoding = String.valueOf(((Map) jsonObject).get("#encoding"));
2867+
xmlOutputStream.write(xml.getBytes(encoding));
2868+
} else {
2869+
xmlOutputStream.write(xml.getBytes(StandardCharsets.UTF_8));
2870+
}
2871+
} else {
2872+
xml = formatString(Xml.toXml((List<?>) jsonObject, identStep), lineSeparator);
2873+
xmlOutputStream.write(xml.getBytes(StandardCharsets.UTF_8));
2874+
}
2875+
}
2876+
28532877
public static byte[] removeBom(byte[] bytes) {
28542878
if ((bytes.length >= 3) && (bytes[0] == -17) && (bytes[1] == -69) && (bytes[2] == -65)) {
28552879
return Arrays.copyOfRange(bytes, 3, bytes.length);

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,4 +1226,86 @@ void testListResult(@TempDir Path tempDir) throws IOException {
12261226
xmlStr,
12271227
"Should write XML using UTF-8 when result is a List");
12281228
}
1229+
1230+
@Test
1231+
void testStreamJsonToXml_ObjectMap_DefaultEncoding() throws Exception {
1232+
String inputJson = "{\"root\":\"value\"}";
1233+
ByteArrayInputStream inputStream =
1234+
new ByteArrayInputStream(inputJson.getBytes(StandardCharsets.UTF_8));
1235+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
1236+
U.streamJsonToXml(inputStream, outputStream, Xml.XmlStringBuilder.Step.TWO_SPACES);
1237+
String expectedXml =
1238+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1239+
+ System.lineSeparator()
1240+
+ "<root>value</root>";
1241+
String actualXml = outputStream.toString(StandardCharsets.UTF_8);
1242+
assertEquals(
1243+
expectedXml,
1244+
actualXml,
1245+
"XML output should match expected XML for simple JSON object with default encoding");
1246+
}
1247+
1248+
@Test
1249+
void testStreamJsonToXml_ObjectMap_CustomEncoding() throws Exception {
1250+
String inputJson = "{\"root\":\"value\",\"#encoding\":\"UTF-16\"}";
1251+
ByteArrayInputStream inputStream =
1252+
new ByteArrayInputStream(inputJson.getBytes(StandardCharsets.UTF_8));
1253+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
1254+
U.streamJsonToXml(inputStream, outputStream, Xml.XmlStringBuilder.Step.TWO_SPACES);
1255+
String expectedXml =
1256+
"<?xml version=\"1.0\" encoding=\"UTF-16\"?>"
1257+
+ System.lineSeparator()
1258+
+ "<root>value</root>";
1259+
String actualXml = outputStream.toString(StandardCharsets.UTF_16);
1260+
assertEquals(
1261+
expectedXml,
1262+
actualXml,
1263+
"XML output should match expected XML for simple JSON object with default encoding");
1264+
}
1265+
1266+
@Test
1267+
void testStreamJsonToXml_List() throws Exception {
1268+
String inputJson = "[{\"item\":42}]";
1269+
ByteArrayInputStream inputStream =
1270+
new ByteArrayInputStream(inputJson.getBytes(StandardCharsets.UTF_8));
1271+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
1272+
U.streamJsonToXml(inputStream, outputStream, Xml.XmlStringBuilder.Step.TWO_SPACES);
1273+
String expectedXml =
1274+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1275+
+ System.lineSeparator()
1276+
+ "<root>"
1277+
+ System.lineSeparator()
1278+
+ " <element array=\"true\">"
1279+
+ System.lineSeparator()
1280+
+ " <item number=\"true\">42</item>"
1281+
+ System.lineSeparator()
1282+
+ " </element>"
1283+
+ System.lineSeparator()
1284+
+ "</root>";
1285+
String actualXml = outputStream.toString(StandardCharsets.UTF_8);
1286+
assertEquals(
1287+
expectedXml,
1288+
actualXml,
1289+
"XML output should match expected XML for JSON array root with default encoding");
1290+
}
1291+
1292+
@Test
1293+
void testStreamJsonToXml_InvalidJson_ThrowsException() {
1294+
String inputJson = "invalid";
1295+
ByteArrayInputStream inputStream =
1296+
new ByteArrayInputStream(inputJson.getBytes(StandardCharsets.UTF_8));
1297+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
1298+
Exception exception =
1299+
assertThrows(
1300+
Json.ParseException.class,
1301+
() ->
1302+
U.streamJsonToXml(
1303+
inputStream,
1304+
outputStream,
1305+
Xml.XmlStringBuilder.Step.TWO_SPACES));
1306+
String actualMessage = exception.getMessage();
1307+
assertTrue(
1308+
actualMessage.contains("Expected value"),
1309+
"Should throw exception if JSON is invalid");
1310+
}
12291311
}

0 commit comments

Comments
 (0)