Skip to content

Commit cae8b73

Browse files
authored
Added method U.streamJsonToXml(jsonInputStream, xmlOutputStream, identStep)
1 parent 41c513b commit cae8b73

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public class U<T> extends Underscore<T> {
103103
java.util.regex.Pattern.compile(
104104
UPPER + "+(?=" + UPPER + LOWER + ")|" + UPPER + "?" + LOWER + "|" + UPPER
105105
+ "+|\\d+");
106+
private static final String ENCODING = "#encoding";
106107

107108
static {
108109
String[] deburredLetters =
@@ -2826,8 +2827,8 @@ public static void fileJsonToXml(
28262827
Path xmlFilePath = Paths.get(xmlFileName);
28272828
String lineSeparator = System.lineSeparator();
28282829
if (result instanceof Map) {
2829-
if (((Map) result).containsKey("#encoding")) {
2830-
String encoding = String.valueOf(((Map) result).get("#encoding"));
2830+
if (((Map) result).containsKey(ENCODING)) {
2831+
String encoding = String.valueOf(((Map) result).get(ENCODING));
28312832
Files.write(
28322833
xmlFilePath,
28332834
formatString(Xml.toXml((Map) result, identStep), lineSeparator)
@@ -2850,6 +2851,35 @@ public static void fileJsonToXml(String jsonFileName, String xmlFileName) throws
28502851
fileJsonToXml(jsonFileName, xmlFileName, Xml.XmlStringBuilder.Step.TWO_SPACES);
28512852
}
28522853

2854+
public static void streamJsonToXml(
2855+
InputStream jsonInputStream,
2856+
OutputStream xmlOutputStream,
2857+
Xml.XmlStringBuilder.Step identStep)
2858+
throws IOException {
2859+
byte[] bytes = jsonInputStream.readAllBytes();
2860+
String jsonText = new String(removeBom(bytes), detectEncoding(bytes));
2861+
Object jsonObject = U.fromJson(jsonText);
2862+
String lineSeparator = System.lineSeparator();
2863+
String xml;
2864+
if (jsonObject instanceof Map) {
2865+
xml = formatString(Xml.toXml((Map<?, ?>) jsonObject, identStep), lineSeparator);
2866+
if (((Map) jsonObject).containsKey(ENCODING)) {
2867+
String encoding = String.valueOf(((Map) jsonObject).get(ENCODING));
2868+
xmlOutputStream.write(xml.getBytes(encoding));
2869+
} else {
2870+
xmlOutputStream.write(xml.getBytes(StandardCharsets.UTF_8));
2871+
}
2872+
} else {
2873+
xml = formatString(Xml.toXml((List<?>) jsonObject, identStep), lineSeparator);
2874+
xmlOutputStream.write(xml.getBytes(StandardCharsets.UTF_8));
2875+
}
2876+
}
2877+
2878+
public static void streamJsonToXml(InputStream jsonInputStream, OutputStream xmlOutputStream)
2879+
throws IOException {
2880+
streamJsonToXml(jsonInputStream, xmlOutputStream, Xml.XmlStringBuilder.Step.TWO_SPACES);
2881+
}
2882+
28532883
public static byte[] removeBom(byte[] bytes) {
28542884
if ((bytes.length >= 3) && (bytes[0] == -17) && (bytes[1] == -69) && (bytes[2] == -65)) {
28552885
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);
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)