Target Languages
Ruby | JavaScript | Flow | Rust | Kotlin |
---|
Dart | Python | C# | Go | C++ |
---|
Java | Scala | TypeScript | Swift | Objective-C | Elm |
---|
JSON Schema | Pike | Prop-Types | Haskell | PHP |
---|
Missing your favorite language? Please implement it!
Using quicktype
# Run quicktype without arguments for help and options
quicktype
# quicktype a simple JSON object in C#
echo '{ "name": "David" }' | quicktype -l csharp
# quicktype a top-level array and save as Go source
echo '[1, 2, 3]' | quicktype -o ints.go
# quicktype a sample JSON file in Swift
quicktype person.json -o Person.swift
# A verbose way to do the same thing
quicktype \
--src person.json \
--src-lang json \
--lang swift \
--top-level Person \
--out Person.swift
# quicktype a directory of samples as a C++ program
# Suppose ./blockchain is a directory with files:
# latest-block.json transactions.json marketcap.json
quicktype ./blockchain -o blockchain-api.cpp
# quicktype a live JSON API as a Java program
quicktype https://api.somewhere.com/data -o Data.java
Generating code from JSON schema
The recommended way to use quicktype
is to generate a JSON schema from sample data, review and edit the schema, commit the schema to your project repo, then generate code from the schema as part of your build process:
# First, infer a JSON schema from a sample.
quicktype pokedex.json -l schema -o schema.json
# Review the schema, make changes,
# and commit it to your project repo.
# Finally, generate model code from schema in your
# build process for whatever languages you need:
quicktype -s schema schema.json -o src/ios/models.swift
quicktype -s schema schema.json -o src/android/Models.java
quicktype -s schema schema.json -o src/nodejs/Models.ts
# All of these models will serialize to and from the same
# JSON, so different programs in your stack can communicate
# seamlessly.
Generating code from TypeScript (Experimental)
You can achieve a similar result by writing or generating a TypeScript file, then quicktyping it. TypeScript is a typed superset of JavaScript with simple, succinct syntax for defining types:
interface Person {
name: string;
nickname?: string; // an optional property
luckyNumber: number;
}
You can use TypeScript just like JSON schema was used in the last example:
# First, infer a TypeScript file from a sample (or just write one!)
quicktype pokedex.json -o pokedex.ts --just-types
# Review the TypeScript, make changes, etc.
quicktype pokedex.ts -o src/ios/models.swift
Setup, Build, Run
quicktype
is implemented in TypeScript and requires nodejs
and npm
to build and run.
First, install typescript
globally via npm
:
Clone this repo and do:
macOS / Linux
nvm use
npm install
script/quicktype # rebuild (slow) and run (fast)
Windows
npm install --ignore-scripts # Install dependencies
npm install -g typescript # Install typescript globally
tsc --project src/cli # Rebuild
node dist\cli\index.js # Run
Edit
Install Visual Studio Code, open this workspace, and install the recommended extensions:
code . # opens in VS Code
Live-reloading for quick feedback
When working on an output language, you’ll want to view generated
output as you edit. Use npm start
to watch for changes and
recompile and rerun quicktype
for live feedback. For example, if you’re
developing a new renderer for fortran
, you could use the following command to
rebuild and reinvoke quicktype
as you implement your renderer:
npm start -- "--lang fortran pokedex.json"
The command in quotes is passed to quicktype
, so you can render local .json
files, URLs, or add other options.
Test
# Run full test suite
npm run test
# Test a specific language (see test/languages.ts)
FIXTURE=golang npm test
# Test a single sample or directory
FIXTURE=swift npm test -- pokedex.json
FIXTURE=swift npm test -- test/inputs/json/samples