TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

What type is my JSON? Ask our Markov chain!

10 pointsby dvdsglabout 7 years ago

1 comment

dvdsglabout 7 years ago
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.<p>For example, given this Bitcoin API data:<p><pre><code> { &quot;0000000000000000000e222e4e7afc29c49f6398783a94c846dee2e13c6408f5&quot;: { &quot;size&quot;: 969709, &quot;height&quot;: 510599, &quot;difficulty&quot;: 3007383866429.732, &quot;previous&quot;: &quot;000000000000000000552a7783efd39eaa1c5ff6789e21a0bbe7547bc454fced&quot; }, &quot;000000000000000000552a7783efd39eaa1c5ff6789e21a0bbe7547bc454fced&quot;: { &quot;size&quot;: 991394, &quot;height&quot;: 510598, &quot;difficulty&quot;: 3007383866429.732, &quot;previous&quot;: &quot;00000000000000000043aba4c065d4d92aec529566287ebec5fe9010246c9589&quot; }, &quot;00000000000000000043aba4c065d4d92aec529566287ebec5fe9010246c9589&quot;: { &quot;size&quot;: 990527, &quot;height&quot;: 510597, &quot;difficulty&quot;: 3007383866429.732, &quot;previous&quot;: &quot;00000000000000000009025b9e95911a4dc050de129ea4eb5e40ef280751a0cb&quot; } } </code></pre> You&#x27;d expect the corresponding Swift code:<p><pre><code> typealias Blocks = [String: Block] &#x2F;&#x2F; a.k.a. Dictionary&lt;String, Block&gt; struct Block { let size, height: Int let difficulty: Double let previous: String } </code></pre> Rather than:<p><pre><code> struct Blocks { let _0000000000000000000e222e4e7afc29c49f6398783a94c846dee2e13c6408f5: Block let _00000000000000000043aba4c065d4d92aec529566287ebec5fe9010246c9589: Block let _00000000000000000009025b9e95911a4dc050de129ea4eb5e40ef280751a0cb: Block } struct Block { let size, height: Int let difficulty: Double let previous: String } </code></pre> 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!
评论 #16526814 未加载