No description
Find a file
2025-12-09 08:33:57 -08:00
.github/workflows Remove std/path usage to support os:any 2025-01-27 18:23:29 -08:00
.vscode Adjust vscode settings 2025-01-30 18:37:20 -08:00
src Support for const fields 2025-09-14 10:53:28 -07:00
tests Remove unused import 2025-12-09 08:33:57 -08:00
.gitignore Initial skeleton 2025-01-15 21:37:43 -08:00
json_schema_import.nimble Version 0.3.0 2025-09-14 10:57:28 -07:00
LICENSE Initial commit 2025-01-18 09:45:14 -08:00
README.md Simplify union unpacking example in readme 2025-01-26 07:57:33 -08:00

Nim Json Schema Type Importer

Build License

This is a Nim package that allows json schema documents to be directly imported into a project. The types will available, as well as helper functions for serializing and deserializing JSON.

It is intended for situations where you don't need to dynamically load the JSON schema itself. For example, if you are building a game that uses LDtk as a level editor, you could download the json schema that describes the file format and directly import it into your project. This would allow you to immediately start opening and interacting with the save files using native Nim objects.

Example

Given a simple json schema file:

{
  "$id": "/schemas/address",
  "type": "object",
  "properties": {
    "street_address": { "type": "string" },
    "city": { "type": "string" },
    "state": { "type": "string" }
  },
  "required": ["street_address", "city", "state"]
}

This can be directly imported into a nim file as follows:

import json_schema_import

importJsonSchema "address.schema.json"

let address = parseJson("""
  {
    "city":"Kingston",
    "street_address":"132 My Street",
    "state":"NY"
  }
""").jsonTo(Address)

echo "Nim object: ", address.repr

echo "Converted back to JSON: ", address.toJson

Prefixing types

If your import creates naming collisions, you can add a prefix to every generated type:

import json_schema_import

importJsonSchema "address.schema.json", "My"

let address = MyAddress(
    street_address: "132 My Street",
    city: "Kingston",
    state: "NY"
)

Packing and unpacking unions

Getters and builders are automatically generated to allow interactiction with union types:

import json_schema_import

# Schemas can be loaded from inline json blocks
jsonSchema %*{
  "$id": "/schemas/unionContainer",
  "required": [ "value" ],
  "properties": {
    "value": {
      "anyOf": [
        { "type": "string" },
        { "type": "integer" },
      ]
    }
  }
}

# Creating a union from an integer
block:
  let unioned = UnionContainer(value: forUnion(123))
  assert(unioned.value.isInt)
  echo unioned.value.asInt

# Creating a union from a string. Notice the `forUnion` call above isn't required,
# as converters are created to automatically wrap types in union objects when possible.
block:
  let unioned = UnionContainer(value: "foo")
  assert(unioned.value.isStr)
  echo unioned.value.asStr

Showing the generated types

To see the exact nim code being generated, you can add the -d:dump compile flag. It will cause the generated code to be printed during compile.