将json模式转换为flatbuffer模式

时间:2018-04-06 11:49:25

标签: flatbuffers

是否有办法将JSON架构转换为Flatbuffer架构? 这里的用例是用户可以创建JSON模式,但我找到了一种将JSON模式转换为Flatbuffer的方法。

示例:输入

 {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "MyGame_Sample_Color": {
      "type": "string",
      "enum": [
        "Red",
        "Green",
        "Blue"
      ]
    },
    "MyGame_Sample_Monster": {
      "type": "object",
      "properties": {
        "mana": {
          "type": "number"
        },
        "hp": {
          "type": "number"
        },
        "name": {
          "type": "string"
        },
        "friendly": {
          "type": "boolean"
        },
        "inventory": {
          "type": "array",
          "items": {
            "type": "number"
          }
        },
        "color": {
          "$ref": "#/definitions/MyGame_Sample_Color"
        }
      },
      "additionalProperties": false
    }
  },
  "$ref": "#/definitions/MyGame_Sample_Monster"
}

输出

namespace MyGame.Sample;
enum Color:byte { Red = 0, Green, Blue = 2 }
table Monster {
  mana:short = 150;
  hp:short = 100;
  name:string;
  friendly:bool = false (deprecated);
  inventory:[ubyte];  // Vector of scalars.
  color:Color = Blue; // Enum.
}
root_type Monster;

我知道可以采用相反的方式。但我不知道如何将JSON模式转换为flatbuffer模式

1 个答案:

答案 0 :(得分:0)

我已经组装了一个简单的转换器,我将其与 genson 输出一起使用,并取得了不同程度的成功。不过,这可能是一个很好的起点: https://gist.github.com/romanbsd/da181151170e396e8e36a6576f045aa2