Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make double roundtrip when serializing and deserializing to JSON #285

Merged
merged 2 commits into from
May 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/cereal/archives/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace cereal
static Options Default(){ return Options(); }

//! Default options with no indentation
static Options NoIndent(){ return Options( std::numeric_limits<double>::max_digits10, IndentChar::space, 0 ); }
static Options NoIndent(){ return Options( JSONWriter::kDefaultMaxDecimalPlaces, IndentChar::space, 0 ); }

//! The character to use for indenting
enum class IndentChar : char
Expand All @@ -124,7 +124,7 @@ namespace cereal
@param indentChar The type of character to indent with
@param indentLength The number of indentChar to use for indentation
(0 corresponds to no indentation) */
explicit Options( int precision = std::numeric_limits<double>::max_digits10,
explicit Options( int precision = JSONWriter::kDefaultMaxDecimalPlaces,
IndentChar indentChar = IndentChar::space,
unsigned int indentLength = 4 ) :
itsPrecision( precision ),
Expand Down Expand Up @@ -420,7 +420,7 @@ namespace cereal
itsNextName( nullptr ),
itsReadStream(stream)
{
itsDocument.ParseStream<0>(itsReadStream);
itsDocument.ParseStream<rapidjson::kParseFullPrecisionFlag>(itsReadStream);
if (itsDocument.IsArray())
itsIteratorStack.emplace_back(itsDocument.Begin(), itsDocument.End());
else
Expand Down
15 changes: 13 additions & 2 deletions include/cereal/external/rapidjson/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "internal/meta.h"
#include "internal/stack.h"
#include "internal/strtod.h"
#include <limits>

#if defined(CEREAL_RAPIDJSON_SIMD) && defined(_MSC_VER)
#include <intrin.h>
Expand Down Expand Up @@ -699,14 +700,18 @@ class GenericReader {
}

template<unsigned parseFlags, typename InputStream, typename Handler>
void ParseNull(InputStream& is, Handler& handler) {
void ParseNullOrNan(InputStream& is, Handler& handler) {
CEREAL_RAPIDJSON_ASSERT(is.Peek() == 'n');
is.Take();

if (CEREAL_RAPIDJSON_LIKELY(Consume(is, 'u') && Consume(is, 'l') && Consume(is, 'l'))) {
if (CEREAL_RAPIDJSON_UNLIKELY(!handler.Null()))
CEREAL_RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell());
}
else if (Consume(is, 'a') && Consume(is, 'n')) {
if (CEREAL_RAPIDJSON_UNLIKELY(!handler.Double( std::numeric_limits<double>::quiet_NaN() )))
CEREAL_RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell());
}
else
CEREAL_RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell());
}
Expand Down Expand Up @@ -1178,6 +1183,12 @@ class GenericReader {
significandDigit++;
}
}
else if (CEREAL_RAPIDJSON_UNLIKELY(Consume(s, 'i')) && Consume(s, 'n') && Consume(s, 'f')) {
double inf = std::numeric_limits<double>::infinity();
if (CEREAL_RAPIDJSON_UNLIKELY(!handler.Double(minus ? -inf : inf)))
CEREAL_RAPIDJSON_PARSE_ERROR(kParseErrorTermination, startOffset);
return;
}
else
CEREAL_RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell());

Expand Down Expand Up @@ -1369,7 +1380,7 @@ class GenericReader {
template<unsigned parseFlags, typename InputStream, typename Handler>
void ParseValue(InputStream& is, Handler& handler) {
switch (is.Peek()) {
case 'n': ParseNull <parseFlags>(is, handler); break;
case 'n': ParseNullOrNan<parseFlags>(is, handler); break;
case 't': ParseTrue <parseFlags>(is, handler); break;
case 'f': ParseFalse <parseFlags>(is, handler); break;
case '"': ParseString<parseFlags>(is, handler); break;
Expand Down
19 changes: 18 additions & 1 deletion include/cereal/external/rapidjson/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,25 @@ class Writer {
}

bool WriteDouble(double d) {
if (internal::Double(d).IsNanOrInf())
if (internal::Double(d).IsNanOrInf()) {
if (internal::Double(d).IsNan()) {
PutReserve(*os_, 3);
PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>('n'));
PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>('a'));
PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>('n'));
} else {
if (internal::Double(d).Sign()) {
PutReserve(*os_, 4);
PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>('-'));
} else {
PutReserve(*os_, 3);
}
PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>('i'));
PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>('n'));
PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>('f'));
}
return false;
}

char buffer[25];
char* end = internal::dtoa(d, buffer, maxDecimalPlaces_);
Expand Down