See the next code
Document d(kObjectType);
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(buffer);
d.AddMember("x", Value(22), d.GetAllocator());
d.AddMember("y", Value(23), d.GetAllocator());
d.AddMember("x", Value(11), d.GetAllocator());
d.AddMember("x", Value(nan("")), d.GetAllocator());
d.AddMember("Z", Value(11), d.GetAllocator());
d.Accept(writer);
const char* output = buffer.GetString();
printf("%s\n", output);
And output
{
"x": 22,
"y": 23,
"x": 11,
"x":
Nothing written after NaN. This bug here looks like not so bad as empty file. I have spent some time to find it.
This is not a bug. Accept() returns false when it failed to write a strictly valid JSON. And NaN is invalid in JSON. Use kWriteNanAndInfFlag to relax the writer for permitting writing NaN and Inf.
Oh, sorry for the "bug". I used to see exceptions in C++. So with rapidjson to catch exceptions user need to go old C way like:
bool done = doc.Accept(writer);
assert(done);
//or
if(!done)
throw or go your way to handle error;
Isn't it?
using PrettyWriterWithNanAndInf =
rj::PrettyWriter<
/*typename OutputStream */ rj::FileWriteStream,
/*typename SourceEncoding*/ UTF8<>,
/*typename TargetEncoding*/ UTF8<>,
/*typename StackAllocator*/ CrtAllocator,
/*unsigned writeFlags*/ 2 //(unsigned)kWriteNanAndInfFlag
>;
PrettyWriterWithNanAndInf json_writer (os)
Produces a compile-time error:
main.cpp: In constructor 'rapidjson::PrettyWriter<OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags>::PrettyWriter(OutputStream&, StackAllocator*, std::size_t) [with OutputStream = rapidjson::FileWriteStream; SourceEncoding = rapidjson::UTF8<>; TargetEncoding = rapidjson::UTF8<>; StackAllocator = rapidjson::CrtAllocator; unsigned int writeFlags = 2u; std::size_t = long long unsigned int]':
main.cpp:46:45: error: 'const size_t rapidjson::Writer<rapidjson::FileWriteStream, rapidjson::UTF8<>, rapidjson::UTF8<>, rapidjson::CrtAllocator, 0u>::kDefaultLevelDepth' is protected within this context
PrettyWriterWithNanAndInf json_writer (os);
^
No error with /*unsigned writeFlags*/ 0.
@miloyip, Please don't mark this issue invalid. Here really was a bug, isn't it?
I'm having problems using this flag with the PrettyWriter
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter
it says
error: ‘const size_t rapidjson::Writer
I'm using the
rapidjson-devel-1.1.0-2.el7.noarch.rpm found at
https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/r/
if I do
rapidjson::PrettyWriter
instead I get an
error: type ‘rapidjson::Writer
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {}
instead
please advise on how to use kWriteNanAndInfFlag with PrettyWriter
@miloyip
Today I met similar problem of @kdalbey
travis says only gcc-8 met this kind of error. maybe it is due to compiler bug but I do not know gcc-8 or other compilers are right.
https://travis-ci.org/ShigekiKarita/thxx/builds/472064022
@kdalbey, @ShigekiKarita: This was fixed in #909.
No it was not fixed. I found this bug is already reported in https://github.com/Tencent/rapidjson/issues/1338
No it was not fixed. I found this bug is already reported in #1338
But #1338 and #905 are two issues with different root causes. The one reported here is fixed.
Most helpful comment
This is not a bug.
Accept()returnsfalsewhen it failed to write a strictly valid JSON. And NaN is invalid in JSON. UsekWriteNanAndInfFlagto relax the writer for permitting writing NaN and Inf.