danielaparker / jsoncons

A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
https://danielaparker.github.io/jsoncons
Other
726 stars 164 forks source link

NaN value in an array add unexpected comma #499

Closed AKA-MMM closed 7 months ago

AKA-MMM commented 7 months ago

Hi, I'm trying to serialize an array with the values [0, 2, NaN, NaN] to obtain a JSON Array representation. For this, I have configured the encoder as follow: ` jsoncons::basic_json_options options;

options.nan_to_str("NaN");

options.inf_to_str("Inf");

options.neginf_to_str("NegInf");

m_encoder= new jsoncons::compact_json_stream_encoder(p_io, options);

` So, I expect the following result: [0, 2, "NaN", "NaN"]. But, the obtained result is [0, 2, "NaN",,"NaN"]. E.g. I have an unexpected comma between the two strings "NaN". Without the options, the result is [0, 2, Null, Null] (no unexpected comma). This difference comes from the way NaN (and also infinities) are managed. Without option (or with the option nan_to_num), visit_double() just add the "Null" string by sink.append. With the option nan_to_str, visit_double() calls visit_string(). Hence visit_string() adds a first comma. And visit_double() adds the second comma.

danielaparker commented 7 months ago

Thanks for reporting this, we'll look into it.

danielaparker commented 7 months ago

Fixed on master