quicktype/README.md

161 строка
5.5 KiB
Markdown
Исходник Обычный вид История

![](quicktype-logo.svg)
2017-11-27 12:08:49 +03:00
[![npm version](https://badge.fury.io/js/quicktype.svg)](https://badge.fury.io/js/quicktype)
[![Build Status](https://travis-ci.org/quicktype/quicktype.svg?branch=master)](https://travis-ci.org/quicktype/quicktype)
[![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.
2018-02-06 19:31:04 +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.
2017-12-22 04:42:15 +03:00
2018-02-06 19:31:04 +03:00
### Supported Inputs
2018-02-06 22:05:47 +03:00
JSON | JSON API URLs | [JSON Schema](https://app.quicktype.io/#s=coordinate) | GraphQL queries
--- | --- | --- | ---
2017-09-02 23:48:17 +03:00
2018-02-06 19:31:04 +03:00
### Target Languages
2018-02-06 22:05:47 +03:00
[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) | [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)
--- | --- | --- | --- | --- | --- | --- | --- | ---
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
There are many ways to use `quicktype`. [app.quicktype.io](https://app.quicktype.io) is the most powerful & complete UI. The web app also works offline and doesn't send your sample data over the Internet, so paste away!
For the best CLI, we recommend installing `quicktype` globally via `npm`:
```bash
npm install -g quicktype
```
Other options:
* [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)\*
<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-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.
quicktype pokedex.json -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.
```
## Development
`quicktype` is open source so if you'd like additional options or a new target language, you can build it yourself and send a pull request. `quicktype` is implemented in TypeScript and requires `nodejs` and `npm` to build and run.
### Setup, Build, Run
Clone this repo and do:
```bash
npm install
script/quicktype # rebuild (slow) and run (fast)
```
### Edit
2017-11-25 04:40:16 +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
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"
```
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
2018-02-06 19:31:04 +03:00
`quicktype` has many complex test dependencies:
2017-11-25 04:40:16 +03:00
* `dotnetcore` SDK
* Java, Maven
* `elm` tools
* `g++` C++ compiler
* `golang` stack
2018-02-06 19:31:04 +03:00
* `swift` compiler
* `clang` and Objective-C Foundation (must be tested separately on macOS)
2017-08-31 08:59:22 +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
```