编程语言
首页 > 编程语言> > java 生成jsonPath树

java 生成jsonPath树

作者:互联网

  public static void main(String[] args) throws Exception {
        generateJsonPath();
    }


public static void generateJsonPath() throws Exception {

        Map<String, Object> data = new HashMap<>();
        data.put("list", new Stu[]{new Stu("xxxx", System.currentTimeMillis())});
        data.put("obj", new Stu("cc", System.currentTimeMillis()));
        ObjectMapper mapper = new ObjectMapper();
        JsonSchemaInferrer inferrer = JsonSchemaInferrer.newBuilder()
                .setSpecVersion(SpecVersion.DRAFT_04)
                .setAdditionalPropertiesPolicy(AdditionalPropertiesPolicies.noOp())
                .setRequiredPolicy(RequiredPolicies.noOp())
                .build();
//        System.out.println(JsonUtils.toJSONString(data));
        JsonNode objectNode = inferrer.inferForSample(mapper.readTree(JsonUtils.toJSONString(data)));

        System.out.println(objectNode.toString());
        List<PathInfo> pathInfoList = new ArrayList<>();
        Map<String, PathInfo> pathInfoMap = new HashMap<>();

        PathInfo rootNode = new PathInfo(UUID.randomUUID().toString(), "-1", "$", "$", objectNode.get("type").asText());
        if ("array".equals(rootNode.getType())) {
            rootNode.setPath(rootNode.getPath() + "[]");
        }
        pathInfoList.add(rootNode);
        pathInfoMap.put(rootNode.getId(), rootNode);
        deepNode(pathInfoList, pathInfoMap, "array".equals(rootNode.getType()) ? objectNode.get("items").get("properties") : objectNode.get("properties"), rootNode.getId());
        System.out.println(JsonUtils.toJSONString(pathInfoMap));

        Map<String, List<PathInfo>> map = pathInfoList.stream().collect(Collectors.groupingBy(PathInfo::getParentId));

        pathInfoList.forEach(e -> e.setChild(map.getOrDefault(e.getId(), new ArrayList<>())));

        System.out.println(JsonUtils.toJSONString(rootNode));

    }

    public static void deepNode(List<PathInfo> pathInfoList, Map<String, PathInfo> pathInfoMap, JsonNode jsonNode, String parentId) {

        Iterator<Map.Entry<String, JsonNode>> iterator = jsonNode.fields();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonNode> nodeEntry = iterator.next();
            JsonNode node = nodeEntry.getValue();
            String prop = nodeEntry.getKey();
            System.out.println(nodeEntry);
            PathInfo pathNode = new PathInfo(UUID.randomUUID().toString(), parentId, prop, pathInfoMap.get(parentId).getPath() + "." + prop, node.get("type").asText());
            if ("array".equals(pathNode.getType())) {
                pathNode.setPath(pathNode.getPath() + "[]");
            }
            pathInfoList.add(pathNode);
            pathInfoMap.put(pathNode.getId(), pathNode);
            if (pathNode.getType().equals("object")) {
                deepNode(pathInfoList, pathInfoMap, node.get("properties"), pathNode.getId());
            }
            if (pathNode.getType().equals("array")) {
                deepNode(pathInfoList, pathInfoMap, node.get("items").get("properties"), pathNode.getId());
            }
        }

    }

    @Data
    public static class PathInfo {


        private String id;
        private String parentId;
        private String propName;
        private String path;
        private String alias;
        private String type;

        private List<PathInfo> child;

        public PathInfo() {
        }

        public PathInfo(String id, String parentId, String propName, String path, String type) {
            this.id = id;
            this.parentId = parentId;
            this.propName = propName;
            this.path = path;
            this.type = type;
        }
    }

    @Data
    public static class Stu {

        private String name;
        private Long id;

        public Stu(String name) {
            this.name = name;
        }

        public Stu(String name, Long id) {
            this.name = name;
            this.id = id;
        }

        public Stu() {
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

 

执行结果

 

 

 

 

 

直接上测试代码  依赖

<dependency>
<groupId>com.github.saasquatch</groupId>
<artifactId>json-schema-inferrer</artifactId>
<version>0.1.4</version>
</dependency>

有问题请指正

 

标签:java,String,生成,jsonPath,rootNode,pathNode,new,public,name
来源: https://www.cnblogs.com/jasonChai/p/15893705.html