Java中的JSONSchema解析和处理 [英] JSONSchema parsing and processing in Java

查看:2036
本文介绍了Java中的JSONSchema解析和处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个完美的.NET库Json.NET Schema.我在C#应用程序中使用它来解析模式,并使用对"name_of_simple_element"-"simple_element"对创建Dictionary<string, JSchema>.然后,我处理每一对,例如尝试查找模式为[[a-z]"的"string"类型元素或maximumLength> 300的"string"元素. 现在,我应该使用Java创建具有相同功能的应用程序.在C#中非常简单:

There is a perfect .NET library Json.NET Schema. I use it in my C# application to parse schemas and make a Dictionary<string, JSchema> with pairs "name_of_simple_element" - "simple_element". Then I process each pair and for example try to find "string" type elements with pattern "[a-z]" or "string" elements with maximumLength > 300. Now I should create application with same functions in Java. It is very simple in C#:

Jschema schema = JSchema.Parse(string json);
IDictionary<string, JSchema> dict = schema.Properties; 
... etc.

但是我找不到在Java中执行相同方法的方法.我需要转换

But i cant find same way to do that in Java. I need to convert this

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://iitrust.ru",
    "type": "object",
    "properties": {
        "regions": {
            "id": "...regions",
            "type": "array",
            "items": {
                "id": "http://iitrust.ru/regions/0",
                "type": "object",
                "properties": {
                    "id": {
                        "id": "...id",
                        "type": "string",
                        "pattern": "^[0-9]+$",
                        "description": "Идентификатор региона"
                    },
                    "name": {
                        "id": "...name",
                        "type": "string",
                        "maxLength": 255,
                        "description": "Наименование региона"
                    },
                    "code": {
                        "id": "...code",
                        "type": "string",
                        "pattern": "^[0-9]{1,3}$",
                        "description": "Код региона"
                    }
                },
                "additionalProperties": false,
                "required": ["id",
                "name",
                "code"]
            }
        }
    },
    "additionalProperties": false,
    "required": ["regions"]
}

像这样伪造字典/地图

["...id" : "id": { ... };
"...name" : "name": { ... };
"...code":  "code": { ... }]

做到这一点的最佳方法是什么?

What is the best way to do that?

推荐答案

好,问题由Dictionary<string, Jschema>,我得到了Java的HashMap<String, JsonNode>.

Ok, problem is resolved by Jackson library. Code below is based on generally accepted rule that JSON Schema object is always has a "properties" element, "array" node is always has a "items" element, "id" is always unique. This is my customer's standart format. Instead of a C#'s Dictionary<string, Jschema> I have got a Java's HashMap<String, JsonNode>.

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

...

static Map<String, JsonNode> elementsMap = new HashMap<>();

public static void Execute(File file) {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(file);
    JsonNode rootNode = root.path("properties");
    FillTheElementMap(rootNode);
}

private static void FillTheElementMap(JsonNode rootNode) {
    for (JsonNode cNode : rootNode){
        if(cNode.path("type").toString().toLowerCase().contains("array")){
            for(JsonNode ccNode : cNode.path("items")){
                FillTheElementMap(ccNode);
            }
        }
        else if(cNode.path("type").toString().toLowerCase().contains("object")){
            FillTheElementMap(cNode.path("properties");
        }
        else{
            elementsMap.put(cNode.path("id").asText(), cNode);
        }
    }

这篇关于Java中的JSONSchema解析和处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆