Skip to content

Commit

Permalink
rapidjson: add ability to write and parse nan/inf/-inf
Browse files Browse the repository at this point in the history
  • Loading branch information
m7thon committed May 17, 2016
1 parent e79526c commit e5911ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
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

0 comments on commit e5911ad

Please sign in to comment.