Commit 63fb38d2 authored by 王品堯's avatar 王品堯

check json files

parent c183e3b8
# Created by .ignore support plugin (hsz.mobi)
JsonFileCompare.iml
target/
# JsonFileCompare
### Find missing key in Target json file
```
mvn package
```
可以在target下找到JsonFileCompare-exec.jar
```java
java -cp JsonFileCompare-exec.jar JsonFileCompare /xxx/locale-zh_TW.json /xxx/locale-zh_CN.json /xxx/locale-en_US.json
```
P.S 第一個參數為基準的json, 後面為要找出缺少key的目標檔案
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.ylhealth</groupId>
<artifactId>JsonFileCompare</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- any other plugins -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>JsonFileCompare-exec</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
</project>
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class JsonFileCompare {
public static void main(String[] args) {
try {
ObjectMapper objectMapper = new ObjectMapper();
Path originPath = Paths.get(args[0]);
JsonNode twJson = objectMapper.readTree(new String(Files.readAllBytes(originPath)));
Set<String> originSet = new HashSet<>();
addKeys("", twJson, originSet, new ArrayList<>());
IntStream.range(1, args.length).forEach(i -> findMissingKeys(Paths.get(args[i]), originSet));
} catch (Exception e) {
System.err.println(args[0] + " is not exists or not json files.");
System.exit(1);
}
}
public static void addKeys(String currentPath, JsonNode jsonNode, Set set, List<Integer> suffix) {
if (jsonNode.isObject()) {
ObjectNode objectNode = (ObjectNode) jsonNode;
Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
String pathPrefix = currentPath.isEmpty() ? "" : currentPath + "-";
while (iter.hasNext()) {
Map.Entry<String, JsonNode> entry = iter.next();
addKeys(pathPrefix + entry.getKey(), entry.getValue(), set, suffix);
}
} else if (jsonNode.isArray()) {
ArrayNode arrayNode = (ArrayNode) jsonNode;
for (int i = 0; i < arrayNode.size(); i++) {
suffix.add(i + 1);
addKeys(currentPath, arrayNode.get(i), set, suffix);
if (i + 1 < arrayNode.size()) {
suffix.remove(arrayNode.size() - 1);
}
}
} else if (jsonNode.isValueNode()) {
if (currentPath.contains("-")) {
for (int i = 0; i < suffix.size(); i++) {
currentPath += "-" + suffix.get(i);
}
}
set.add(currentPath);
}
}
public static void findMissingKeys(Path targetPath, Set<String> originSet) {
if (!targetPath.toFile().exists()) return;
try {
Set<String> targetSet = new HashSet<>();
ObjectMapper objectMapper = new ObjectMapper();
JsonNode json = objectMapper.readTree(new String(Files.readAllBytes(targetPath)));
addKeys("", json, targetSet, new ArrayList<>());
Set<String> missingKey =
originSet
.parallelStream()
.filter(key -> !targetSet.contains(key))
.collect(Collectors.toSet());
if (!missingKey.isEmpty()) {
System.err.println("Missing Key on " + targetPath.toString());
System.err.println("Missing Keys: ");
missingKey.stream().sorted().forEach(System.err::println);
System.exit(1);
}
} catch (Exception e) {
System.err.println(targetPath.toString() + " is not json files.");
System.exit(1);
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment