Skip to main content

JSON-Schema Codegen

This python library consumes JSON-Schema and generates C++ or Python code. It generates structures to hold the values defined in the schema, restricting the values according to the schema.

Python Requirements for Code Generation

These requirements should be satisfied when pip3 installing json-schema-codegen.

  • python 3.7
  • jinja2
  • stringcase

Installation

pip3 install json-schema-codegen

C++ Generated Code

Supported Schema Features in C++ code generation

A C++ class is generated for each schema node according to the schema's type property. Schemas without a type property, with the exception of combining operators *Of, are not supported.

  • type: string
    • minLength
    • maxLength
    • pattern
    • format=date-time (enforces ISO8601 format)
    • format=uuid (enables string object to be populated with a uuid)
  • type: string with enum
  • type: integer
    • maximum
    • minimum
    • exclusiveMaximum
    • exclusiveMinimum
    • multipleOf
  • type: number
    • maximum
    • minimum
    • exclusiveMaximum
    • exclusiveMinimum
    • multipleOf
  • type: boolean
  • type: null
  • type: array
    • items
    • minItems
    • maxItems
  • type: object
    • properties
    • required
  • allOf
  • anyOf
  • oneOf
References

$ref references are supported for array items, object properties, allOf, anyOf, and oneOf. However, the caller must provide a "resolver" class which translates the reference into a class name and namespace.

Dependencies of the C++ generated code

  • boost (boost::optional and boost::variant among others)
  • rapidjson 1.1
  • C++11

Usage

See example_usage.py for a more elaborate example on generating C++ code.

import jsonschemacodegen.cpp as cpp

simpleResolver = cpp.SimpleResolver()
output_dir = "/tmp"
    
generator = cpp.GeneratorFromSchema(src_output_dir=output_dir,
    header_output_dir=output_dir, 
    resolver=simpleResolver,
    namespace=[],
    src_usings=[])

sampleSchema = {"type": "string"}

generator.Generate(sampleSchema, 'Example', 'example')

Python Generated Code

A Python3 class is generated for each schema node; the class encapsulating the data described by the schema. The class accepts in its constructor python primative data types that match the format described the the schema. Each class has a Serializable method which returns data in a format that can be serialized.

JSON (de-)serialization does not happen in the actual class. This allows for flexibility to use other line-formats, for example, YAML.

Supported schema features for generating Python code

  • type: string
    • minLength
    • maxLength
    • pattern
    • enum
  • type: integer
    • maximum
    • minimum
    • exclusiveMaximum
    • exclusiveMinimum
    • multipleOf
    • enum
  • type: number
    • maximum
    • minimum
    • exclusiveMaximum
    • exclusiveMinimum
    • multipleOf
    • enum
  • type: boolean
  • type: null
  • type: array
    • items
    • minItems
    • maxItems
  • type: object
    • properties
    • required
  • allOf
  • anyOf
  • oneOf
    • Component schemas with the title property.

Example usage for generating Python code

For a more elaborate example, see example_python.py

from jsonschemacodegen import python as pygen
import json

with open('schema.json') as fp:
    generator = pygen.GeneratorFromSchema('output_dir')
    generator.Generate(json.load(fp), 'Example', 'example')

This example will create the file output_dir/example.py containing the Python3 class Example and nested classes as required.

Using the generated code looks like this:

import example
import json

jsonText = '["an example string in an array"]'

obj = example.Example(json.loads(jsonText))

print(json.dumps(obj, default=lambda x: x.Serializable()))

License

GPLv2

Metadata

Release files for json-schema-codegen 0.6.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-schema-codegen 0.6.3
File Size Uploaded
json-schema-codegen-0.6.3.tar.gz 17.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-schema-codegen 0.6.3
File Interpreter ABI Platform
json_schema_codegen-0.6.3-py3-none-any.whl Python 3 none any Details

Total release size: 88.7 kB

Release files / json-schema-codegen-0.6.3.tar.gz

Download URL json-schema-codegen-0.6.3.tar.gz
Size 17.5 kB
Tags Source
SHA-256 checksum
How to use checksums
16ae61c1efea79e6339be56ebd1076ed046b0611136926afb047418535debe27
BLAKE2b-256 checksum
How to use checksums
74a19aebb4a61beeab23946cad75afaacd2170aca7d936b85307f468e04ef480
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.12

Release files / json_schema_codegen-0.6.3-py3-none-any.whl

Download URL json_schema_codegen-0.6.3-py3-none-any.whl
Size 71.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cdb265ce0f6125446fa5d6c8d39975c427d0df8785107d2c325bfc46894e2122
BLAKE2b-256 checksum
How to use checksums
5642cdf9e9f756ac90d90233398a71caf89bdd6fcd12f6b8b6cb42b11cf6f4f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.12

Release history Release notifications | RSS feed

This release

0.6.3 This release

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page