Skip to content

Commit f45986c

Browse files
committed
Added method U.streamXmlToJson(xmlInputStream, jsonOutputStream, identStep)
1 parent 09549ab commit f45986c

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
import java.io.FileInputStream;
2727
import java.io.FileOutputStream;
28+
import java.io.InputStream;
2829
import java.io.IOException;
30+
import java.io.OutputStream;
2931
import java.net.URI;
3032
import java.net.URISyntaxException;
3133
import java.net.URL;
@@ -2793,6 +2795,16 @@ public static void fileXmlToJson(String xmlFileName, String jsonFileName) throws
27932795
fileXmlToJson(xmlFileName, jsonFileName, Json.JsonStringBuilder.Step.TWO_SPACES);
27942796
}
27952797

2798+
public static void streamXmlToJson(InputStream xmlInputStream, OutputStream jsonOutputStream,
2799+
Json.JsonStringBuilder.Step indentStep) throws IOException {
2800+
byte[] bytes = xmlInputStream.readAllBytes();
2801+
String encoding = detectEncoding(bytes);
2802+
String xmlText = new String(removeBom(bytes), encoding);
2803+
String jsonText = xmlToJson(xmlText, indentStep);
2804+
String formattedJson = formatString(jsonText, System.lineSeparator());
2805+
jsonOutputStream.write(formattedJson.getBytes(StandardCharsets.UTF_8));
2806+
}
2807+
27962808
public static byte[] removeBom(byte[] bytes) {
27972809
if ((bytes.length >= 3) && (bytes[0] == -17) && (bytes[1] == -69) && (bytes[2] == -65)) {
27982810
return Arrays.copyOfRange(bytes, 3, bytes.length);

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
import static org.junit.jupiter.api.Assertions.assertTrue;
3838
import static org.junit.jupiter.api.Assertions.fail;
3939

40+
import java.io.ByteArrayInputStream;
41+
import java.io.ByteArrayOutputStream;
42+
import java.io.InputStream;
43+
import java.io.OutputStream;
4044
import java.io.IOException;
4145
import java.nio.charset.StandardCharsets;
4246
import java.nio.file.Files;
@@ -1084,4 +1088,52 @@ void testFileXmlToJsonWithInvalidInput(@TempDir Path tempDir) {
10841088
"Should throw IOException when input file doesn't exist"
10851089
);
10861090
}
1091+
1092+
@Test
1093+
void testStreamXmlToJson_validXml_writesJson() throws IOException {
1094+
String xml = "<root><name>Test</name></root>";
1095+
InputStream xmlStream = new ByteArrayInputStream(xml.getBytes());
1096+
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
1097+
U.streamXmlToJson(xmlStream, jsonStream, Json.JsonStringBuilder.Step.TWO_SPACES);
1098+
String jsonOutput = jsonStream.toString("UTF-8");
1099+
assertTrue(jsonOutput.contains("name"), "JSON output should contain 'name' field.");
1100+
assertTrue(jsonOutput.contains("Test"), "JSON output should contain 'Test' value.");
1101+
assertTrue(jsonOutput.startsWith("{"), "JSON output should start with '{'.");
1102+
assertTrue(jsonOutput.endsWith("}"), "JSON output should end with '}'.");
1103+
}
1104+
1105+
@Test
1106+
void testStreamXmlToJson_emptyInput_producesEmptyOrError() {
1107+
InputStream xmlStream = new ByteArrayInputStream(new byte[0]);
1108+
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
1109+
Exception exception = assertThrows(Exception.class, () -> {
1110+
U.streamXmlToJson(xmlStream, jsonStream, Json.JsonStringBuilder.Step.TWO_SPACES);
1111+
}, "Should throw exception for empty input.");
1112+
String msg = exception.getMessage();
1113+
assertNotNull(msg, "Exception message should not be null.");
1114+
}
1115+
1116+
@Test
1117+
void testStreamXmlToJson_invalidXml_throwsException() {
1118+
// missing closing tag
1119+
String invalidXml = "<root><name>Test</name>";
1120+
InputStream xmlStream = new ByteArrayInputStream(invalidXml.getBytes());
1121+
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
1122+
Exception exception = assertThrows(Exception.class, () -> {
1123+
U.streamXmlToJson(xmlStream, jsonStream, Json.JsonStringBuilder.Step.TWO_SPACES);
1124+
}, "Should throw exception for invalid XML.");
1125+
1126+
String msg = exception.getMessage();
1127+
assertNotNull(msg, "Exception message for invalid XML should not be null.");
1128+
}
1129+
1130+
@Test
1131+
void testStreamXmlToJson_withIndentSteps_producesIndentedJson() throws IOException {
1132+
String xml = "<root><field>value</field></root>";
1133+
InputStream xmlStream = new ByteArrayInputStream(xml.getBytes());
1134+
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
1135+
U.streamXmlToJson(xmlStream, jsonStream, Json.JsonStringBuilder.Step.FOUR_SPACES);
1136+
String jsonOutput = jsonStream.toString("UTF-8");
1137+
assertTrue(jsonOutput.contains(" "), "JSON output should be indented with four spaces.");
1138+
}
10871139
}

0 commit comments

Comments
 (0)