如何将JSON模式转换为OCaml/ReasonML类型



有一个JSON文件,它已被转换为JSON模式和ReasonML类型的模式定义。请帮助评论以下问题。

  1. 使用JSON模式作为表/模式定义是个好选择吗?定义表/模式似乎是事实上的标准。如果不是建议的方法,请给我一个解决的方法这个问题
  2. 是否可以动态定义类型?我会将所有的表/模式定义和数据保存到一个JSON文件中。当我解码JSON文件时,所有定义和数据都应该同时定义为OCaml/ReasonML中的类型。还有一种选择,我可以通过编码将所有表/模式转换为类型,但我会尽可能灵活

谢谢。

列表1:Stocks.json

[
{
"code": "AAPL.US",
"timestamp": 1584388800,
"gmtoffset": 0,
"open": 241.95,
"high": 259.08,
"low": 240,
"close": 242.21,
"volume": 80605865,
"previousClose": 277.97,
"change": -35.76,
"change_p": -12.865
},
{
"code": "ABPL.US",
"timestamp": 1584388800,
"gmtoffset": 0,
"open": 241.95,
"high": 259.08,
"low": 240,
"close": 242.21,
"volume": 80605865,
"previousClose": 277.97,
"change": -35.76,
"change_p": -12.865
}
]

列表2:根据json-schema.org的标准,json schema是从列表1生成的。

{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "array",
"readOnly": false,
"writeOnly": false,
"uniqueItems": false,
"minItems": 0,
"minContains": 1,
"title": "The Root Schema",
"description": "The root schema comprises the entire JSON document.",
"additionalItems": true,
"items": {
"$id": "#/items",
"type": "object",
"readOnly": false,
"writeOnly": false,
"minProperties": 0,
"title": "The Items Schema",
"description": "An explanation about the purpose of this instance.",
"default": {},
"examples": [
{
"code": "AAPL.US",
"previousClose": 277.97,
"timestamp": 1584388800.0,
"change": -35.76,
"close": 242.21,
"open": 241.95,
"gmtoffset": 0.0,
"volume": 80605865.0,
"low": 240.0,
"high": 259.08,
"change_p": -12.865
},
{
"volume": 80605865.0,
"low": 240.0,
"high": 259.08,
"change_p": -12.865,
"previousClose": 277.97,
"code": "ABPL.US",
"timestamp": 1584388800.0,
"open": 241.95,
"close": 242.21,
"change": -35.76,
"gmtoffset": 0.0
}
],
"additionalProperties": true,
"required": [
"code",
"timestamp",
"gmtoffset",
"open",
"high",
"low",
"close",
"volume",
"previousClose",
"change",
"change_p"
],
"properties": {
"code": {
"$id": "#/items/properties/code",
"type": "string",
"readOnly": false,
"writeOnly": false,
"minLength": 0,
"title": "The Code Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"AAPL.US"
]
},
"timestamp": {
"$id": "#/items/properties/timestamp",
"type": "integer",
"readOnly": false,
"writeOnly": false,
"title": "The Timestamp Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
1584388800
]
},
"gmtoffset": {
"$id": "#/items/properties/gmtoffset",
"type": "integer",
"readOnly": false,
"writeOnly": false,
"title": "The Gmtoffset Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
0
]
},
"open": {
"$id": "#/items/properties/open",
"type": "number",
"readOnly": false,
"writeOnly": false,
"title": "The Open Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
241.95
]
},
"high": {
"$id": "#/items/properties/high",
"type": "number",
"readOnly": false,
"writeOnly": false,
"title": "The High Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
259.08
]
},
"low": {
"$id": "#/items/properties/low",
"type": "integer",
"readOnly": false,
"writeOnly": false,
"title": "The Low Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
240
]
},
"close": {
"$id": "#/items/properties/close",
"type": "number",
"readOnly": false,
"writeOnly": false,
"title": "The Close Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
242.21
]
},
"volume": {
"$id": "#/items/properties/volume",
"type": "integer",
"readOnly": false,
"writeOnly": false,
"title": "The Volume Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
80605865
]
},
"previousClose": {
"$id": "#/items/properties/previousClose",
"type": "number",
"readOnly": false,
"writeOnly": false,
"title": "The Previousclose Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
277.97
]
},
"change": {
"$id": "#/items/properties/change",
"type": "number",
"readOnly": false,
"writeOnly": false,
"title": "The Change Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
-35.76
]
},
"change_p": {
"$id": "#/items/properties/change_p",
"type": "number",
"readOnly": false,
"writeOnly": false,
"title": "The Change_p Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
-12.865
]
}
}
}
}

列表3:对应的ReasonML类型

type position = {
symbol: string,
holding: int,
pprice: float,
};
type account = {
name: string,
max_ind_holding: float,
pos: list(position),
};

我也尝试过类似的事情,但没有找到tl;dr解决方案。所以,我不能给你一个。

无论如何,我可以推荐两个半解决方案,也许加起来就是一个完整的解决方案:

1.graphql codegen正如您提到的graphql,使用schema ast插件和reason插件,您可以从JSON模式转到ReasonML类型。(见末尾注释(

2.quicktype它可以为许多语言生成类型。不幸的是,ReasonML仍然缺失。但你可以在这里找到如何添加新语言的进度和说明:语言请求:OCaml/Reason

3.合并1&2用于JSON作为起点

您可以尝试从JSON到typescript(或graphql-codegen支持的任何其他语言(,然后从typescript(或者x语言(到使用graphql-code的graphql typescript,最后从typescript到ReasonML:(

我知道,这些都不方便,而且需要大量的阅读/测试,但我希望你觉得这很有帮助,至少能更接近你的目标

注意:如果使用bucklescript 7编译到JS:graphql代码生成原因有一段时间没有更新。因此,它没有利用Bucklescript7的新特性将记录直接编译到JS对象中。相反,它使用Js.t,我觉得这是不必要的,也很烦人,因为你可以有更干净的类型。我试图更改它,但我在ReasonML方面仍然太缺乏经验,所以它只适用于特定的情况,并且充满了错误。因此,没有在任何地方发表。如果你仍然感兴趣,我可以推它(正如你所猜测的,没有保证:(。请告诉我

最新更新