5

我有以下 json 数据对象:

{
    "name": "John",
    "favorite_number": 5,
    "favorite_color" : "green"
}

此对象的 JSON 模式如下所示:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Person",
    "description": "some description",
    "type": "object",
    "properties": {
        "name": {
            "description": "name",
            "type": "string"
        },
        "favorite_number": {
            "type": "number",
        },
        "favorite_color": {
            "type": "string",
        },
    },
    "required": ["name", "favorite_number","favorite_color"]
}

我可以使用这个 JSON 模式来验证我的数据对象是否符合它:

public static boolean isJsonValid(String schemaText, String jsonText) throws ProcessingException, IOException
    {   
        final JsonSchema schemaNode = getSchemaNode(schemaText);
        final JsonNode jsonNode = getJsonNode(jsonText);
        return isJsonValid(schemaNode, jsonNode);
    } 

在我的 java 应用程序中,我从 REST API 调用中接收到该对象的相应 AVRO 模式,该模式如下所示:

{
 "namespace": "example.avro",
 "type": "record",
 "name": "Person",
 "fields": [
     {"name": "name", "type": "string"},
     {"name": "favorite_number",  "type": ["int", "null"]},
     {"name": "favorite_color", "type": ["string", "null"]}
 ]
}

是否有一种普遍可接受的方式将此类 AVRO 模式转换为 JSON 模式?

4

1 回答 1

-3
  1. 下载:(或来自存储库的avro-tools-1.7.4.jar最新版本)
  2. 跑:java -jar avro-tools-1.7.4.jar tojson avro-filename.avro>output-filename.json

这将create output-filename.json包含所有数据。如果output-filename.json已经存在,它将覆盖它。

于 2018-01-09T16:14:12.777 回答