jtd-codegen
jtd-codegen
generates code (datatypes, classes, etc.) in many programming
languages from JSON Typedef schemas.
For high-level guidance on how to use this package, see "Generating Code from JSON Typedef Schemas" in the JSON Typedef docs. For high-level guidance on how to use this package with your particular programming language of choice, see:
In addition to the general docs in this README, each programming language that
jtd-codegen
supports has its own set of specific documentation:
Installation
To install jtd-codegen
, you have a few options:
Install with Homebrew
This option is recommended if you're on macOS.
Install with Docker
This option is recommended on non-Mac platforms, or if you're running
jtd-codegen
in some sort of script and you want to make sure that everyone
running the script uses the same version of jtd-codegen
.
If you opt to use the Docker approach, you will need to change all invocations
of jtd-codegen
in this README from:
To:
Install with Cargo
This option is recommended if you already have cargo
installed, or if you
would prefer to use a version of jtd-codegen
compiled on your machine:
Usage
See the top of this README for links to high-level guidance and specifics for each programming language.
For help running jtd-codegen
, run:
There are two prerequisites to invoking jtd-codegen
:
-
You need to have your schema in a file.
jtd-codegen
will use the name of that file to infer the names of the datatypes/classes/etc it will generate. -
You need to have a separate "output" directory for each output language you want to use.
jtd-codegen
will not create this directory for you.
For example, if you have a schema called user.jtd.json
that looks like this:
Generate code for a single programming language
To create code for a single programming language, run:
# Generate TypeScript into "out/" from user.jtd.json
This will create a file called out/index.ts
that looks like this:
export interface User {
createdAt: string;
favoriteNumbers: number[];
name: string;
}
Each programming language has its own set of options. See jtd-codegen --help
and the specific documentation for each programming language for more specifics.
Generate code for multiple programming languages
To create code for multiple programming languages at once, pass flags for each programming language you'd like to generate code for:
# Generate Golang into "go_out/" and Java into "java-out/" from user.jtd.json
This will create two files, called go_out/index.go
:
package go_out
import "time"
type User struct
And another called java-out/User.java
:
;
;
;