Skip to content

Added method U.xmlFolderToJson(xmlFolder, jsonFolder, identStep) #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2025
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
33 changes: 33 additions & 0 deletions src/main/java/com/github/underscore/U.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/github/underscore/UnderscoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<a>1</a>";
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
Expand Down
Loading