A tool for converting JSON schema files to type definitions in various programming languages.
- Go
- Python (with Pydantic or dataclasses)
- TypeScript
- C# (.NET)
- Protocol Buffers (proto3)
Install with curl:
curl -sSL https://raw.githubusercontent.com/LongStoryMedia/schema2code/main/install.sh | bashOr with wget:
wget -qO- https://raw.githubusercontent.com/LongStoryMedia/schema2code/main/install.sh | bashThis will:
- Create a
.schema2codedirectory in your home folder - Clone or download the repository
- Set up a virtual environment
- Install the package and add it to your PATH
For development or manual installation:
- Clone the repository:
git clone https://github.com/LongStoryMedia/schema2code.git
cd schema2code- Run the build script:
./build.shOr install directly with pip for development:
pip install -e .python -m src.main [schema_file] --language [language] --output [output_file] [options]schema_file: Path to the JSON or YAML schema file--language,-l: Target language (go,python,typescript,csharp,dotnet,proto,protobuf)--output,-o: Output file path
--mode:create(default) orappend- Whether to create a new file or append to existing--no-create: Don't create the file if it doesn't exist--package: Go package name or Protocol Buffer package name (default: "main")--namespace: C# namespace (default: "SchemaTypes")--no-pydantic: Use dataclasses instead of Pydantic for Python--no-overwrite: Prevents overwriting of existing files--go-package: Go package option for Protocol Buffer files
Generate Go types:
python -m src.main schema.json --language go --output models.go --package modelsGenerate Python types with Pydantic:
python -m src.main schema.json --language python --output models.pyGenerate Python types with dataclasses:
python -m src.main schema.json --language python --output models.py --no-pydanticGenerate TypeScript interfaces:
python -m src.main schema.json --language typescript --output models.tsGenerate C# classes:
python -m src.main schema.json --language csharp --output Models.cs --namespace MyApp.ModelsGenerate Protocol Buffer message definitions:
python -m src.main schema.json --language proto --output message.proto --package mypackage --go-package "example/mypackage"Append to existing file:
python -m src.main schema.json --language go --output models.go --mode appendTo test the command line tool across all sample schemas and supported languages, run:
./test_command_line.shThis script will generate code for all schemas in sample_schemas/ and output to sample_code/, reporting any errors or issues.
Contributions are welcome! To contribute:
- Fork this repository and create a new branch for your feature or bugfix.
- Make your changes and add tests as appropriate.
- Run
./test_command_line.shto ensure all code generation works as expected. - Submit a pull request with a clear description of your changes.
Please follow PEP8 style for Python code and keep code generation logic modular and well-documented.