4

Is there is a way to convert the JSON schema to Flatbuffer schema? The use case here is user can create the JSON schema but I am finding a way to convert the JSON schema to Flatbuffer.

Example:Input

 {
  "$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"
}

Output:

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;

I knew the reverse way is available. But i don't know how to convert the JSON schema to flatbuffer schema

Syed Abdul Kather
  • 608
  • 2
  • 12
  • 29

1 Answers1

0

I have put together a naive converter, which I use with genson output with varying degrees of success. It can be a good starting point though: https://gist.github.com/romanbsd/da181151170e396e8e36a6576f045aa2

Roman
  • 13,100
  • 2
  • 47
  • 63