Giter Site home page Giter Site logo

glideapps / quicktype Goto Github PK

View Code? Open in Web Editor NEW
11.6K 11.6K 1.0K 18.63 MB

Generate types and converters from JSON, Schema, and GraphQL

Home Page: https://app.quicktype.io

License: Apache License 2.0

Shell 0.22% Python 0.56% TypeScript 97.55% JavaScript 1.65% PowerShell 0.01%
cplusplus csharp elm golang graphql java json json-schema kotlin objective-c rust swift typescript

quicktype's Issues

Make Elm output nicer

  • Remove comments
  • Omit unnecessary parentheses
  • Use toplevel name for Root
  • Add explanatory comments
  • All type definitions before encoders and decoders
  • Move toplevel definition up
  • Expose all types
  • No more underscores in names
  • Array and Dict instead of Array.Array and Dict.Dict

Sort classes

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.

Enforce required fields in Go

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)
}

Subclasses in the IR?

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.

Launch blog post

Once we add options, we should blog about them and also mention:

  • Name combination
  • Class de-duplication
  • Map detection
  • Optional types rather than default values (e.g. null : int? not 0 : int)
  • Numeric types

More sound similar class detector

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.

Handle toplevel properly

  • TopLevel class should come first
  • don't test for class name TopLevel
  • Handle non-class JSON (we don't even generate a TopLevel class for that now)

Support enums

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:

  • Supporting enums in our IR, accepting them from JSON Schema, and producing code for them
  • Automatically detecting them from given JSON samples

We can and should do the former, whether we ever do the latter or not.

Better map heuristics

Heuristics:

  • Same property types
  • Large number of properties
  • Property names that don't look like names
  • Detect specific patterns in names, like hashes or numbers

Use incremental parsing

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.

Handle toplevel better

fromJson should ideally be in the top-level class. Figure out how to handle these three cases:

  1. There is one class that can be interpreted as top level.
  2. There is no class at all.
  3. There is more than one class that can be interpreted as top level.

Exposing the Converter class is not ideal because it inherits from JsonConverter, which serves a different purpose than what we want to expose.

Allow non-inferred class names

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.

Add more tests

In particular, all the smaller ones from the Awesome JSON dataset.

Command line client

If only for the use case of running with large JSON files.

Generate go to parse latest Bitcoin block

$ quicktype -o latest-block.go \
  "https://blockchain.info/rawblock/0000000000000bae09a7a393a8acded75aa67e46cb81f7acaa5ad94f9eacd103"

Make Go output fmt-compliant

  • JSON attributes are not horizontally aligned:
 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.

  • Align comments (on consecutively commented lines)
  • Use tab for indentation
  • Don't pad last table column

Support dates

{
    "DepartureDateTime": "2016-08-13T18:15:00",
    "ArrivalDateTime": "2016-08-13T19:27:00"
}

Option to parse missing arrays as empty rather than null

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; }

}

Support tuples

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])

Fix IRAnything

IRAnything currently serves two purposes in the IR:

  1. 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.

  2. 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]

Command line package

$ # Generate go to parse latest Bitcoin block
$ quicktype -o latest-block.go \
  "https://blockchain.info/rawblock/0000000000000bae09a7a393a8acded75aa67e46cb81f7acaa5ad94f9eacd103"

Customization

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.