In this blog post, we explain how we used a Markov chain to detect whether a JSON object represents a class (fixed properties) or a map (dynamic keys) to infer its type and generate the same code a human programmer would for representing that data.
typealias Blocks = [String: Block] // a.k.a. Dictionary<String, Block>
struct Block {
let size, height: Int
let difficulty: Double
let previous: String
}
Rather than:
struct Blocks {
let _0000000000000000000e222e4e7afc29c49f6398783a94c846dee2e13c6408f5: Block
let _00000000000000000043aba4c065d4d92aec529566287ebec5fe9010246c9589: Block
let _00000000000000000009025b9e95911a4dc050de129ea4eb5e40ef280751a0cb: Block
}
struct Block {
let size, height: Int
let difficulty: Double
let previous: String
}
We taught quicktype to make the same decision by evaluating the JSON property names with a Markov chain trained on simulated class property names. Our blog article goes into detail and lets you play with our Markov chain!
Replied on twitter, but you've got a small mistake in the tacos/sushi graph vs. the text (tacos->tacos are labeled with 60% in the image, should be 40 according to the text).
3 comments
[ 8.2 ms ] story [ 21.3 ms ] threadFor example, given this Bitcoin API data:
You'd expect the corresponding Swift code: Rather than: We taught quicktype to make the same decision by evaluating the JSON property names with a Markov chain trained on simulated class property names. Our blog article goes into detail and lets you play with our Markov chain!