2018-04-30 06:51:01 +03:00
![](https://raw.githubusercontent.com/quicktype/quicktype/master/quicktype-logo.svg?sanitize=true)
2017-12-22 03:28:54 +03:00
2017-11-27 12:08:49 +03:00
[![npm version ](https://badge.fury.io/js/quicktype.svg )](https://badge.fury.io/js/quicktype)
2018-02-18 00:21:29 +03:00
[![Build status ](https://badge.buildkite.com/147309f9f492c2af1ea53df922be7140ba4035dbb31f61ee1e.svg )](https://buildkite.com/typeguard/quicktype-master)
2017-11-27 12:08:49 +03:00
[![Join us in Slack ](http://slack.quicktype.io/badge.svg )](http://slack.quicktype.io/)
2017-08-31 08:59:22 +03:00
2018-02-06 19:31:04 +03:00
`quicktype` generates strongly-typed models and serializers from JSON, JSON Schema, and [GraphQL queries ](https://blog.quicktype.io/graphql-with-quicktype/ ), making it a breeze to work with JSON type-safely in any programming language.
2017-07-13 03:22:10 +03:00
2018-08-08 18:08:42 +03:00
- [Try `quicktype` in your browser ](https://app.quicktype.io ).
- View [awesome JSON APIs ](https://github.com/typeguard/awesome-typed-datasets ) that have been strongly typed with `quicktype` .
- Read ['A first look at quicktype' ](http://blog.quicktype.io/first-look/ ) for more introduction.
- If you have any questions, check out the [FAQ ](FAQ.md ) first.
2017-12-22 04:42:15 +03:00
2018-02-06 19:31:04 +03:00
### Supported Inputs
2017-07-25 04:03:21 +03:00
2018-04-03 07:07:51 +03:00
| JSON | JSON API URLs | [JSON Schema ](https://app.quicktype.io/#s=coordinate ) |
| ---- | ------------- | ----------------------------------------------------- |
2018-05-19 17:21:21 +03:00
2018-04-03 07:07:51 +03:00
| TypeScript | GraphQL queries |
| ---------- | --------------- |
2018-02-12 02:45:16 +03:00
2017-09-02 23:48:17 +03:00
2018-02-06 19:31:04 +03:00
### Target Languages
2017-07-25 04:03:21 +03:00
2018-08-20 16:59:10 +03:00
| [Ruby ](https://app.quicktype.io/#l=ruby ) | [JavaScript ](https://app.quicktype.io/#l=js ) | [Flow ](https://app.quicktype.io/#l=flow ) | [Rust ](https://app.quicktype.io/#l=rust ) | [Kotlin ](https://app.quicktype.io/#l=kotlin ) | [Dart ](https://app.quicktype.io/#l=dart ) |
| ---------------------------------------- | -------------------------------------------- | ---------------------------------------- | ---------------------------------------- | -------------------------------------------- | ---------------------------------------- |
2018-05-19 17:21:21 +03:00
2018-02-13 21:07:10 +03:00
2018-08-01 17:15:53 +03:00
| [Python ](https://app.quicktype.io/#l=python ) | [C# ](https://app.quicktype.io/#l=cs ) | [Go ](https://app.quicktype.io/#l=go ) | [C++ ](https://app.quicktype.io/#l=cpp ) | [Java ](https://app.quicktype.io/#l=java ) | [TypeScript ](https://app.quicktype.io/#l=ts ) |
| -------------------------------------------- | ------------------------------------ | ------------------------------------ | -------------------------------------- | ---------------------------------------- | -------------------------------------------- |
2018-02-13 21:07:10 +03:00
2018-05-19 17:21:21 +03:00
2019-01-17 12:55:00 +03:00
| [Swift ](https://app.quicktype.io/#l=swift ) | [Objective-C ](https://app.quicktype.io/#l=objc ) | [Elm ](https://app.quicktype.io/#l=elm ) | [JSON Schema ](https://app.quicktype.io/#l=schema ) | [Pike ](https://app.quicktype.io/#l=pike ) |
| ------------------------------------------ | ----------------------------------------------- | -------------------------------------- | ------------------------------------------------- | ------------------------------------------------- |
2018-02-12 02:45:16 +03:00
2018-05-19 17:21:21 +03:00
2018-02-06 19:31:04 +03:00
_Missing your favorite language? Please implement it!_
2017-08-31 08:59:22 +03:00
2018-02-06 19:31:04 +03:00
## Installation
2018-02-13 21:10:25 +03:00
There are many ways to use `quicktype` . [app.quicktype.io ](https://app.quicktype.io ) is the most powerful and complete UI. The web app also works offline and doesn't send your sample data over the Internet, so paste away!
2018-02-06 19:31:04 +03:00
For the best CLI, we recommend installing `quicktype` globally via `npm` :
```bash
npm install -g quicktype
```
Other options:
2018-08-08 18:08:42 +03:00
- [Homebrew ](http://formulae.brew.sh/formula/quicktype ) _(infrequently updated)_
- [Xcode extension ](https://itunes.apple.com/us/app/paste-json-as-code-quicktype/id1330801220?mt=12 )\*
- [VSCode extension ](https://marketplace.visualstudio.com/items/quicktype.quicktype )\*
- [Visual Studio extension ](https://marketplace.visualstudio.com/items?itemName=typeguard.quicktype-vs )\*
2018-02-06 19:31:04 +03:00
< small > \* limited functionality</ small >
2017-11-25 04:40:16 +03:00
2018-02-06 19:31:04 +03:00
## Using `quicktype`
```bash
# 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
2017-11-25 04:40:16 +03:00
```
2018-04-03 07:07:51 +03:00
### Generating code from JSON schema
2018-02-06 19:31:04 +03:00
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:
```bash
# First, infer a JSON schema from a sample.
2018-10-02 20:27:20 +03:00
quicktype pokedex.json -l schema -o schema.json
2018-02-06 19:31:04 +03:00
# 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.
```
2018-04-03 07:07:51 +03:00
### Generating code from TypeScript (Experimental)
2018-04-05 17:33:25 +03:00
You can achieve a similar result by writing or generating a [TypeScript ](http://www.typescriptlang.org/ ) file, then quicktyping it. TypeScript is a typed superset of JavaScript with simple, succinct syntax for defining types:
2018-04-03 07:07:51 +03:00
```typescript
interface Person {
2018-08-08 18:08:42 +03:00
name: string;
nickname?: string; // an optional property
luckyNumber: number;
2018-04-03 07:07:51 +03:00
}
```
You can use TypeScript just like JSON schema was used in the last example:
```bash
# 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
```
2018-02-12 02:45:16 +03:00
## Contributing
2018-02-06 19:31:04 +03:00
2018-02-13 21:09:54 +03:00
`quicktype` is [Open Source ](LICENSE ) and we love contributors! In fact, we have a [list of issues ](https://github.com/quicktype/quicktype/issues?utf8=✓&q=is%3Aissue+is%3Aopen+label%3Ahelp-wanted ) that are low-priority for us, but for which we'd happily accept contributions. Support for new target languages is also strongly desired. If you'd like to contribute, need help with anything at all, or would just like to talk things over, come [join us on Slack ](http://slack.quicktype.io ).
2018-02-06 19:31:04 +03:00
### Setup, Build, Run
2018-02-12 02:45:16 +03:00
`quicktype` is implemented in TypeScript and requires `nodejs` and `npm` to build and run.
2018-11-13 18:35:08 +03:00
First, install `typescript` globally via `npm` :
2018-02-06 19:31:04 +03:00
Clone this repo and do:
2018-11-13 18:35:08 +03:00
#### macOS / Linux
2018-02-06 19:31:04 +03:00
```bash
npm install
script/quicktype # rebuild (slow) and run (fast)
```
2018-11-13 18:35:08 +03:00
#### Windows
```bash
npm install --ignore-scripts # Install dependencies
npm install -g typescript # Install typescript globally
tsc --project src/cli # Rebuild
node dist\cli\index.js # Run
```
2018-02-06 19:31:04 +03:00
### Edit
2017-11-25 04:40:16 +03:00
2017-12-22 03:28:54 +03:00
Install [Visual Studio Code ](https://code.visualstudio.com/ ), open this
workspace, and install the recommended extensions:
2017-11-25 04:40:16 +03:00
2018-02-06 19:31:04 +03:00
```bash
code . # opens in VS Code
2017-11-25 04:40:16 +03:00
```
2018-02-06 19:31:04 +03:00
### Live-reloading for quick feedback
2017-08-01 23:42:12 +03:00
2017-12-22 03:28:54 +03:00
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:
2017-11-25 04:40:16 +03:00
2018-02-06 19:31:04 +03:00
```bash
npm start -- "--lang fortran pokedex.json"
2017-07-13 05:12:20 +03:00
```
2017-12-22 03:28:54 +03:00
The command in quotes is passed to `quicktype` , so you can render local `.json`
files, URLs, or add other options.
2017-11-25 04:40:16 +03:00
2018-02-06 19:31:04 +03:00
### Test
2017-07-15 05:51:42 +03:00
2018-02-06 19:31:04 +03:00
`quicktype` has many complex test dependencies:
2017-11-25 04:40:16 +03:00
2018-09-17 22:41:32 +03:00
- `crystal` compiler
2018-08-08 18:08:42 +03:00
- `dotnetcore` SDK
- Java, Maven
- `elm` tools
- `g++` C++ compiler
- `golang` stack
- `swift` compiler
- `clang` and Objective-C Foundation (must be tested separately on macOS)
- `rust` tools
2019-01-17 12:55:00 +03:00
- `pike` interpreter
2018-08-08 18:08:42 +03:00
- [Bundler ](https://bundler.io ) for Ruby
2017-08-31 08:59:22 +03:00
2017-12-22 03:28:54 +03:00
We've assembled all of these tools in a Docker container that you build and test within:
2017-11-25 04:40:16 +03:00
2018-02-06 19:31:04 +03:00
```bash
# Build and attach to Docker container
script/dev
# Run full test suite
npm run test
2017-08-31 08:59:22 +03:00
2018-02-06 19:31:04 +03:00
# Test a specific language (see test/languages.ts)
FIXTURE=golang npm test
2017-08-31 08:59:22 +03:00
2018-02-06 19:31:04 +03:00
# Test a single sample or directory
FIXTURE=swift npm test -- pokedex.json
FIXTURE=swift npm test -- test/inputs/json/samples
2017-11-25 04:40:16 +03:00
```