Rapidjson: Writer can't handle double value with infinity

Created on 1 Jun 2017  路  3Comments  路  Source: Tencent/rapidjson

342a616a7ff83279e96b01effdeeb7936ace75fb

#include` <iostream>
#include <limits>
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"

using namespace rapidjson;

int main ()
{
  Document d;
  d.SetObject ();

  Value v1 (3.14);
  double tmp = std::numeric_limits<double>::infinity();
  Value v2 (tmp);
  Value v3 (345);

  d.AddMember ("foo", v1, d.GetAllocator());
  d.AddMember ("bar", v2, d.GetAllocator());
  d.AddMember ("baz", v3, d.GetAllocator());

  StringBuffer buffer;
  Writer<StringBuffer> writer(buffer);
  d.Accept(writer);

  std::cout << buffer.GetString() << std::endl;

  return 0;
}

writes to stdout:

{"foo":3.14,"bar":

Same issue with signaling_NaN and quiet_NaN.
I don't know if JSON should support +/-Inf and NaN but at least it shouldn't silently stop writing and leaving the generated JSON string useless.

question

Most helpful comment

You can use kWriteNanAndInfFlag as documented.

All 3 comments

Inf and NaN are not permitted in JSON. The standard says "Numeric values that cannot be represented as sequences of digits (such as Infinity and NaN) are not permitted.".

Did you check the output from d.Accept(writer) ? I guess it will tell you that the writing did not succeed.

You can use kWriteNanAndInfFlag as documented.

For invalid input under the current options, Accept() returns false to indicate failure to write a well-formed JSON.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lelit picture lelit  路  5Comments

lixiaofan0 picture lixiaofan0  路  3Comments

accelerated picture accelerated  路  6Comments

dan-ryan picture dan-ryan  路  6Comments

lasalvavida picture lasalvavida  路  5Comments