diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index 96284906..2b2cd979 100644 --- a/src/main/java/com/github/underscore/U.java +++ b/src/main/java/com/github/underscore/U.java @@ -2887,6 +2887,39 @@ public static void covertJsonToXml(Path path, Path sourceRoot, Path targetRoot, fileJsonToXml(path.toAbsolutePath().toString(), targetPath.toString(), identStep); } + public static void xmlFolderToJson( + String xmlFolder, String jsonFolder, Json.JsonStringBuilder.Step identStep) + throws IOException { + Path sourceRoot = Paths.get(xmlFolder); + Path targetRoot = Paths.get(jsonFolder); + Files.walkFileTree(sourceRoot, new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { + covertXmlToJson(path, sourceRoot, targetRoot, identStep); + return FileVisitResult.CONTINUE; + } + }); + } + + public static void xmlFolderToJson(String xmlFolder, String jsonFolder) throws IOException { + xmlFolderToJson(xmlFolder, jsonFolder, Json.JsonStringBuilder.Step.TWO_SPACES); + } + + public static void covertXmlToJson(Path path, Path sourceRoot, Path targetRoot, + Json.JsonStringBuilder.Step identStep) throws IOException { + Path relativePath = sourceRoot.relativize(path); + String fileName = relativePath.getFileName().toString(); + String xmlFileName; + if (fileName.endsWith(".xml")) { + xmlFileName = fileName.substring(0, fileName.length() - 4) + ".json"; + } else { + return; + } + Path targetPath = targetRoot.resolve(relativePath).getParent().resolve(xmlFileName); + Files.createDirectories(targetPath.getParent()); + fileXmlToJson(path.toAbsolutePath().toString(), targetPath.toString(), identStep); + } + public static void streamJsonToXml( InputStream jsonInputStream, OutputStream xmlOutputStream, diff --git a/src/test/java/com/github/underscore/UnderscoreTest.java b/src/test/java/com/github/underscore/UnderscoreTest.java index f1080e05..91a51bf9 100644 --- a/src/test/java/com/github/underscore/UnderscoreTest.java +++ b/src/test/java/com/github/underscore/UnderscoreTest.java @@ -1220,6 +1220,29 @@ void testJsonFolderToXml(@TempDir Path tempDir) throws IOException { "Should write XML using UTF-8 when #encoding key not present"); } + @Test + void testXmlFolderToJson(@TempDir Path tempDir) throws IOException { + // Arrange + Path xmlFile = tempDir.resolve("in.xml"); + Path jsonFile = tempDir.resolve("in.json"); + String xmlText = "1"; + Files.write(xmlFile, xmlText.getBytes(StandardCharsets.UTF_8)); + Files.write(jsonFile, xmlText.getBytes(StandardCharsets.UTF_8)); + // Act + U.xmlFolderToJson( + xmlFile.getParent().toString(), jsonFile.getParent().toString()); + // Assert + byte[] jsonBytes = Files.readAllBytes(jsonFile); + String jsonStr = new String(jsonBytes, StandardCharsets.UTF_8); + assertEquals("{" + + System.lineSeparator() + + " \"a\": \"1\"," + + System.lineSeparator() + + " \"#omit-xml-declaration\": \"yes\"" + + System.lineSeparator() + + "}", jsonStr, "Should write JSON using UTF-8"); + } + @Test void testListResult(@TempDir Path tempDir) throws IOException { // Arrange