Little Big Detail #1: Perfect Property Names

little-big-details

This post is the first in a series we’re calling “Little Big Details”. It explains some of the subtle details that quicktype takes care of when turning your data into code, and the surprising amount of meticulous technical work behind these details. One of quicktype’s main goals is to generate the code that you would have written; the Little Big Details account for much of how quicktype achieves this.

The first Little Big Detail is about how quicktype turns JSON property names into nice property names in your preferred target language. Let's say you have some JSON like this:

{ "my_favorite_url": "https://quicktype.io/" }

What should the property name corresponding to "my_favorite_url" be? If you write C#, the answer is MyFavoriteUrl. Gophers prefer MyFavoriteURL, because they like their acronyms all caps, whereas in C++ you might just go with my_favorite_url. In Java you have getter and setter methods rather than properties, so my_favorite_url becomes getMyFavoriteURL and setMyFavoriteURL:

// C# class for { "my_favorite_url": "..." }
public class Demo
{
    public string MyFavoriteUrl { get; set; }
}

// Java class for { "my_favorite_url": "..." }
public class Demo {
    private String myFavoriteURL;
    public String getMyFavoriteURL() { return myFavoriteURL; }
    public void setMyFavoriteURL(String value) { this.myFavoriteURL = value; }
}

// Swift struct for { "my_favorite_url": "..." }
struct Demo {
    let myFavoriteURL: String
}

No matter what language you’re using, quicktype will figure out the best names for each property, and it will do so whether the JSON property is "my_favorite_url", "MyFavoriteURL", or "my favorite URL!!?!?!". How does quicktype do this?

First, quicktype splits up the JSON property name into its component words, in this example my, favorite, and url. Then it figures out which words are acronyms using two rules:

  • If the word is all caps, and the whole property name contains at least one lowercase letter, then treat the word as an acronym

  • If the word is in our big list of acronyms, then it's an acronym

The first rule will recognize acronyms in property names like "covered_by_ACA", and the second one hopefully takes care of all the rest.

To put the words back together to form the target property name, each target language specifies whether a word should be lowercase, capitalized (e.g. Url), or all caps for each of four conditions:

  1. For the first word if it's not an acronym, like for good in good_vibes

  2. For any other word if it's not an acronym, such as for three and musketeers in the_three_musketeers

  3. For the first word if it's an acronym, like json in json_property

  4. For any other word if it's an acronym, like url in homepage_url

Target languages may also specify a separator (e.g. C++ uses an underscore) to place between words.

These mechanisms allow quicktype to express all property styles common among its target languages, so you get properties named according to your language’s preferred conventions, no matter how gnarly your JSON is! If you find a case that quicktype doesn't handle correctly, please file an issue, or talk to us on Slack.

Mark

Mark