Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Generate types and converters from JSON, Schema, and GraphQL

License

NotificationsYou must be signed in to change notification settings

glideapps/quicktype

Repository files navigation

npm versionBuild status

quicktype generates strongly-typed models and serializers from JSON, JSON Schema, TypeScript, andGraphQL queries, making it a breeze to work with JSON type-safely in many programming languages.

Supported Inputs

JSONJSON API URLsJSON Schema
TypeScriptGraphQL queries

Target Languages

RubyJavaScriptFlowRustKotlin
DartPythonC#GoC++
JavaScalaTypeScriptSwiftObjective-CElm
JSON SchemaPikeProp-TypesHaskellPHP

Missing your favorite language? Please implement it!

Installation

There are many ways to usequicktype.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!

For the best CLI, we recommend installingquicktype globally vianpm:

npm install -g quicktype

Usingquicktype

# Run quicktype without arguments for help and optionsquicktype# quicktype a simple JSON object in C#echo'{ "name": "David" }'| quicktype -l csharp# quicktype a top-level array and save as Go sourceecho'[1, 2, 3]'| quicktype -o ints.go# quicktype a sample JSON file in Swiftquicktype person.json -o Person.swift# A verbose way to do the same thingquicktype \  --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.jsonquicktype ./blockchain -o blockchain-api.cpp# quicktype a live JSON API as a Java programquicktype https://api.somewhere.com/data -o Data.java

Generating code from JSON schema

The recommended way to usequicktype 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.swiftquicktype -s schema schema.json -o src/android/Models.javaquicktype -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 aTypeScript file, then quicktyping it. TypeScript is a typed superset of JavaScript with simple, succinct syntax for defining types:

interfacePerson{name:string;nickname?:string;// an optional propertyluckyNumber: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

Callingquicktype from JavaScript

You can usequicktype as a JavaScript function withinnode or browsers. First add thequicktype-core package:

$ npm install quicktype-core

In general, first you create anInputData value with one or more JSON samples, JSON schemas, TypeScript sources, or other supported input types. Then you callquicktype, passing thatInputData value and any options you want.

import{quicktype,InputData,jsonInputForTargetLanguage,JSONSchemaInput,FetchingJSONSchemaStore}from"quicktype-core";asyncfunctionquicktypeJSON(targetLanguage,typeName,jsonString){constjsonInput=jsonInputForTargetLanguage(targetLanguage);// We could add multiple samples for the same desired// type, or many sources for other types. Here we're// just making one type from one piece of sample JSON.awaitjsonInput.addSource({name:typeName,samples:[jsonString]});constinputData=newInputData();inputData.addInput(jsonInput);returnawaitquicktype({        inputData,lang:targetLanguage});}asyncfunctionquicktypeJSONSchema(targetLanguage,typeName,jsonSchemaString){constschemaInput=newJSONSchemaInput(newFetchingJSONSchemaStore());// We could add multiple schemas for multiple types,// but here we're just making one type from JSON schema.awaitschemaInput.addSource({name:typeName,schema:jsonSchemaString});constinputData=newInputData();inputData.addInput(schemaInput);returnawaitquicktype({        inputData,lang:targetLanguage});}asyncfunctionmain(){const{lines:swiftPerson}=awaitquicktypeJSON("swift","Person",jsonString);console.log(swiftPerson.join("\n"));const{lines:pythonPerson}=awaitquicktypeJSONSchema("python","Person",jsonSchemaString);console.log(pythonPerson.join("\n"));}main();

The argument toquicktype is a complex object with many optional properties.Explore its definition to understand what options are allowed.

Adding Custom logic or Rendering:

Quicktype supports creating your own custom languages and rendering output, you can extend existing classes or create your own to be using by thequicktype function.
Check outthis guide for more info.

Contributing

quicktype isOpen Source and we love contributors! In fact, we have alist of issues 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, comejoin us on Slack.

Setup, Build, Run

quicktype is implemented in TypeScript and requiresnodejs andnpm to build and run.

First, installtypescript globally vianpm:

Clone this repo and do:

macOS / Linux

nvm usenpm installscript/quicktype# rebuild (slow) and run (fast)

Windows

npm install --ignore-scripts# Install dependenciesnpm install -g typescript# Install typescript globallytsc --project src/cli# Rebuildnode dist\cli\index.js# Run

Edit

InstallVisual Studio Code, open thisworkspace, 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 generatedoutput as you edit. Usenpm start to watch for changes andrecompile and rerunquicktype for live feedback. For example, if you'redeveloping a new renderer forfortran, you could use the following command torebuild and reinvokequicktype as you implement your renderer:

npm start --"--lang fortran pokedex.json"

The command in quotes is passed toquicktype, so you can render local.jsonfiles, URLs, or add other options.

Test

# Run full test suitenpm runtest# Test a specific language (see test/languages.ts)FIXTURE=golang npmtest# Test a single sample or directoryFIXTURE=swift npmtest -- pokedex.jsonFIXTURE=swift npmtest -- test/inputs/json/samples

Sponsor this project

    Packages

    No packages published

    [8]ページ先頭

    ©2009-2025 Movatter.jp