<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to 89: Invalid JSON serialization of NaN values</title><link>https://sourceforge.net/p/cppcms/bugs/89/</link><description>Recent changes to 89: Invalid JSON serialization of NaN values</description><atom:link href="https://sourceforge.net/p/cppcms/bugs/89/feed.rss" rel="self"/><language>en</language><lastBuildDate>Wed, 04 Sep 2013 09:11:21 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/cppcms/bugs/89/feed.rss" rel="self" type="application/rss+xml"/><item><title>#89 Invalid JSON serialization of NaN values</title><link>https://sourceforge.net/p/cppcms/bugs/89/?limit=50#3889</link><description>&lt;div class="markdown_content"&gt;&lt;p&gt;fjfj&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Anonymous</dc:creator><pubDate>Wed, 04 Sep 2013 09:11:21 -0000</pubDate><guid>https://sourceforge.net0a1d778456a032c0e0d5a7cef9df573c575ab783</guid></item><item><title>#89 Invalid JSON serialization of NaN values</title><link>https://sourceforge.net/p/cppcms/bugs/89/?limit=25#f651</link><description>&lt;div class="markdown_content"&gt;&lt;p&gt;For the record, my quick-and-dirty fix - assuming the silent conversion to &lt;code&gt;null&lt;/code&gt; is desired - was:&lt;/p&gt;
&lt;p&gt;1) Add a usable implementation of &lt;code&gt;isfinite&lt;/code&gt; from Boost as &lt;code&gt;cppcms::json::detail::isfinite_impl()&lt;/code&gt; (a proper solution would be to add the relevant parts of Boost.Math to &lt;code&gt;cppcms_boost&lt;/code&gt; and/or use an external Boost installation - I don't know the exact "CppCMS way" for this)&lt;/p&gt;
&lt;p&gt;2) In the implementation of &lt;code&gt;value::write_value()&lt;/code&gt; in &lt;strong&gt;&lt;em&gt;json.cpp&lt;/em&gt;&lt;/strong&gt;, change:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;is_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;setprecision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;numeric_limits&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;digits10&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;1&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;to&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;is_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;!&lt;span class="n"&gt;cppcms&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;detail&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;isfinite_impl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
        &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&amp;quot;&lt;span class="n"&gt;null&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;   
        &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;setprecision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;numeric_limits&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;digits10&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;1&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Moti Zilberman</dc:creator><pubDate>Tue, 05 Feb 2013 16:05:06 -0000</pubDate><guid>https://sourceforge.net7738dfc9e505b3fcf6acd8511db50b0c8e984213</guid></item><item><title>#89 Invalid JSON serialization of NaN values</title><link>https://sourceforge.net/p/cppcms/bugs/89/?limit=25#5806</link><description>&lt;div class="markdown_content"&gt;&lt;p&gt;Artyom, meaning no disrespect - CppCMS is cool, which is why I'm taking the time to do this :) - I think you went outside the JSON-relevant part of that specification.&lt;/p&gt;
&lt;p&gt;Here's my more detailed take on the issue:&lt;/p&gt;
&lt;p&gt;1) &lt;a class="" href="http://www.ietf.org/rfc/rfc4627.txt"&gt;RFC 4627&lt;/a&gt; says (on page 2): &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Numeric values that cannot be represented as sequences of digits&lt;br /&gt;
   (such as Infinity and NaN) are not permitted.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;2) &lt;a class="" href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.12.3"&gt;ECMA 262 section 15.12.3&lt;/a&gt; (the one I linked to) specifies the &lt;code&gt;JSON.stringify&lt;/code&gt; function's behavior. I think you'll agree that this is an authoritative source on what JavaScript does in the equivalent case. JavaScript/ECMAScript &lt;em&gt;has&lt;/em&gt; NaNs and infinities so there is indeed an exact equivalent case of "there's a NaN in my data - how does that translate to JSON?". Quoting the interesting parts:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The abstract operation Str(key, holder) &lt;span&gt;[...]&lt;/span&gt;&lt;br /&gt;
  1. Let value be the result of &lt;span&gt;[...]&lt;/span&gt;&lt;br /&gt;
&lt;span&gt;[...]&lt;/span&gt;&lt;br /&gt;
&lt;strong&gt;9. If Type(value) is Number&lt;br /&gt;
    If value is finite then return ToString(value).&lt;br /&gt;
    Else, return "null".&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;(Emphasis mine)&lt;/p&gt;
&lt;p&gt;From #1 I gather that there is no JSON &lt;em&gt;representation&lt;/em&gt; for NaNs and infinities. On its own, it would mean any implementation is free to do what it deems correct, including throwing an exception. &lt;strong&gt;But&lt;/strong&gt;, in #2, JavaScript already sets an example for an implementation's &lt;em&gt;behavior&lt;/em&gt;. And according to this example (which IMHO CppCMS should follow) it's not an error to &lt;em&gt;try&lt;/em&gt; to serialize a NaN or Infinity to JSON - you just get a &lt;code&gt;null&lt;/code&gt; out of it. So from the principle of least surprise (among other things), I think no exception should be thrown, at least not by default.&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Moti Zilberman</dc:creator><pubDate>Tue, 05 Feb 2013 15:49:34 -0000</pubDate><guid>https://sourceforge.net7261d27ac4bb40df870b05df553f10242daa449d</guid></item><item><title>#89 Invalid JSON serialization of NaN values</title><link>https://sourceforge.net/p/cppcms/bugs/89/?limit=25#749f</link><description>&lt;div class="markdown_content"&gt;&lt;p&gt;Actually I'm not sure that using null is correct:&lt;/p&gt;
&lt;p&gt;According to the link:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;Number&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt; &lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;From there to: &lt;a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.8"&gt;http://www.ecma-international.org/ecma-262/5.1/#sec-9.8&lt;/a&gt;&lt;br /&gt;
And then to &lt;a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.8.1"&gt;http://www.ecma-international.org/ecma-262/5.1/#sec-9.8.1&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;NaN value should be converted to NaN.&lt;/p&gt;
&lt;p&gt;But NaN is not part of the JSON specifications: &lt;a href="http://www.json.org/"&gt;http://www.json.org/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So technically NaN can't be presented in JSON. &lt;/p&gt;
&lt;p&gt;Probably in case of NaN I should throw some kind of exception. Not sure thou.&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Artyom Beilis</dc:creator><pubDate>Tue, 05 Feb 2013 14:47:24 -0000</pubDate><guid>https://sourceforge.netcafd2eb0e3bb3c67fda81afdd938affcb6524257</guid></item><item><title>Invalid JSON serialization of NaN values</title><link>https://sourceforge.net/p/cppcms/bugs/89/</link><description>&lt;div class="markdown_content"&gt;&lt;p&gt;Hi,&lt;br /&gt;
CppCMS's JSON encoding facility encodes NaN values as &lt;code&gt;-1.#QNAN&lt;/code&gt; (at least in my test case, with a particular data set, on MSVC 2012 / Windows). This isn't valid JSON and therefore causes client code to error out.&lt;/p&gt;
&lt;p&gt;The applicable standard (ECMA-262) &lt;a class="" href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.12.3"&gt;seems to specify&lt;/a&gt; that the correct serialization of any non-finite number values (NaNs, infinities, etc.) is &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Moti Zilberman</dc:creator><pubDate>Tue, 05 Feb 2013 14:19:12 -0000</pubDate><guid>https://sourceforge.net98a0e9f99eeee1b0455cd716d72694207f64fc8a</guid></item></channel></rss>