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 even is a JSON number?

179 pointsby bterlsonabout 1 year ago

25 comments

kccqzyabout 1 year ago
I&#x27;ll add that for Haskell, the library everyone uses for JSON parses numbers into Scientific types with almost unlimited size and precision. I say almost unlimited because they use a decimal coefficient-and-exponent representation where the exponent is a 64-bit integer.<p>The documentation is quite paranoid that if you are dealing with untrusted inputs, you could parse two JSON numbers from the untrusted source fine and then performing an addition on them could cause your memory to fill up. Exciting new DoS vector.<p>Of course in practice people end up parsing them into custom types with 64-bit integers, so this is only a problem if you are manipulating JSON directly which is very rare in Haskell.
评论 #39900871 未加载
评论 #39902083 未加载
hinkleyabout 1 year ago
One of the first Ajax projects I worked on was multi tenant, and someone decided to solve the industrial espionage problem by using random 64 bit identifiers for all records in the system. You have about a .1% chance of generating an ID that gets truncated in JavaScript, which is just enough that you might make it past MVP before anyone figures out it’s broken, and that’s exactly what happened to us.<p>So we had to go through all the code adding quotes to all the ID fields. That was a giant pain in my ass.
评论 #39900452 未加载
评论 #39901716 未加载
评论 #39900803 未加载
yawaraminabout 1 year ago
Long story short: don&#x27;t use JSON numbers to represent money or monetary rates. Always use decimals encoded as string. It&#x27;s surprising how many APIs fall short of this basic bar.
评论 #39903763 未加载
评论 #39903434 未加载
评论 #39903240 未加载
评论 #39925748 未加载
whalesaladabout 1 year ago
I tend to end up encoding everything as an integer (multiply by 1000, 10000 etc) and then turn it back into a float&#x2F;decimal on decode. For instance if I am building a system dealing with dollar amounts I will store cent amounts everywhere, communicate cent amounts over the wire, etc. then treat it as a presentation concern to render it as a dollar amount.
评论 #39899391 未加载
评论 #39903594 未加载
评论 #39900254 未加载
评论 #39899562 未加载
评论 #39899320 未加载
评论 #39899314 未加载
评论 #39900162 未加载
评论 #39900042 未加载
vbezhenarabout 1 year ago
My opinion is that a safe approach is to use either 52-bit integer number or 64-bit floating number to keep JavaScript compatibility. JavaScript is too important and at the same time, the errors are too terrific (JS will silently round to the nearest 52-bit integer number which could lead to various exploits) to skip on that. If you need anything else, just use strings.
EdSchoutenabout 1 year ago
I think the description for Go is inaccurate&#x2F;incomplete. You can call this function to instruct the decoder to leave numbers in unparsed string form:<p><a href="https:&#x2F;&#x2F;pkg.go.dev&#x2F;encoding&#x2F;json#Decoder.UseNumber" rel="nofollow">https:&#x2F;&#x2F;pkg.go.dev&#x2F;encoding&#x2F;json#Decoder.UseNumber</a><p>That allows you to capture&#x2F;forward numbers without any loss of precision.
评论 #39898955 未加载
bevekspldnwabout 1 year ago
Try to get a DECIMAL value out of a Postgres database into a JSON API response and you’ll learn all this and more in the most painful way possible!
Someoneabout 1 year ago
Other values one could test for:<p>- <i>“+1”</i> (not a valid number, according to ECMA-404 and RFC-8259)<p>- <i>“+0”</i> (also not a valid number, but trickier than <i>“+1”</i> because IEEE floats have <i>“+0”</i> and <i>“-0”</i>)<p>- <i>“070”</i> (not a valid number, but may get parsed as octal 56)<p>- <i>“1.”</i> (not a valid number in json)<p>- <i>“.1”</i> (not a valid number in json)<p>- <i>“0E-0”</i> (a valid number in json)<p>There probably are others.
paulddraperabout 1 year ago
&gt; I-JSON messages SHOULD NOT include numbers that express greater magnitude or precision than an IEEE 754 double precision number provides<p>I&#x27;m confused by this.<p>What is the precision of 0.1, relative to IEEE 754?<p>If I read it correctly, that statement is saying:<p><pre><code> json_number_precision(json_number) &lt;= ieee_754_precision </code></pre> ^ How do I calculate these values?
评论 #39899046 未加载
评论 #39899096 未加载
评论 #39901874 未加载
Cloudefabout 1 year ago
First thing that I check before using a JSON parser library is that if it lets me to get the number as a string and let me do my own conversion. Libraries that try to treat the number as double or bring in a large bigint&#x2F;decimal library gets usually pass from me.
评论 #39903769 未加载
nigeltaoabout 1 year ago
When I wrote my jsonptr tool a few years ago, I noticed that some JSON libraries (in both C++ and Rust) don&#x27;t even do &quot;parse a string of decimal digits as a float64&quot; properly. I don&#x27;t mean that in the &quot;0.3 isn&#x27;t exactly representable; have 0.30000000000000004 instead&quot; sense.<p>I mean that rapidjson (C++) parsed the string &quot;0.99999999999999999&quot; as the number 1.0000000000000003. Apart from just looking weird, it&#x27;s a different float64 bit-pattern: 0x3FF0000000000000 vs 0x3FF0000000000001.<p>Similarly, serde-json (Rust) parsed &quot;122.416294033786585&quot; as 122.4162940337866. This isn&#x27;t as obvious a difference, but the bit-patterns differ by one: 0x405E9AA48FBB2888 vs 0x405E9AA48FBB2889. Serde-json does have an &quot;float_roundtrip&quot; feature flag, but it&#x27;s opt-in, not enabled by default.<p>For details, look for &quot;rapidjson issue #1773&quot; and &quot;serde_json issue #707&quot; at <a href="https:&#x2F;&#x2F;nigeltao.github.io&#x2F;blog&#x2F;2020&#x2F;jsonptr.html" rel="nofollow">https:&#x2F;&#x2F;nigeltao.github.io&#x2F;blog&#x2F;2020&#x2F;jsonptr.html</a>
评论 #39900275 未加载
评论 #39901255 未加载
egworabout 1 year ago
I think the thing folk miss is when there’s an error like divide by zero, or the calculation would return NaN. I feel like this is the main gap&#x2F;concern with using JSON and it seems to be rarely discussed.
评论 #39900364 未加载
eternityforestabout 1 year ago
I like to think of floating point values as noisy analog voltages, with the extra propery that they can store small integers perfectly, and they can be copied within code but not round trip serialized and deserializer without noise.<p>They&#x27;re not really noisy, but if an application would work with some random noise added, it will probably work with floats, and if it wouldn&#x27;t work with noise added, it&#x27;s probably easier to just not use floats and expext people to reason about IEEE details, while risking subtle bugs if different float representations get mixed.<p>Of course I&#x27;m not doing a lot of high performance algorithms, I would imagine in some applications you really do need to reason about floats.
j16sdizabout 1 year ago
The font choice for inline text is so distracting<p>This &quot;Averia Serif Libre&quot; is unreadable for me.
ctrwabout 1 year ago
I still get a laugh of ecma 404. The first time I looked it up I refreshed the page a large number of times before I realized it wasn&#x27;t an error.
p0w3n3dabout 1 year ago
I think that good decision regarding numbers in API (as it was made in my project) is to put meaningful decimal numbers into string and let them be handled by exact decimal calculation framework, e.g. BigDecimal in Java etc.
Tabular-Icebergabout 1 year ago
Does anyone have any idea why Crockford decided that at least one digit is required after the decimal point, as opposed to JavaScript which has zero or more?
erik_seabergabout 1 year ago
It&#x27;s weird that any parser that loses digits is tolerated. A parser that forces strings into uppercase US-ASCII never would be.
评论 #39902113 未加载
评论 #39899276 未加载
评论 #39899375 未加载
yau8edq12iabout 1 year ago
JSON is a notation. It&#x27;s syntax. The semantics are left up to the implementation. The question has no answer.
datagreedabout 1 year ago
That font &gt;.&lt;
评论 #39904024 未加载
frizlababout 1 year ago
It’s missing Swift tests, but otherwise it’s a great post.
评论 #39899560 未加载
ape4about 1 year ago
Since JSON is so widely used it should be modified to support more types - Mongo DB&#x27;s Extended JSON supports all the BSON (Binary) types:<p><pre><code> Array Binary Date Decimal128 Document Double Int32 Int64 MaxKey MinKey ObjectId Regular Expression Timestamp </code></pre> <a href="https:&#x2F;&#x2F;www.mongodb.com&#x2F;docs&#x2F;manual&#x2F;reference&#x2F;mongodb-extended-json&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.mongodb.com&#x2F;docs&#x2F;manual&#x2F;reference&#x2F;mongodb-extend...</a>
评论 #39899089 未加载
评论 #39899225 未加载
评论 #39900931 未加载
评论 #39899898 未加载
mulmenabout 1 year ago
There are no numbers in JSON. There are only strings.
billpgabout 1 year ago
&quot;ID numbers start from 2^53 and are allocated sequentially including odd numbers that are not compatible with &quot;double&quot; types. Please ensure you are reading this value as a 64-bit integer.&quot;
timvdalenabout 1 year ago
A little off topic, but fun to see that someone else has adopted that magical CSS theme! (<a href="https:&#x2F;&#x2F;css.winterveil.net&#x2F;" rel="nofollow">https:&#x2F;&#x2F;css.winterveil.net&#x2F;</a>)
评论 #39899143 未加载