如何将JSON模式转换为OCaml/ReasonML类型
有一个JSON文件,它已转换为JSON模式和ReasonML类型的模式定义。 请帮助评论以下问题如何将JSON模式转换为OCaml/ReasonML类型,json,ocaml,reason,Json,Ocaml,Reason,有一个JSON文件,它已转换为JSON模式和ReasonML类型的模式定义。 请帮助评论以下问题 使用JSON模式作为表/模式定义是一个好选择吗? 定义表/模式似乎是事实上的标准。如果是 不是建议的方法,请给我建议一种解决方法 这个问题 可以动态定义类型吗?我会将所有表/模式定义和数据保存到一个JSON文件中。当我解码JSON文件时,所有定义和数据都应该在OCaml/ReasonML中同时定义为type。有一种替代方法,我可以通过编码将所有表/模式转换为类型,但我会使它们尽可能灵活 多谢各位 列
[
{
"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模式是从列表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
]
}
}
}
}
type position = {
symbol: string,
holding: int,
pprice: float,
};
type account = {
name: string,
max_ind_holding: float,
pos: list(position),
};
列表3:对应的ReasonML类型。
{
"$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
]
}
}
}
}
type position = {
symbol: string,
holding: int,
pprice: float,
};
type account = {
name: string,
max_ind_holding: float,
pos: list(position),
};
我也尝试过类似的方法,但没有找到tl;dr解决方案。所以,我不能提供给你 无论如何,我可以推荐两个半解决方案,可能加起来就是一个完整的解决方案: 1正如您提到的GraphQL,与一起使用可以从JSON模式转换为ReasonML类型。(见最后的注释) 2可以为许多语言生成类型。不幸的是,ReasonML仍然缺失。但您可以在此处找到有关如何添加新语言的进度和说明: 3。结合JSON的1和2作为起点 您可以尝试使用从JSON到typescript(或graphql codegen支持的任何其他语言),然后使用typescript(或语言x)到graphql,最后使用从typescript到ReasonML:) 我知道,所有这些都不方便,而且需要大量阅读/测试,但我希望你能发现它们对你有帮助,至少能更接近你的目标 注意:如果您使用bucklescript 7编译到JS:
graphql codegen原因暂时未更新。所以它没有利用。相反,它使用Js.t,我觉得这是不必要的,而且很烦人,因为您可以使用更干净的类型。我试图改变它,但我在ReasonML方面仍然太缺乏经验,所以它只适用于特定的情况,并且充满了bug。因此,没有出版任何地方。如果你感兴趣的话,我可以推它(你猜不出有什么保证:))。请让我知道。“当我解码JSON文件时,所有定义和数据都应该同时在OCaml/ReasonML中定义为类型”-您可以添加一个JSON模式的示例以及您想要从框中取出的相应类型定义吗?@KonstantinStrukov,谢谢您的评论。列表1-列表3是供您参考的示例。列表2由列表1生成,列表3是对应的类型定义。请随意评论。@KonstantinStrukov,我发现GraphQL更受欢迎。有一个在线页面提供将JSON转换为GraphQL模式的服务。“谢谢你的帮助。我发现闭包查询数据比SQL更自然,所以我想从闭包(ReasonML/OCaml)的角度实现一个简单的数据库。据我所知,MongoDB JSON格式能够将数据导出到Oracle NoSQL中,它可能并不完全相同,但一些基本原则是相似的(将模式导入/导出到JSON、解析JSON和创建模式等)。我不是一个经验丰富的函数式程序员,还有很多事情我不知道。请给我一些意见和建议。谢谢。@farkg有一个链接供您参考“”