json_schema_import

Converts JSON schema definitions to nim types

Pure Nim score 15/100 · tests present · no docs generated

Summary

Latest Version 0.4.0
License MIT
CI Status Failing
Downloads 0
Last Indexed 2026-07-21 05:26

Tags

Authors

  • Nycto

Installation

nimble install json_schema_import
choosenim install json_schema_import
git clone https://github.com/Nycto/NimJsonSchemaImporter

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
json_schema_import - - - - - - -

Dependencies

Package Version Optional
nim >= 2.0.0 No
regex >= 0.26.1 No

Source

Repository https://github.com/Nycto/NimJsonSchemaImporter
Homepage https://github.com/Nycto/NimJsonSchemaImporter
Registry Source nimble_official

README

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:

```json address.schema.json { "$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:

```nim basic.nim
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:

```nim typePrefix.nim 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:

```nim unions.nim
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.