Skip to content

Commit e0be532

Browse files
committed
Added method U.fileJsonToXml(jsonFileName, xmlFileName, identStep)
1 parent 69d883e commit e0be532

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.nio.channels.ReadableByteChannel;
3636
import java.nio.charset.StandardCharsets;
3737
import java.nio.file.Files;
38+
import java.nio.file.Path;
3839
import java.nio.file.Paths;
3940
import java.util.ArrayList;
4041
import java.util.Arrays;
@@ -2816,6 +2817,40 @@ public static void streamXmlToJson(InputStream xmlInputStream, OutputStream json
28162817
streamXmlToJson(xmlInputStream, jsonOutputStream, Json.JsonStringBuilder.Step.TWO_SPACES);
28172818
}
28182819

2820+
public static void fileJsonToXml(
2821+
String jsonFileName, String xmlFileName, Xml.XmlStringBuilder.Step identStep)
2822+
throws IOException {
2823+
final byte[] bytes = Files.readAllBytes(Paths.get(jsonFileName));
2824+
String jsonText = new String(removeBom(bytes), detectEncoding(bytes));
2825+
Object result = U.fromJson(jsonText);
2826+
Path xmlFilePath = Paths.get(xmlFileName);
2827+
String lineSeparator = System.lineSeparator();
2828+
if (result instanceof Map) {
2829+
if (((Map) result).containsKey("#encoding")) {
2830+
String encoding = String.valueOf(((Map) result).get("#encoding"));
2831+
Files.write(
2832+
xmlFilePath,
2833+
formatString(Xml.toXml((Map) result, identStep), lineSeparator)
2834+
.getBytes(encoding));
2835+
} else {
2836+
Files.write(
2837+
xmlFilePath,
2838+
formatString(Xml.toXml((Map) result, identStep), lineSeparator)
2839+
.getBytes(StandardCharsets.UTF_8));
2840+
}
2841+
} else {
2842+
Files.write(
2843+
xmlFilePath,
2844+
formatString(Xml.toXml((List) result, identStep), lineSeparator)
2845+
.getBytes(StandardCharsets.UTF_8));
2846+
}
2847+
}
2848+
2849+
public static void fileJsonToXml(
2850+
String jsonFileName, String xmlFileName) throws IOException {
2851+
fileJsonToXml(jsonFileName, xmlFileName, Xml.XmlStringBuilder.Step.TWO_SPACES);
2852+
}
2853+
28192854
public static byte[] removeBom(byte[] bytes) {
28202855
if ((bytes.length >= 3) && (bytes[0] == -17) && (bytes[1] == -69) && (bytes[2] == -65)) {
28212856
return Arrays.copyOfRange(bytes, 3, bytes.length);

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,4 +1153,61 @@ void testStreamXmlToJson_withIndentSteps_producesIndentedJson() throws IOExcepti
11531153
String jsonOutput = jsonStream.toString("UTF-8");
11541154
assertTrue(jsonOutput.contains(" "), "JSON output should be indented with four spaces.");
11551155
}
1156+
1157+
@Test
1158+
void testMapWithEncodingKey(@TempDir Path tempDir) throws IOException {
1159+
// Arrange
1160+
Path jsonFile = tempDir.resolve("in.json");
1161+
Path xmlFile = tempDir.resolve("out.xml");
1162+
String encoding = "UTF-16";
1163+
Map<String, Object> map = new LinkedHashMap<>();
1164+
map.put("#encoding", encoding);
1165+
// Write json
1166+
String jsonText = "{\"#encoding\":\"" + encoding + "\"}";
1167+
Files.write(jsonFile, jsonText.getBytes(StandardCharsets.UTF_8));
1168+
// Act
1169+
U.fileJsonToXml(jsonFile.toString(), xmlFile.toString());
1170+
// Assert
1171+
byte[] xmlBytes = Files.readAllBytes(xmlFile);
1172+
String xmlStr = new String(xmlBytes, encoding);
1173+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-16\"?>" + System.lineSeparator()
1174+
+ "<root></root>", xmlStr, "Should write XML with provided encoding when #encoding key present");
1175+
}
1176+
1177+
@Test
1178+
void testMapWithoutEncodingKey(@TempDir Path tempDir) throws IOException {
1179+
// Arrange
1180+
Path jsonFile = tempDir.resolve("in.json");
1181+
Path xmlFile = tempDir.resolve("out.xml");
1182+
Map<String, Object> map = new LinkedHashMap<>();
1183+
String jsonText = "{}";
1184+
Files.write(jsonFile, jsonText.getBytes(StandardCharsets.UTF_8));
1185+
// Act
1186+
U.fileJsonToXml(jsonFile.toString(), xmlFile.toString(), Xml.XmlStringBuilder.Step.TWO_SPACES);
1187+
// Assert
1188+
byte[] xmlBytes = Files.readAllBytes(xmlFile);
1189+
String xmlStr = new String(xmlBytes, StandardCharsets.UTF_8);
1190+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + System.lineSeparator()
1191+
+ "<root></root>", xmlStr, "Should write XML using UTF-8 when #encoding key not present");
1192+
}
1193+
1194+
@Test
1195+
void testListResult(@TempDir Path tempDir) throws IOException {
1196+
// Arrange
1197+
Path jsonFile = tempDir.resolve("in.json");
1198+
Path xmlFile = tempDir.resolve("out.xml");
1199+
Files.write(jsonFile, "[1,2,3]".getBytes(StandardCharsets.UTF_8));
1200+
// Act
1201+
U.fileJsonToXml(jsonFile.toString(), xmlFile.toString(), Xml.XmlStringBuilder.Step.TWO_SPACES);
1202+
// Assert
1203+
byte[] xmlBytes = Files.readAllBytes(xmlFile);
1204+
String xmlStr = new String(xmlBytes, StandardCharsets.UTF_8);
1205+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + System.lineSeparator()
1206+
+ "<root>" + System.lineSeparator()
1207+
+ " <element number=\"true\">1</element>" + System.lineSeparator()
1208+
+ " <element number=\"true\">2</element>" + System.lineSeparator()
1209+
+ " <element number=\"true\">3</element>" + System.lineSeparator()
1210+
+ "</root>", xmlStr,
1211+
"Should write XML using UTF-8 when result is a List");
1212+
}
11561213
}

0 commit comments

Comments
 (0)