Skip to main content

Auto-generate plain old object models + custom JSON serialization/deserialization code from JSON schema definitions.

Project description


Js2model makes it easy to use JSON data as plain old Objective C, C++, or Python objects.

JSON is as a data protocol is simple, convenient, ubiquitous. When working in Javascript, a valid JSON string can be simply eval’ed to yield a native data instance. When working in other languages, things are not as simple. The usual approach is to use a general language-specific JSON parser which yields a generalized data structure composed of dictionaries, arrays, string and numbers. You can use these general structures, at the cost of decreased efficiency, no type safety, harder code maintainability, etc. You can regain efficiency, type safety, etc by hand rolling some post-processing logic to load the data into native, custom class instances - a task that is usually tedious, bug prone, and hard to maintain.

Js2model addresses the aforementioned shortcomings of typical JSON deserialization approaches by automatically generating model classes and serialization/deserialization code. Js2model uses the JSON Schema definition of your JSON data to generate source files which you include and build with the rest of your project.


The script is designed to target multiple output languages. Objective-C and C++ are supported out of the box. Future versions will add additional languages, as well as support for adding your own language templates.


You can review the documentation here

JSON Schema

Chances are you do not have existing JSON Schema definitions for your JSON data. Theoretically, creating a schema is simply a matter of creating JSON file per the JSON Schema specifications. Realistically, creating a schema definition manually can be tedious and time intensive, depending on the complexity of your data. Luckily, there is a better way. GenSON is a script that will automatically generate JSON Schema from one or more JSON files. The resulting schema definitions may need some tweaking, but GenSON will save you considerable time and effort.


usage: js2model [-h] [-l LANG] [--prefix PREFIX] [--namespace NAMESPACE]
                [--rootname ROOTNAME] [--novalidate] [-o OUTPUT]
                [--implements IMPLEMENTS] [--super SUPER]
                [--import IMPORT_FILES] [-v] [--no-deserialize]
                FILES [FILES ...]

Generate native data models from JSON Schema definitions.

positional arguments:
    FILES                 JSON Schema files for model generation

optional arguments:
    -h, --help            show this help message and exit
    -l LANG, --lang LANG  Target language. Supported values: objc, cpp (default:
    --prefix PREFIX       prefix for class names (default: TR)
    --namespace NAMESPACE
                        parent namespace for generated code (default: tr)
    --rootname ROOTNAME   Class name for root schema object (default: base name
                        of file)
    --novalidate          Skip schema validation
    -o OUTPUT, --output OUTPUT
                        Target directory of output files
    --implements IMPLEMENTS
                        Comma separated list of interface(s)|protocol(s)
                        supported by the generated classes
    --super SUPER         Comma separated list of super classes. Generated
                        classes inherit these
    --import IMPORT_FILES
                        Comma separated list of files to @import for Objective
                        C source files.
    -v, --verbose         Print actions to STDOUT.
    --no-deserialize      Do not generate deserialization code. Implies --no-
    --no-dependencies     Do not include dependencies in output.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for js2model, version 0.3.dev11
Filename, size File type Python version Upload date Hashes
Filename, size js2model-0.3.dev11.tar.gz (89.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page