<story><title>Parsing JSON Is a Minefield (2018)</title><url>http://seriot.ch/parsing_json.php</url></story><parent_chain><item><author>userbinator</author><text>I suppose you could saw that parsing &lt;i&gt;any&lt;/i&gt; text-based protocol in general &amp;quot;Is a Minefield&amp;quot;. They look so simple and &amp;quot;readable&amp;quot;, which is why they&amp;#x27;re appealing initially, but parsing text always involves lots of corner-cases and I&amp;#x27;ve always thought it a huge waste of resources to use text-based protocols for data that&amp;#x27;s not actually meant for human consumption the vast majority of the time.&lt;p&gt;Consider something as simple as parsing an integer in a text-based format; there may be whitespace to skip, an optional sign character, and then a loop to accumulate digits and convert them (itself a subtraction, multiply, and add), and there&amp;#x27;s still the questions of all the invalid cases and what they should do. In contrast, in a binary format, all that&amp;#x27;s required is to read the data, and the most complex thing which might be required is endianness conversion. Length-prefixed binary formats are almost trivial to parse, on par with reading a field from a struture.</text></item></parent_chain><comment><author>robocat</author><text>Binary formats have their own serious problems.&lt;p&gt;&amp;gt; Length-prefixed binary formats are almost trivial to parse&lt;p&gt;They definitely are not, as displayed by the fact that binary lengths are the root cause of a huge number of security flaws. JSON mostly avoids that.&lt;p&gt;&amp;gt; the most complex thing which might be required is endianness conversion&lt;p&gt;That is a gross simplification. When you look at the details of binary representations, things get complex, and you end up with corner cases.&lt;p&gt;Let&amp;#x27;s look at floating point numbers: with a binary format you can transmit NaN, Infinity, -infinity, and -0. You can also create two NaN numbers that do not have the same binary representation. You have to choose single or double precision (maybe a benefit, not always). Etc.&lt;p&gt;Similarly in JSON integers or arrays of integers are nothing special. It is mostly a benefit not to have to specify UInt8Array.&lt;p&gt;JSON is one of many competitors within an ecology of programming, including binary formats, and yet JSON currently dominates large parts of that ecology. So far a binary format mutation hasn&amp;#x27;t beaten JSON, which is telling since binary had the early advantage (well: binary definitely wins in parts of the ecology, just as JSON wins in other parts).</text></comment>
<story><title>Parsing JSON Is a Minefield (2018)</title><url>http://seriot.ch/parsing_json.php</url></story><parent_chain><item><author>userbinator</author><text>I suppose you could saw that parsing &lt;i&gt;any&lt;/i&gt; text-based protocol in general &amp;quot;Is a Minefield&amp;quot;. They look so simple and &amp;quot;readable&amp;quot;, which is why they&amp;#x27;re appealing initially, but parsing text always involves lots of corner-cases and I&amp;#x27;ve always thought it a huge waste of resources to use text-based protocols for data that&amp;#x27;s not actually meant for human consumption the vast majority of the time.&lt;p&gt;Consider something as simple as parsing an integer in a text-based format; there may be whitespace to skip, an optional sign character, and then a loop to accumulate digits and convert them (itself a subtraction, multiply, and add), and there&amp;#x27;s still the questions of all the invalid cases and what they should do. In contrast, in a binary format, all that&amp;#x27;s required is to read the data, and the most complex thing which might be required is endianness conversion. Length-prefixed binary formats are almost trivial to parse, on par with reading a field from a struture.</text></item></parent_chain><comment><author>juliusmusseau</author><text>Consider only this:  &amp;quot;1.001&amp;quot;&lt;p&gt;I&amp;#x27;ll use JavaScript numeric literals here as my translation medium (ironic!):&lt;p&gt;Norway locale parses it to: 1001&lt;p&gt;USA locale parses it to:    1.001&lt;p&gt;France locale parses it to: NaN&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;docs.oracle.com&amp;#x2F;cd&amp;#x2F;E19455-01&amp;#x2F;806-0169&amp;#x2F;overview-9&amp;#x2F;index.html&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;docs.oracle.com&amp;#x2F;cd&amp;#x2F;E19455-01&amp;#x2F;806-0169&amp;#x2F;overview-9&amp;#x2F;ind...&lt;/a&gt;</text></comment>