Back Midas Rome Roody Rootana
  Midas DAQ System  Not logged in ELOG logo
Entry  05 Apr 2013, Konstantin Olchanski, Info, ODB JSON support 
    Reply  10 May 2013, Konstantin Olchanski, Info, mhttpd JSON support 
       Reply  17 May 2013, Konstantin Olchanski, Info, mhttpd JSON-P support 
          Reply  31 May 2013, Konstantin Olchanski, Info, mhttpd JSON-P support 
    Reply  27 Sep 2013, Konstantin Olchanski, Info, ODB JSON support 
       Reply  09 Oct 2013, Konstantin Olchanski, Info, ODB JSON support 
          Reply  17 Mar 2014, Konstantin Olchanski, Info, ODB JSON support 
             Reply  12 Apr 2022, Konstantin Olchanski, Info, ODB JSON support 
                Reply  13 Apr 2022, Stefan Ritt, Info, ODB JSON support 
                   Reply  13 Apr 2022, Konstantin Olchanski, Info, ODB JSON support 
Message ID: 918     Entry time: 09 Oct 2013     In reply to: 916     Reply to this: 988
Author: Konstantin Olchanski 
Topic: Info 
Subject: ODB JSON support 
> > odbedit can now save ODB in JSON-formatted files.
> A bug was reported in my JSON ODB encoder: NaN values are not encoded correctly.

Tested the browser-builtin JSON.stringify() function in google-chrome, firefox, safari, opera:
everybody encodes numeric values NaN and Inf as JSON value [null].

To me, this clearly demonstrates a severe defect in the JSON standard and in it's Javascript implementation:
a) NaN, Inf and -Inf are valid, useful and commonly used numeric values defined by the IEEE754/854 standard (as opposed to the special value "-0", which is also defined by the standard, but is not nearly as useful)
b) they are all distinct numeric values, encoding them all into the same JSON value [null] is the same as encoding all even numbers into the JSON value [42].
c) on the decoding end, JSON value [null] is decoded into Javascript value [null], which works as 0 for numeric computation, so effectively NaN, Inf and -Inf are made equal to zero. A neat trick.

Note that (c) - NaN, Inf is same as 0 - eventually produces incorrect numerical results by breaking the IEEE754/854 standard specification that number+NaN->NaN, number+infinity->infinity, etc.

In MIDAS we have a requirement that results be numerically correct: if an ODB value is "infinity", the corresponding web page should not show "0".

In addition we have a requirement that JSON encoding should be lossess: i.e. ODB contents encoded by JSON should decode back into the same ODB contents.

To satisfy both requirements, I now encode NaN, Inf and -Inf as JSON string values "NaN", "Infinity" and "-Infinity". (Corresponding to the respective Javascript values).

Notes:
1) this is valid JSON
2) it survives decode/encode in the browser (ODBMCopy()/JSON.parse/modify some values/JSON.stringify/ODBMPaste() does not destroy these special values)
3) it is numerically correct for "NaN" values (Javascript [1+"NaN"] -> NaN)
4) it fails in an obvious way for Inf and -Inf values (Javascript [1+"Infinity"] is NaN instead of Infinity).

https://bitbucket.org/tmidas/midas/commits/82dd203cc95dacb6ec9c0a24bc97ffd45bb58427
K.O.
ELOG V3.1.4-2e1708b5