Re: [Json] Limitations on number size?

Bjoern Hoehrmann <derhoermi@gmx.net> Tue, 09 July 2013 16:23 UTC

Return-Path: <derhoermi@gmx.net>
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 5291E21F9CA4 for <json@ietfa.amsl.com>; Tue, 9 Jul 2013 09:23:41 -0700 (PDT)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -2.566
X-Spam-Level:
X-Spam-Status: No, score=-2.566 tagged_above=-999 required=5 tests=[AWL=0.033, BAYES_00=-2.599]
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 UXVUmOA0gtgN for <json@ietfa.amsl.com>; Tue, 9 Jul 2013 09:23:35 -0700 (PDT)
Received: from mout.gmx.net (mout.gmx.net [212.227.15.15]) by ietfa.amsl.com (Postfix) with ESMTP id CCB5F21F9CB1 for <json@ietf.org>; Tue, 9 Jul 2013 09:23:33 -0700 (PDT)
Received: from =?utf-8?q?netb.Speedport=5fW=5f700V?= ([91.35.39.121]) by mail.gmx.com (mrgmx103) with ESMTPA (Nemesis) id 0MgKoE-1Ua6J43zlO-00Nl38; Tue, 09 Jul 2013 18:23:32 +0200
From: Bjoern Hoehrmann <derhoermi@gmx.net>
To: "Peter F. Patel-Schneider" <pfpschneider@gmail.com>
Date: Tue, 09 Jul 2013 18:23:28 +0200
Message-ID: <hf8ot8hnpa93pi3t54c4d5qcc3p5tnb3ca@hive.bjoern.hoehrmann.de>
References: <51DC0F95.7010407@gmail.com>
In-Reply-To: <51DC0F95.7010407@gmail.com>
X-Mailer: Forte Agent 3.3/32.846
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Provags-ID: V03:K0:hoChWIw904rupxVcDZe8hxwSlaZXRZdkqpfvmXv+uw8m0y6viWi r8S+gEmhvawFqjulgC69SkX6TU//g8fuE1aOBb9EQSK7sMrzB4i2/6EYsTYN2m32fz4r7mp CCWz9gAsozSDS+a8Ykqxo9CBjJT3UWgLn/WG/HJZ+5HEjLKv7mIcBvCz798neqk43FKqgsF kkzhjuEH/zn+6d3YJhVgA==
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 16:23:41 -0000

* Peter F. Patel-Schneider wrote:
>My informed understanding of JSON, from reading all the relevant documents, 
>was (and again is) also that JSON numbers are ECMAScript numbers are IEEE 
>floating point doubles (minus some odd bits).  I was astonished to find out 
>that some people disagree, apparently to the point that they believe that 0 is 
>different from .0

RFC 4627 does not define this, but it notes among other things:

  Numeric values that cannot be represented as sequences of digits
  (such as Infinity and NaN) are not permitted.

If JSON used ecmascript semantics then `1e309` would be Infinity,
`-1E-324` would be another way to write `-0.0`.

Certain values like `0.12345678901234567` do have interoperability
problems in practise, consider for instance that

  0.12345678901234568 - 0.12345678901234566 != 0 (ecmascript and Perl)
  0.12345678901234567 - 0.12345678901234566 == 0 (ecmascript)
  0.12345678901234567 - 0.12345678901234568 == 0 (Perl)

and libraries are known round on serialisation; while in ecmascript

  % es -e "JSON.stringify(JSON.parse('[0.12345678901234567]')))"
  "[0.12345678901234566]"

in Perl

  % perl -MJSON -e "print JSON->new->encode(JSON->new
      ->decode('[0.12345678901234567]'))"
  [0.123456789012346]

I think it is well-understood that if you need particularily small,
big, precise or otherwise unusual numbers in JSON then it's best to
encode them as strings so you can do the string-to-number conversion
at a higher level than whatever JSON library you might be using today.
-- 
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/