json模式:如何从一个json模式转换为另一个 [英] json-schema: how to transform from one json-schema to another

查看:234
本文介绍了json模式:如何从一个json模式转换为另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的 json-方案:

schemaA->在 http://json-schema.org/calendar 中定义的日历

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description": "A representation of an event",
    "type": "object",
    "required": [ "dtstart", "summary" ],
    "properties": {
        "dtstart": {
            "format": "date-time",
            "type": "string",
            "description": "Event starting time"
        },
        "dtend": {
            "format": "date-time",
            "type": "string",
            "description": "Event ending time"
        },
        "summary": { "type": "string" },
        "location": { "type": "string" },
        "url": { "type": "string", "format": "uri" },
        "duration": {
            "format": "time",
            "type": "string",
            "description": "Event duration"
        },
        "rdate": {
            "format": "date-time",
            "type": "string",
            "description": "Recurrence date"
        },
        "rrule": {
            "type": "string",
            "description": "Recurrence rule"
        },
        "category": { "type": "string" },
        "description": { "type": "string" },
        "geo": { "$ref": "http: //json-schema.org/geo" }
    }
}

schemaB->另一个日历架构(也是json-schema版本draft-04)

我的问题很简单.我有一个遵循第一个模式的javascript对象"calendarA",即

validates(calendarA, schemaA); // true

我想描述模式之间的转换,即从模式A到模式B的转换,因此我可以将calendarA作为输入传递,并获得一个适合schemaB的新CalendarB.将其放在代码中:

var calendarB = fromSchemaAtoB(calendarA, schemaA, schemaB);
validates(calendarB, schemaB); // true

从您的角度来看,哪种是从SchemaAtoB专家那里编写的最佳方法/工具?我真的很想使用模式来描述变形,例如:

schemaB.properties.foo = schemaA.properties.dtstart

我看到了很多基本的json转换包,但在我看来,大多数它们都将输出指定为不考虑架构的外部模板(因此,对于schemaB,结果可能无效). /p>

非常感谢您!

JG

PS:如果可能的话,我更喜欢基于javascript的解决方案,但我真的对任何可能性持开放态度.

要在阅读@jason的答案后澄清一下,问题是如何更好地描述模式之间的这种关系以及如何应用它们来获得calendarB.因此,如果您愿意:

var calendarB = transform(calendarA, schemaA, schemaB, relationsAtoB);
validates(calendarB, schemaB); // true

然后问题是如何更好地描述"relationsAtoB"以及如何实现"transform"功能.

解决方案

我相信像 可以用来解决您的问题. 这并不能直接解决问题(从一个JSON模式转换为另一个JSON模式),但是您可以做的事情(这是我当前正在做的事情)如下:

  1. 为您的输入指定JSON模式
  2. 为输出指定JSON模式
  3. 指定映射模板(例如,使用我引用的库).

当然,理想情况下,您不必同时执行23,但是我还没有找到可以自动执行此操作的东西.因此,例如,您可以指定映射模板并创建一些库函数,该函数将其以及1中的JSON模式作为输入,并在3中生成JSON模式作为输出.

但是,这并不简单,因此目前我同时指定了23.

此外,请记住,您不能拥有12,并且会以某种方式自动生成3.这是因为存在多个映射函数,这些映射函数会将数据粘附到架构1上并产生数据粘附在架构2上.

I have the two different json-schemas:

schemaA -> A calendar as defined at http://json-schema.org/calendar

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description": "A representation of an event",
    "type": "object",
    "required": [ "dtstart", "summary" ],
    "properties": {
        "dtstart": {
            "format": "date-time",
            "type": "string",
            "description": "Event starting time"
        },
        "dtend": {
            "format": "date-time",
            "type": "string",
            "description": "Event ending time"
        },
        "summary": { "type": "string" },
        "location": { "type": "string" },
        "url": { "type": "string", "format": "uri" },
        "duration": {
            "format": "time",
            "type": "string",
            "description": "Event duration"
        },
        "rdate": {
            "format": "date-time",
            "type": "string",
            "description": "Recurrence date"
        },
        "rrule": {
            "type": "string",
            "description": "Recurrence rule"
        },
        "category": { "type": "string" },
        "description": { "type": "string" },
        "geo": { "$ref": "http: //json-schema.org/geo" }
    }
}

schemaB -> Another calendar schema (also json-schema version draft-04)

My quesiton is simple. I have a javascript object 'calendarA' that follows the first schema, i.e.,

validates(calendarA, schemaA); // true

I want to describe a transformation between the schemas, i.e., from schemaA to schemaB, so I can pass calendarA as input and get a new calendarB that fits schemaB. Put it in code:

var calendarB = fromSchemaAtoB(calendarA, schemaA, schemaB);
validates(calendarB, schemaB); // true

From your point of view which is the best approach/tools to write fromSchemaAtoB guys? I really want to describe transfomations using the schemas, something like:

schemaB.properties.foo = schemaA.properties.dtstart

I saw a lot of basic json transformation packages but it seems to me that in most of them you specify your output as external templates that do not take into account the schemas (so result could be invalid with respect to schemaB).

Thank you so much in advance!!

JG

PS: I prefer javascript based solutions if possible but I am really open to any possibility.

EDIT 1: To clarify after reading @jason's answer, the question is how to better describe such relations between schemas and how to apply them to obtain calendarB. So if you prefer:

var calendarB = transform(calendarA, schemaA, schemaB, relationsAtoB);
validates(calendarB, schemaB); // true

and the question then is how to better describe "relationsAtoB" and how to implement the "transform" function.

解决方案

I believe a library like this could be used to address your question. This does not directly address the question (transforming from one JSON schema to the other) but what you can do (which is what I am currently doing) is the following:

  1. Specify a JSON schema for your input
  2. Specify a JSON schema for your output
  3. Specify a mapping template (e.g. using the library I referenced).

Of course, ideally you would not have to do both 2 and 3 but I have not found something which does this automatically. So, for example, you could specify the mapping template and create some library function which takes that as well as the JSON schema in 1 as its inputs, and would generate the JSON schema in 3 as its output.

This, however, is not trivial so currently I am specifying both 2 and 3.

Also, keep in mind that you cannot have 1 and 2 and somehow automatically generate 3. This is because there are more than one mapping functions that would take data adhering to schema 1 and produce data adhering to schema 2.

这篇关于json模式:如何从一个json模式转换为另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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