JsonFileCompare.java 2.69 KB
Newer Older
王品堯's avatar
王品堯 committed
1 2 3
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
4 5
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
王品堯's avatar
王品堯 committed
6 7 8 9 10 11 12 13 14

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 {
15
  private static Logger log = LoggerFactory.getLogger(JsonFileCompare.class);
王品堯's avatar
王品堯 committed
16 17 18 19 20 21 22 23 24 25 26 27

  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) {
28
      log.error("{} is not exists or not json files.", args[0], e);
王品堯's avatar
王品堯 committed
29 30 31 32
      System.exit(1);
    }
  }

33
  private static void addKeys(String currentPath, JsonNode jsonNode, Set<String> set, List<Integer> suffix) {
王品堯's avatar
王品堯 committed
34 35 36 37 38 39 40 41 42 43 44
    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.isValueNode()) {
45
      StringBuilder sb = new StringBuilder(currentPath);
王品堯's avatar
王品堯 committed
46
      if (currentPath.contains("-")) {
47 48
        for (Integer suffix1 : suffix) {
          sb.append("-").append(suffix1);
王品堯's avatar
王品堯 committed
49 50
        }
      }
51
      set.add(sb.toString());
王品堯's avatar
王品堯 committed
52 53 54
    }
  }

55
  private static void findMissingKeys(Path targetPath, Set<String> originSet) {
王品堯's avatar
王品堯 committed
56 57 58 59 60 61 62 63 64 65 66 67 68
    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()) {
69
        log.error("Missing Key on {} \n Missing Keys: {}", targetPath, missingKey.stream().sorted().collect(Collectors.toList()));
王品堯's avatar
王品堯 committed
70 71 72
        System.exit(1);
      }
    } catch (Exception e) {
73
      log.error("{} is not json files.", targetPath, e);
王品堯's avatar
王品堯 committed
74 75 76 77
      System.exit(1);
    }
  }
}