[ANN] Quicktype -- generate JSON3 structs from json samples (WIP)

Quicktype is a neat program that infers schemas from json samples and generates code in various different languages to read the json into native objects.

I hacked together basic support for Julia in Quicktype. It should work for basic json. Enums, Unions, Nullables and escaping strings do not work properly yet. If your json doesn’t have these and the keys in your dictionaries do not contain the dollar sign, you can probably already use it.

Here is an example of what it does. Clone the repo and change to the “julia” branch. Then run

npm install
echo '{"name": "Bob"}' | script/quicktype -l julia 

Here is the output:

# Example code that deserializes and serializes an object of type "MyType" from a string.
# using JSON3
# JSON3.read(str::String, MyType)
# JSON3.write(obj)

using JSON3
using StructTypes
using Nullables

mutable struct TopLevel
    name :: String

    TopLevel() = new()
end
StructTypes.StructType(::Type{TopLevel}) = StructTypes.Mutable()

StructTypes.names(::Type{TopLevel}) = (
)

Since Quicktype doesn’t have any kind of documentation this was cobbled together based on the Rust and Python translators. I could really use some help from someone who knows TypeScript. Enums are also tricky, since different enums can not contain similarly named fields.

2 Likes