如何将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","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","minProperties": 0,"title": "The Items Schema","description": "An explanation about the purpose of this instance.","default": {},"examples": [
            {
                "code": "AAPL.US","timestamp": 1584388800.0,"gmtoffset": 0.0,"volume": 80605865.0,"low": 240.0,"change_p": -12.865
            },{
                "volume": 80605865.0,"change_p": -12.865,"code": "Abpl.US","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","minLength": 0,"title": "The Code Schema","default": "","examples": [
                    "AAPL.US"
                ]
            },"timestamp": {
                "$id": "#/items/properties/timestamp","type": "integer","title": "The Timestamp Schema","default": 0,"examples": [
                    1584388800
                ]
            },"gmtoffset": {
                "$id": "#/items/properties/gmtoffset","title": "The Gmtoffset Schema","examples": [
                    0
                ]
            },"open": {
                "$id": "#/items/properties/open","type": "number","title": "The Open Schema","examples": [
                    241.95
                ]
            },"high": {
                "$id": "#/items/properties/high","title": "The High Schema","examples": [
                    259.08
                ]
            },"low": {
                "$id": "#/items/properties/low","title": "The Low Schema","examples": [
                    240
                ]
            },"close": {
                "$id": "#/items/properties/close","title": "The Close Schema","examples": [
                    242.21
                ]
            },"volume": {
                "$id": "#/items/properties/volume","title": "The Volume Schema","examples": [
                    80605865
                ]
            },"previousClose": {
                "$id": "#/items/properties/previousClose","title": "The Previousclose Schema","examples": [
                    277.97
                ]
            },"change": {
                "$id": "#/items/properties/change","title": "The Change Schema","examples": [
                    -35.76
                ]
            },"change_p": {
                "$id": "#/items/properties/change_p","title": "The Change_p Schema","examples": [
                    -12.865
                ]
            }
        }
    }
}

清单3:相应的ReasonmL类型。

 type position = {
    symbol: string,holding: int,pprice: float,};

  type account = {
    name: string,max_ind_holding: float,pos: list(position),};
qaz1321 回答:如何将JSON模式转换为OCaml / ReasonML类型

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

无论如何,我可以推荐 2个半解,也许可以总计为一个半解:

1。 graphql-codegen 正如您提到的GraphQL,将schema-ast-pluginreason-plugin结合使用,您可以从JSON模式转换为ReasonML类型。 (请参阅最后的注释)

2。 quicktype ,它可以为多种语言生成类型。不幸的是,ReasonML仍然缺失。但是您可以在这里找到有关添加新语言的进度和说明:Language request: OCaml / Reason

3。将JSON的1&2作为起点

您可以尝试使用graphql-codegen-typescript从JSON转换为Typescript(或graphql-codegen支持的任何其他语言),然后使用bucklescript 7's new feature to compile records directly into JS-Objects将Typescript(或语言x)转换为GraphQL,最后从Typescript转换为ReasonML:)

我知道,这都不方便,而且涉及很多阅读/测试,但是我希望您发现它对您有所帮助,并且至少接近您的目标。

注意:如果要使用扣脚本7编译为JS : graphql-codegen-reason暂时未更新。因此,它没有利用https://youtu.be/a5b0KaITPTo的优势。相反,它使用Js.t,我觉得这是不必要和烦人的,因为您可以使用更多的干净类型。我试图进行更改,但是我对ReasonML仍然经验不足,因此它仅适用于特定情况,并且充满了错误。因此,不会在任何地方发布。但是,如果您有兴趣,我可以推动它(您无法猜测:)不能保证。请让我知道。

本文链接:https://www.f2er.com/2634035.html

大家都在问