glideapps / quicktype Goto Github PK
View Code? Open in Web Editor NEWGenerate types and converters from JSON, Schema, and GraphQL
Home Page: https://app.quicktype.io
License: Apache License 2.0
Generate types and converters from JSON, Schema, and GraphQL
Home Page: https://app.quicktype.io
License: Apache License 2.0
Root
Array
and Dict
instead of Array.Array
and Dict.Dict
I think the classes should be sorted by a breadth-first traversal of the graph, starting from the top-level type. At the same depth we could sort alphanumerically, for example.
This code via Gustavo Poscidonio from Slack. We could use this to do arbitrary validation.
package main
import (
"encoding/json"
"fmt"
)
type Pokemon struct {
Name string // required
Type []string // required with length >= 1
Nickname string // not required
}
func (p *Pokemon) UnmarshalJSON(data []byte) error {
var aux struct {
Name string
Type []string
Nickname string
}
err := json.Unmarshal(data, &aux)
if err != nil {
return err
}
if aux.Name == "" {
return fmt.Errorf("Name field not set for Pokemon")
}
if len(aux.Type) < 1 {
return fmt.Errorf("Type field not set for Pokemon")
}
p.Name = aux.Name
p.Type = aux.Type
p.Nickname = aux.Nickname
return nil
}
var validPokemon = []byte(`
{
"name" : "Bulbasaur",
"type" : ["grass", "poison"],
"nickname" : "little foot"
}
`)
var invalidPokemon = []byte(`
{
"name" : "Bulbasaur",
"nickname" : "little foot"
}
`)
func main() {
var pokemon Pokemon
fmt.Println(json.Unmarshal(invalidPokemon, &pokemon))
fmt.Println(json.Unmarshal(validPokemon, &pokemon))
fmt.Printf("My pokemon: %+v\n", pokemon)
}
Extending the similarity metric to include subclasses as similar led to some mayhem, but if we had proper subclasses in the IR that could be avoided. For example, in github-events.json
, there's a small repo
and a large repo
class, the large one containing a strict superset of the small one. The large one could be a proper subclass of the smaller one.
npm test
doesn't rebuild the CLI.
struct TopLevel {
let `class`: String
}
Once we add options, we should blog about them and also mention:
null : int?
not 0 : int
)Right now we pick one class and then find all classes that are similar to it. But depending on which class you start out with, you can end up with different sets. So we might want to find sets where all classes are pairwise similar. The complexity of that is most likely larger, definitely when implemented naively.
Like for us-average-temperatures.json
:
[JsonProperty("192612")]
public 189512 192612 { get; set; }
public bool @private { get; set; }
Just read them all and unify them.
TopLevel
class should come firstTopLevel
TopLevel
class for that now)For string properties, can we form a relatively small set of equivalence classes from a large number of samples? We might represent that as an enum (if the target language has enums).
There are two parts to this:
We can and should do the former, whether we ever do the latter or not.
Heuristics:
Newlines in property names, empty property names, and unicode stuff, doesn't seem to work in Go. See identifiers.json
.
The 270MB Boris validation corpus, for example.
We can't keep the whole JSON document in memory. I get heap overflows with the default V8 settings for JSON files as small as 250ish MB.
This doesn't compile:
class A
{
public object A { get; set; }
}
fromJson
should ideally be in the top-level class. Figure out how to handle these three cases:
Exposing the Converter
class is not ideal because it inherits from JsonConverter
, which serves a different purpose than what we want to expose.
Right now all class names are inferred from property names via regatherClassNames
. That's fine for JSON input, where classes are not named. In JSON Schema it is possible to name classes, so wherever we have a class name we should preserve it, and not try re-infer it. This should be easy to do by making the names
property of IRClassData
an Either String (Set String)
instead of just a Set String
.
Run through samples, generate code, compile, run.
We'll probably end up generating individual classes for each IRUnion
that need custom converters.
In particular, all the smaller ones from the Awesome JSON dataset.
Right now it shows C#.
If only for the use case of running with large JSON files.
$ quicktype -o latest-block.go \
"https://blockchain.info/rawblock/0000000000000bae09a7a393a8acded75aa67e46cb81f7acaa5ad94f9eacd103"
Probably depends on #27
type TopLevel struct {
- Y []*float64 `json:"y"` // array of optional
- X []DoubleOrNullOrString `json:"x"`
+ Y []*float64 `json:"y"`
+ X []DoubleOrNullOrString `json:"x"`
Z []DoubleOrDoubleArrayOrZ `json:"z"`
}
And weirdly it deletes comments. That must be a bug.
{
"DepartureDateTime": "2016-08-13T18:15:00",
"ArrivalDateTime": "2016-08-13T19:27:00"
}
C# naming convention uses Url
, but Go (and others) use URL
.
Otherwise you get
LambdaBokPro430:quicktype schani$ time node cli/quicktype.js --src /tmp/jsons/corpus-validation-adhoc.json
"toString()" failed
dependencies
and devDependencies
are Map String String
but are treated as custom objects.
We'll have to expose some way for people to determine these.
In this example, one person has no friends
property, so this may result in a null
array property in C#. We should consider an option to parse missing arrays as empty rather than null.
{
"people": [
{
"name": "Nice Bob",
"friends": ["David"]
},
{
"name": "Evil Bob"
}
]
}
public class Person
{
[JsonProperty("friends")]
public string[] Friends { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
In the simplest case, an array of arrays, or dictionary of arrays, where each inner array has the same structure (same length, same element types), can be interpreted as an array of tuples, or dictionary of tuples. Eg.
[[1, true, "abc"],
[2, false, "foo"],
[3, true, "bar"]]
would be an Array (Tuple [Int, Bool, String])
There are two Repo
s in github-events.json
, for example.
And a short comment on how to use it
The schema shouldn't change, obviously.
IRAnything
currently serves two purposes in the IR:
In inference, to signify a type we know nothing about. The only way for it to come about is as the element type of an empty array.
In generation, for any type, which we translate to object
in C#.
We make no explicit distinction between the two in the code, i.e. they are one and the same.
That mostly works for now, but we will run into trouble if our inference algorithm ever wants to distinguish between the two. In addition to that, JSON Schema does have a representation for "any type", but our unification algorithm does not treat IRAnything
as "any type", so we can't use and expect to get correct results. For example, if we treat "any type" as IRAnything
, we would unify an array that contains one "any type" and one integer as an array containing integers. The correct unification is an array containing "any type".
If we had IRNoInformation
and IRAny
, unification would look like this:
unify IRNoInformation x = x
unify IRAny x = IRAny
Of course, to preserve more type information we could still do
unify IRAny x = IRUnion [IRAny, x]
$ # Generate go to parse latest Bitcoin block
$ quicktype -o latest-block.go \
"https://blockchain.info/rawblock/0000000000000bae09a7a393a8acded75aa67e46cb81f7acaa5ad94f9eacd103"
For the backends we can start with boolean options, for all languages we'd like the ability to at least give a top level class name.
We can later expand this to typed options. It seems that generics can be used for this.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.