Skip to content

Commit

Permalink
Encoding NaN/Inf as null instead of throwing.
Browse files Browse the repository at this point in the history
Consistent with behavior in JavaScript and elsewhere.
  • Loading branch information
kring committed Apr 19, 2021
1 parent 3bd97b3 commit b3d7dfa
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions CesiumUtility/include/CesiumUtility/JsonValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ struct JsonValueMissingKey : public std::runtime_error {
: std::runtime_error(key + " is not present in Object") {}
};

struct JsonInvalidDoubleValue : public std::runtime_error {
JsonInvalidDoubleValue(const std::string& err) : std::runtime_error(err) {}
};

struct JsonValueNotRealValue : public std::runtime_error {
JsonValueNotRealValue()
: std::runtime_error("this->value was not double, uint64_t or int64_t") {}
Expand Down Expand Up @@ -90,19 +86,15 @@ class CESIUMUTILITY_API JsonValue final {

/**
* @brief Creates a `Number` JSON value.
* @throws If a NaN or ±Infinity double is supplied.
*
* NaN and ±Infinity are represented as {@link JsonValue::Null}.
*/
JsonValue(double v) {
if (std::isnan(v)) {
throw JsonInvalidDoubleValue("NaN is not a valid JsonValue type.");
if (std::isnan(v) || std::isinf(v)) {
value = nullptr;
} else {
value = v;
}

if (std::isinf(v)) {
throw JsonInvalidDoubleValue("±inf is not a valid JsonValue type.");
}

value = v;
}

/**
Expand Down

0 comments on commit b3d7dfa

Please sign in to comment.