Re: [Json] Limitations on number size?

Carsten Bormann <cabo@tzi.org> Tue, 09 July 2013 14:42 UTC

Return-Path: <cabo@tzi.org>
X-Original-To: json@ietfa.amsl.com
Delivered-To: json@ietfa.amsl.com
Received: from localhost (localhost [127.0.0.1]) by ietfa.amsl.com (Postfix) with ESMTP id 7ED9821F9C6E for <json@ietfa.amsl.com>; Tue, 9 Jul 2013 07:42:00 -0700 (PDT)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -106.249
X-Spam-Level:
X-Spam-Status: No, score=-106.249 tagged_above=-999 required=5 tests=[BAYES_00=-2.599, HELO_EQ_DE=0.35, RCVD_IN_DNSWL_MED=-4, USER_IN_WHITELIST=-100]
Received: from mail.ietf.org ([12.22.58.30]) by localhost (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id xeBialh9whLX for <json@ietfa.amsl.com>; Tue, 9 Jul 2013 07:41:55 -0700 (PDT)
Received: from informatik.uni-bremen.de (mailhost.informatik.uni-bremen.de [IPv6:2001:638:708:30c9::12]) by ietfa.amsl.com (Postfix) with ESMTP id 5579121F9EC4 for <json@ietf.org>; Tue, 9 Jul 2013 07:41:54 -0700 (PDT)
X-Virus-Scanned: amavisd-new at informatik.uni-bremen.de
Received: from smtp-fb3.informatik.uni-bremen.de (smtp-fb3.informatik.uni-bremen.de [134.102.224.120]) by informatik.uni-bremen.de (8.14.4/8.14.4) with ESMTP id r69Efmj9006242; Tue, 9 Jul 2013 16:41:48 +0200 (CEST)
Received: from [10.0.1.4] (reingewinn.informatik.uni-bremen.de [134.102.218.123]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by smtp-fb3.informatik.uni-bremen.de (Postfix) with ESMTPSA id A74E035B1; Tue, 9 Jul 2013 16:41:48 +0200 (CEST)
Mime-Version: 1.0 (Mac OS X Mail 6.5 \(1508\))
Content-Type: text/plain; charset="iso-8859-1"
From: Carsten Bormann <cabo@tzi.org>
In-Reply-To: <51DC0F95.7010407@gmail.com>
Date: Tue, 09 Jul 2013 16:41:48 +0200
Content-Transfer-Encoding: quoted-printable
Message-Id: <F142B117-37C4-4936-AE81-D3571AB118C8@tzi.org>
References: <51DC0F95.7010407@gmail.com>
To: "Peter F. Patel-Schneider" <pfpschneider@gmail.com>
X-Mailer: Apple Mail (2.1508)
Cc: json@ietf.org
Subject: Re: [Json] Limitations on number size?
X-BeenThere: json@ietf.org
X-Mailman-Version: 2.1.12
Precedence: list
List-Id: "JavaScript Object Notation \(JSON\) WG mailing list" <json.ietf.org>
List-Unsubscribe: <https://www.ietf.org/mailman/options/json>, <mailto:json-request@ietf.org?subject=unsubscribe>
List-Archive: <http://www.ietf.org/mail-archive/web/json>
List-Post: <mailto:json@ietf.org>
List-Help: <mailto:json-request@ietf.org?subject=help>
List-Subscribe: <https://www.ietf.org/mailman/listinfo/json>, <mailto:json-request@ietf.org?subject=subscribe>
X-List-Received-Date: Tue, 09 Jul 2013 14:42:00 -0000

>  JSON numbers are ECMAScript numbers

That's a common misconception.

JSON numbers are what they are (see the production "number" in RFC 4627).
JSON numbers obviously are decimal, ECMAScript numbers are binary.

The text then goes on to say "The representation of numbers is similar to that used in most programming languages."; most programming languages distinguish 0 and 0.0 very heavily, and almost all modern ones have more than 53 bits of precision in numbers, but most programming languages also use binary numbers for the semantics of that decimal representation.

When generating JSON for high levels of interoperability, you wouldn't want to rely on the recipient distinguishing 0 from 0.0, or more generally on distinguishing two numbers that differ but map to the same IEEE 754 double precision number.  Some user communities have done the latter*) and I know a lot of parsers that do the former.

> ECMAScript numbers are IEEE floating point doubles (minus some odd bits).  

Indeed.

> I was astonished to find out that some people disagree, apparently to the point that they believe that 0 is different from .0

.0 is not a JSON number, but 0.0 is.
Whether that is different from 0 is up to the data model (RFC 4627 is mute about that, and I have reason to believe that is intentional); in interoperable JSON, it would be foolish to rely on them being different (but it is less clear to me how foolish it would be to rely on them not being different), in JSON syntax, they clearly are different.

Grüße, Carsten

*) https://dev.twitter.com/docs/twitter-ids-json-and-snowflake