Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/com/github/underscore/U.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -2793,6 +2795,20 @@ public static void fileXmlToJson(String xmlFileName, String jsonFileName) throws
fileXmlToJson(xmlFileName, jsonFileName, Json.JsonStringBuilder.Step.TWO_SPACES);
}

public static void streamXmlToJson(InputStream xmlInputStream, OutputStream jsonOutputStream,
Json.JsonStringBuilder.Step indentStep) throws IOException {
byte[] bytes = xmlInputStream.readAllBytes();
String encoding = detectEncoding(bytes);
String xmlText = new String(removeBom(bytes), encoding);
String jsonText = xmlToJson(xmlText, indentStep);
String formattedJson = formatString(jsonText, System.lineSeparator());
jsonOutputStream.write(formattedJson.getBytes(StandardCharsets.UTF_8));
}

public static void streamXmlToJson(InputStream xmlInputStream, OutputStream jsonOutputStream) throws IOException {
streamXmlToJson(xmlInputStream, jsonOutputStream, Json.JsonStringBuilder.Step.TWO_SPACES);
}

public static byte[] removeBom(byte[] bytes) {
if ((bytes.length >= 3) && (bytes[0] == -17) && (bytes[1] == -69) && (bytes[2] == -65)) {
return Arrays.copyOfRange(bytes, 3, bytes.length);
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/com/github/underscore/UnderscoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -1084,4 +1087,51 @@ void testFileXmlToJsonWithInvalidInput(@TempDir Path tempDir) {
"Should throw IOException when input file doesn't exist"
);
}

@Test
void testStreamXmlToJson_validXml_writesJson() throws IOException {
String xml = "<root><name>Test</name></root>";
InputStream xmlStream = new ByteArrayInputStream(xml.getBytes());
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
U.streamXmlToJson(xmlStream, jsonStream);
String jsonOutput = jsonStream.toString("UTF-8");
assertTrue(jsonOutput.contains("name"), "JSON output should contain 'name' field.");
assertTrue(jsonOutput.contains("Test"), "JSON output should contain 'Test' value.");
assertTrue(jsonOutput.startsWith("{"), "JSON output should start with '{'.");
assertTrue(jsonOutput.endsWith("}"), "JSON output should end with '}'.");
}

@Test
void testStreamXmlToJson_emptyInput_producesEmptyOrError() {
InputStream xmlStream = new ByteArrayInputStream(new byte[0]);
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
Exception exception = assertThrows(Exception.class, () -> {
U.streamXmlToJson(xmlStream, jsonStream, Json.JsonStringBuilder.Step.TWO_SPACES);
}, "Should throw exception for empty input.");
String msg = exception.getMessage();
assertNotNull(msg, "Exception message should not be null.");
}

@Test
void testStreamXmlToJson_invalidXml_throwsException() {
// missing closing tag
String invalidXml = "<root><name>Test</name>";
InputStream xmlStream = new ByteArrayInputStream(invalidXml.getBytes());
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
Exception exception = assertThrows(Exception.class, () -> {
U.streamXmlToJson(xmlStream, jsonStream, Json.JsonStringBuilder.Step.TWO_SPACES);
}, "Should throw exception for invalid XML.");
String msg = exception.getMessage();
assertNotNull(msg, "Exception message for invalid XML should not be null.");
}

@Test
void testStreamXmlToJson_withIndentSteps_producesIndentedJson() throws IOException {
String xml = "<root><field>value</field></root>";
InputStream xmlStream = new ByteArrayInputStream(xml.getBytes());
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
U.streamXmlToJson(xmlStream, jsonStream, Json.JsonStringBuilder.Step.FOUR_SPACES);
String jsonOutput = jsonStream.toString("UTF-8");
assertTrue(jsonOutput.contains(" "), "JSON output should be indented with four spaces.");
}
}
Loading