Skip to content

Commit b6c9520

Browse files
authored
Added method U.xmlFolderToJson(xmlFolder, jsonFolder, identStep)
1 parent eda6b8f commit b6c9520

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,6 +2887,39 @@ public static void covertJsonToXml(Path path, Path sourceRoot, Path targetRoot,
28872887
fileJsonToXml(path.toAbsolutePath().toString(), targetPath.toString(), identStep);
28882888
}
28892889

2890+
public static void xmlFolderToJson(
2891+
String xmlFolder, String jsonFolder, Json.JsonStringBuilder.Step identStep)
2892+
throws IOException {
2893+
Path sourceRoot = Paths.get(xmlFolder);
2894+
Path targetRoot = Paths.get(jsonFolder);
2895+
Files.walkFileTree(sourceRoot, new SimpleFileVisitor<>() {
2896+
@Override
2897+
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
2898+
covertXmlToJson(path, sourceRoot, targetRoot, identStep);
2899+
return FileVisitResult.CONTINUE;
2900+
}
2901+
});
2902+
}
2903+
2904+
public static void xmlFolderToJson(String xmlFolder, String jsonFolder) throws IOException {
2905+
xmlFolderToJson(xmlFolder, jsonFolder, Json.JsonStringBuilder.Step.TWO_SPACES);
2906+
}
2907+
2908+
public static void covertXmlToJson(Path path, Path sourceRoot, Path targetRoot,
2909+
Json.JsonStringBuilder.Step identStep) throws IOException {
2910+
Path relativePath = sourceRoot.relativize(path);
2911+
String fileName = relativePath.getFileName().toString();
2912+
String xmlFileName;
2913+
if (fileName.endsWith(".xml")) {
2914+
xmlFileName = fileName.substring(0, fileName.length() - 4) + ".json";
2915+
} else {
2916+
return;
2917+
}
2918+
Path targetPath = targetRoot.resolve(relativePath).getParent().resolve(xmlFileName);
2919+
Files.createDirectories(targetPath.getParent());
2920+
fileXmlToJson(path.toAbsolutePath().toString(), targetPath.toString(), identStep);
2921+
}
2922+
28902923
public static void streamJsonToXml(
28912924
InputStream jsonInputStream,
28922925
OutputStream xmlOutputStream,

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,29 @@ void testJsonFolderToXml(@TempDir Path tempDir) throws IOException {
12201220
"Should write XML using UTF-8 when #encoding key not present");
12211221
}
12221222

1223+
@Test
1224+
void testXmlFolderToJson(@TempDir Path tempDir) throws IOException {
1225+
// Arrange
1226+
Path xmlFile = tempDir.resolve("in.xml");
1227+
Path jsonFile = tempDir.resolve("in.json");
1228+
String xmlText = "<a>1</a>";
1229+
Files.write(xmlFile, xmlText.getBytes(StandardCharsets.UTF_8));
1230+
Files.write(jsonFile, xmlText.getBytes(StandardCharsets.UTF_8));
1231+
// Act
1232+
U.xmlFolderToJson(
1233+
xmlFile.getParent().toString(), jsonFile.getParent().toString());
1234+
// Assert
1235+
byte[] jsonBytes = Files.readAllBytes(jsonFile);
1236+
String jsonStr = new String(jsonBytes, StandardCharsets.UTF_8);
1237+
assertEquals("{"
1238+
+ System.lineSeparator()
1239+
+ " \"a\": \"1\","
1240+
+ System.lineSeparator()
1241+
+ " \"#omit-xml-declaration\": \"yes\""
1242+
+ System.lineSeparator()
1243+
+ "}", jsonStr, "Should write JSON using UTF-8");
1244+
}
1245+
12231246
@Test
12241247
void testListResult(@TempDir Path tempDir) throws IOException {
12251248
// Arrange

0 commit comments

Comments
 (0)