A first look at quicktype

ide-big

Yesterday we launched a preview of quicktype. Here's a brief explanation of what it is.

For programmers

quicktype infers types from sample data (e.g. JSON), then outputs typed code in your desired programming language (C# and Go are available to start but more are planned).

There are a few simple tools that do this already, but quicktype is already a bit cleverer:

  • meaningful type names are guessed from names in data
  • equivalent types with different names are unified
  • quicktype optionally outputs de/serialization helpers
  • quicktype is smarter about maps/dictionaries
  • quicktype synthesizes union types for heterogeneous data
  • quicktype works locally and doesn't transfer, retain, or share your sample data

For everyone else

Apps and web services communicate with each other over the Internet by sending and receiving 'documents' of data. Here's an example of the kind of data Pokémon Go might download about nearby pokémon from its servers:

{
  "pokemon": [
    {
      "num": "001",
      "name": "Bulbasaur",
      "picture": "http://www.serebii.net/pokemongo/pokemon/001.png",
      "type": ["Grass", "Poison"]
    },
    {
      "num": "002",
      "name": "Ivysaur",
      "picture": "http://www.serebii.net/pokemongo/pokemon/002.png",
      "type": ["Grass", "Poison"]
    }
  ]
}

As another example, Tinder might download information for nearby matches that looks like this:

{
  "nearby_matches": [
    {
      "name": "Becky",
      "age": 28,
      "bio": "Looking for a bouldering partner!",
      "last_active": "2017-01-06T17:42:02Z"
    },
    {
      "name": "Julia",
      "age": 31,
      "bio": "Sci-fi nerd. Dog mom. Anesthesiologist.",
      "last_active": "2017-01-06T17:42:02Z"
    }
  ]
}

Before you can toss a pokéball or swipe right on a cutie, each app needs to translate the downloaded data into a structured representation so it can be filtered, transformed, or displayed graphically. For example, Pokémon Go might want to ignore nearby pokémon that are too strong for your character to catch, then show pictures of the remaining pokémon on a map. Tinder might prioritize recently active matches to increase your chances of matching with someone while they're using the app.

The structured representation, also known as the type of the data, is what allows the app to 'ignore', 'show pictures', 'prioritize', or do anything else meaningful with the downloaded data. The programmers building these apps must write code to represent the types of data the app expects to work with, then convert the downloaded data into those types. Here's an example of what the type of the nearby pokémon data looks like in a popular programming language:

namespace DataModel
{
  // This is the type of the nearby pokémon data
  class NearbyPokemon
  {
    public Pokemon[] Pokemon { get; set; }
  }

  class Pokemon
  {
    public string Num { get; set; }
    public string Name { get; set; }
    public string Picture { get; set; }
    public string[] Type { get; set; }
  }

  // This turns the downloaded data into a NearbyPokemon type
  class Converter
  {
    public static NearbyPokemon NearbyPokemonFromData(string json) =>
      Newstonsoft.Json.JsonConvert.DeserializeObject<NearbyPokemon>(json);
  }
}

This is not difficult code for a programmer to write--in fact, it's trivial and boring! That's where quicktype comes in.


Given sample data, quicktype automatically derives its type and writes the code needed to work with that data. It writes the boring code for the programmer, so she can save time and focus on the interesting code instead. Neat!

David

David