Имеется файл JSON, он был преобразован в JSON -схему и определение схемы в типе ReasonML. Пожалуйста, помогите прокомментировать следующие вопросы.
- Можно ли использовать схему JSON в качестве определения таблицы / схемы? Кажется, это де-факто стандарт для определения таблиц / схем. Если это не предложенный метод, предложите мне метод решения этой проблемы.
- Можно ли определить тип динамически? Я бы сохранил все определения таблиц / схем и данные в одном файле JSON. Когда я декодирую файл JSON, все определения и данные должны быть определены как тип в OCaml / ReasonML одновременно. Есть альтернатива, что я могу преобразовать все таблицы / схемы в тип путем кодирования, но я бы сделал их максимально гибкими.
Спасибо.
Список 1: Акции. 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 Схема была сгенерирована из Списка 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),
};