8000 Added `ARDUINOJSON_ENABLE_NAN` to enable NaN in JSON (closes #973) · joglosemarduino/ArduinoJson@7427888 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7427888

Browse files
committed
Added ARDUINOJSON_ENABLE_NAN to enable NaN in JSON (closes bblanchon#973)
1 parent 90c1d54 commit 7427888

File tree

8 files changed

+72
-4
lines changed

8 files changed

+72
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ HEAD
77
* Fixed `deserializeJson()` silently accepting a `Stream*` (issue #978)
88
* Fixed invalid result from `operator|` (issue #981)
99
* Made `deserializeJson()` more picky about trailing characters (issue #980)
10+
* Added `ARDUINOJSON_ENABLE_NAN` to enable NaN in JSON (issue #973)
1011

1112
> ### BREAKING CHANGE
1213
>

src/ArduinoJson/Configuration.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@
135135
#define ARDUINOJSON_DECODE_UNICODE 0
136136
#endif
137137

138+
// Support NaN in JSON
139+
#ifndef ARDUINOJSON_ENABLE_NAN
140+
#define ARDUINOJSON_ENABLE_NAN 1
141+
#endif
142+
138143
// Control the exponentiation threshold for big numbers
139144
// CAUTION: cannot be more that 1e9 !!!!
140145
#ifndef ARDUINOJSON_POSITIVE_EXPONENTIATION_THRESHOLD

src/ArduinoJson/Json/TextFormatter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class TextFormatter {
5252

5353
template <typename T>
5454
void writeFloat(T value) {
55-
if (isnan(value)) return writeRaw("NaN");
55+
if (isnan(value)) return writeRaw(ARDUINOJSON_ENABLE_NAN ? "NaN" : "null");
5656

5757
if (value < 0.0) {
5858
writeRaw('-');

src/ArduinoJson/Namespace.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
#define ARDUINOJSON_CONCAT8(A, B, C, D, E, F, G, H) \
1616
ARDUINOJSON_CONCAT2(ARDUINOJSON_CONCAT4(A, B, C, D), \
1717
ARDUINOJSON_CONCAT4(E, F, G, H))
18+
#define ARDUINOJSON_CONCAT9(A, B, C, D, E, F, G, H, I) \
19+
ARDUINOJSON_CONCAT2(ARDUINOJSON_CONCAT4(A, B, C, D), \
20+
ARDUINOJSON_CONCAT4(E, F, G, ARDUINOJSON_CONCAT2(H, I)))
1821

1922
#define ARDUINOJSON_NAMESPACE \
20-
ARDUINOJSON_CONCAT8(ArduinoJson, ARDUINOJSON_VERSION_MAJOR, \
23+
ARDUINOJSON_CONCAT9(ArduinoJson, ARDUINOJSON_VERSION_MAJOR, \
2124
ARDUINOJSON_VERSION_MINOR, ARDUINOJSON_VERSION_REVISION, \
2225
_, ARDUINOJSON_USE_LONG_LONG, ARDUINOJSON_USE_DOUBLE, \
23-
ARDUINOJSON_DECODE_UNICODE)
26+
ARDUINOJSON_DECODE_UNICODE, ARDUINOJSON_ENABLE_NAN)

src/ArduinoJson/Numbers/parseNumber.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ inline ParsedNumber<TFloat, TUInt> parseNumber(const char *s) {
7171
s++;
7272
break;
7373
}
74-
74+
#if ARDUINOJSON_ENABLE_NAN
7575
if (*s == 'n' || *s == 'N') return traits::nan();
76+
#endif
7677
if (*s == 'i' || *s == 'I')
7778
return is_negative ? -traits::inf() : traits::inf();
7879
if (!isdigit(*s) && *s != '.') return return_type();

test/MixedConfiguration/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ add_executable(MixedConfigurationTests
1212
use_double_1.cpp
1313
use_long_long_0.cpp
1414
use_long_long_1.cpp
15+
enable_nan_0.cpp
16+
enable_nan_1.cpp
1517
)
1618

1719
target_link_libraries(MixedConfigurationTests catch)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#define ARDUINOJSON_ENABLE_NAN 0
2+
#include <ArduinoJson.h>
3+
4+
#include <catch.hpp>
5+
#include <limits>
6+
7+
TEST_CASE("ARDUINOJSON_ENABLE_NAN == 0") {
8+
DynamicJsonDocument doc(4096);
9+
JsonObject root = doc.to<JsonObject>();
10+
11+
SECTION("serializeJson()") {
12+
root["X"] = std::numeric_limits<double>::signaling_NaN();
13+
14+
std::string json;
15+
serializeJson(doc, json);
16+
17+
REQUIRE(json == "{\"X\":null}");
18+
}
19+
20+
SECTION("deserializeJson()") {
21+
auto err = deserializeJson(doc, "{\"X\":NaN}");
22+
23+
REQUIRE(err == DeserializationError::InvalidInput);
24+
}
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#define ARDUINOJSON_ENABLE_NAN 1
2+
#include <ArduinoJson.h>
3+
4+
#include <catch.hpp>
5+
#include <limits>
6+
7+
namespace my {
8+
using ARDUINOJSON_NAMESPACE::isnan;
9+
} // namespace my
10+
11+
TEST_CASE("ARDUINOJSON_ENABLE_NAN == 1") {
12+
DynamicJsonDocument doc(4096);
13+
JsonObject root = doc.to<JsonObject>();
14+
15+
SECTION("serializeJson()") {
16+
root["X"] = std::numeric_limits<double>::signaling_NaN();
17+
18+
std::string json;
19+
serializeJson(doc, json);
20+
21+
REQUIRE(json == "{\"X\":NaN}");
22+
}
23+
24+
SECTION("deserializeJson()") {
25+
auto err = deserializeJson(doc, "{\"X\":NaN}");
26+
float x = doc["X"];
27+
28+
REQUIRE(err == DeserializationError::Ok);
29+
REQUIRE(my::isnan(x));
30+
}
31+
}

0 commit comments

Comments
 (0)
0