[![Build Status](https://travis-ci.org/expobrain/json-schema-codegen.svg?branch=master)](https://travis-ci.org/expobrain/json-schema-codegen)
# json-schema-codegen
Generate code from JSON schema files.
# Table of contents
- [Introduction](#introduction)
- [Currently supported languages](#currently-supported-languages)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
- [Code generation](#code-generation)
- [Python3](#python-3)
- [Python3+Marshmallow](#python-3marshmallow)
- [JavaScript+Flow and Flow](#javascriptflow-and-flow)
- [Contribute](#contribute)
# Introduction
This is a command line tool to take a [json-schema](http://json-schema.org/) file and generate code automatically.
For instance this `json-schema` definition:
```json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Test",
"type": "object",
"properties": {
"id": { "type": "integer" }
}
}
```
will generate this Python code:
```python
class Test(object):
def __init__(self, data=None):
data = data or {}
self.id = data.get("id")
```
or this JavaScript+Flow code:
```javascript
export class Test {
id: ?number;
constructor(data: Object = {}) {
this.id = data.id;
}
}
```
Currently this tool generates code for Python and JavaScript with [Flow](https://flow.org/) annotations but it can be extended to generate code for any language.
The code generation is divided in two stages:
1. generate the [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree) for the target language from the `json-schema` file
1. convert the AST into the target language
This allows the tool to be language agnostic, that is it just needs to generate the AST in JSON format for the target language and then a language specific tool will convert this AST into proper code.
# Currently supported languages
List of currently supported languages:
- Python 3.7+
- JavaScript ES7+ with Flow annotations
- pure Flow annotations
# Requirements
- Python 3.6 / 3.7
- Node v12
# Installation
Until this [pull request](https://github.com/pypa/setuptools/pull/1389) in [`setuptools`](https://pypi.org/project/setuptools/) is fixed, the only way to install `json-schema-codegen` is to clone the repo:
```shell
git clone https://github.com/expobrain/json-schema-codegen.git
```
# Usage
```shell
usage: json_codegen.py [-h] [--prefix PREFIX] [--language LANGUAGE]
[--output OUTPUT]
schema
positional arguments:
schema Definition of the PRD as JSON schema
optional arguments:
-h, --help show this help message and exit
--prefix PREFIX, -p PREFIX
Optional prefix for generated classes
--language LANGUAGE, -l LANGUAGE
Output language. Default is python
--output OUTPUT, -o OUTPUT
Output filename for the generated code
```
# Code generation
## Python 3
The egenerator of pure Python 3 compatible code:
```shell
json_codegen --language python3 --output <output_py_file> <json-schema>
```
## Python 3+Marshmallow
The generation of Python 3's code with [Marshmallow](https://marshmallow.readthedocs.io/en/2.x-line/) support is integrated into the tool so it needs just a single invocation:
```shell
json_codegen --language python3+marshmallow --output <output_py_file> <json-schema>
```
## JavaScript+Flow and Flow
Generating JavaScript+Flow and Flow code involves two steps, generating the AST:
```shell
json_codegen --language [javascript+flow|flow] --output <output_ast_json> <json-schema>
```
and generating the code from the AST:
```shell
bin/ast_to_js <output_ast_json> <output_js_file>
```
Raw data
{
"_id": null,
"home_page": "",
"name": "json-codegen",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7,<3.8",
"maintainer_email": "",
"keywords": "python,javascript,json-schema,codegen",
"author": "Daniele Esposti",
"author_email": "daniele.esposti@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/04/f7/178db4d0667ed93b4801b78ed64048b1aa2c211d4380fcc5c5b49bec9d6c/json_codegen-0.6.0.tar.gz",
"platform": null,
"description": "[![Build Status](https://travis-ci.org/expobrain/json-schema-codegen.svg?branch=master)](https://travis-ci.org/expobrain/json-schema-codegen)\n\n# json-schema-codegen\n\nGenerate code from JSON schema files.\n\n# Table of contents\n\n- [Introduction](#introduction)\n- [Currently supported languages](#currently-supported-languages)\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Usage](#usage)\n- [Code generation](#code-generation)\n - [Python3](#python-3)\n - [Python3+Marshmallow](#python-3marshmallow)\n - [JavaScript+Flow and Flow](#javascriptflow-and-flow)\n- [Contribute](#contribute)\n\n# Introduction\n\nThis is a command line tool to take a [json-schema](http://json-schema.org/) file and generate code automatically.\n\nFor instance this `json-schema` definition:\n\n```json\n{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"title\": \"Test\",\n \"type\": \"object\",\n \"properties\": {\n \"id\": { \"type\": \"integer\" }\n }\n}\n```\n\nwill generate this Python code:\n\n```python\nclass Test(object):\n def __init__(self, data=None):\n data = data or {}\n\n self.id = data.get(\"id\")\n```\n\nor this JavaScript+Flow code:\n\n```javascript\nexport class Test {\n id: ?number;\n\n constructor(data: Object = {}) {\n this.id = data.id;\n }\n}\n```\n\nCurrently this tool generates code for Python and JavaScript with [Flow](https://flow.org/) annotations but it can be extended to generate code for any language.\n\nThe code generation is divided in two stages:\n\n1. generate the [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree) for the target language from the `json-schema` file\n1. convert the AST into the target language\n\nThis allows the tool to be language agnostic, that is it just needs to generate the AST in JSON format for the target language and then a language specific tool will convert this AST into proper code.\n\n# Currently supported languages\n\nList of currently supported languages:\n\n- Python 3.7+\n- JavaScript ES7+ with Flow annotations\n- pure Flow annotations\n\n# Requirements\n\n- Python 3.6 / 3.7\n- Node v12\n\n# Installation\n\nUntil this [pull request](https://github.com/pypa/setuptools/pull/1389) in [`setuptools`](https://pypi.org/project/setuptools/) is fixed, the only way to install `json-schema-codegen` is to clone the repo:\n\n```shell\ngit clone https://github.com/expobrain/json-schema-codegen.git\n```\n\n# Usage\n\n```shell\nusage: json_codegen.py [-h] [--prefix PREFIX] [--language LANGUAGE]\n [--output OUTPUT]\n schema\n\npositional arguments:\n schema Definition of the PRD as JSON schema\n\noptional arguments:\n -h, --help show this help message and exit\n --prefix PREFIX, -p PREFIX\n Optional prefix for generated classes\n --language LANGUAGE, -l LANGUAGE\n Output language. Default is python\n --output OUTPUT, -o OUTPUT\n Output filename for the generated code\n```\n\n# Code generation\n\n## Python 3\n\nThe egenerator of pure Python 3 compatible code:\n\n```shell\njson_codegen --language python3 --output <output_py_file> <json-schema>\n```\n\n## Python 3+Marshmallow\n\nThe generation of Python 3's code with [Marshmallow](https://marshmallow.readthedocs.io/en/2.x-line/) support is integrated into the tool so it needs just a single invocation:\n\n```shell\njson_codegen --language python3+marshmallow --output <output_py_file> <json-schema>\n```\n\n## JavaScript+Flow and Flow\n\nGenerating JavaScript+Flow and Flow code involves two steps, generating the AST:\n\n```shell\njson_codegen --language [javascript+flow|flow] --output <output_ast_json> <json-schema>\n```\n\nand generating the code from the AST:\n\n```shell\nbin/ast_to_js <output_ast_json> <output_js_file>\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Generate code from JSON schema files.",
"version": "0.6.0",
"split_keywords": [
"python",
"javascript",
"json-schema",
"codegen"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "229a20a7bf41d8b0ce15d61038ca1ca4db7f41761afbbac385c5dedf9df4aee4",
"md5": "133b94f0ecc617a30a3eca1853a27ffc",
"sha256": "9d6bc0d1d4dd8f7ca105b3d7ccbcfae2c90fe0f4c28cf5a20fc77b353d2259cc"
},
"downloads": -1,
"filename": "json_codegen-0.6.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "133b94f0ecc617a30a3eca1853a27ffc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<3.8",
"size": 134841,
"upload_time": "2023-01-29T10:25:00",
"upload_time_iso_8601": "2023-01-29T10:25:00.895966Z",
"url": "https://files.pythonhosted.org/packages/22/9a/20a7bf41d8b0ce15d61038ca1ca4db7f41761afbbac385c5dedf9df4aee4/json_codegen-0.6.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "04f7178db4d0667ed93b4801b78ed64048b1aa2c211d4380fcc5c5b49bec9d6c",
"md5": "487691fbca6b7766031060701d906ba0",
"sha256": "d4bfb002f54e3f908a962f32bcec7d9d8c46315a6dc676a8435f71dd107fbada"
},
"downloads": -1,
"filename": "json_codegen-0.6.0.tar.gz",
"has_sig": false,
"md5_digest": "487691fbca6b7766031060701d906ba0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<3.8",
"size": 128240,
"upload_time": "2023-01-29T10:25:02",
"upload_time_iso_8601": "2023-01-29T10:25:02.820121Z",
"url": "https://files.pythonhosted.org/packages/04/f7/178db4d0667ed93b4801b78ed64048b1aa2c211d4380fcc5c5b49bec9d6c/json_codegen-0.6.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-29 10:25:02",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "json-codegen"
}