8000 Added `ARDUINOJSON_ENABLE_INFINITY` to enable Infinity in JSON · joglosemarduino/ArduinoJson@80a02cd · GitHub
[go: up one dir, main page]

Skip to content

Commit 80a02cd

Browse files
committed
Added ARDUINOJSON_ENABLE_INFINITY to enable Infinity in JSON
1 parent 7427888 commit 80a02cd

File tree

8 files changed

+107
-10
lines changed

8 files changed

+107
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ HEAD
88
* Fixed invalid result from `operator|` (issue #981)
99
* Made `deserializeJson()` more picky about trailing characters (issue #980)
1010
* Added `ARDUINOJSON_ENABLE_NAN` to enable NaN in JSON (issue #973)
11+
* Added `ARDUINOJSON_ENABLE_INFINITY` to enable Infinity in JSON
1112

1213
> ### BREAKING CHANGE
1314
>

src/ArduinoJson/Configuration.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@
140140
#define ARDUINOJSON_ENABLE_NAN 1
141141
#endif
142142

143+
// Support Infinity in JSON
144+
#ifndef ARDUINOJSON_ENABLE_INFINITY
145+
#define ARDUINOJSON_ENABLE_INFINITY 1
146+
#endif
147+
143148
// Control the exponentiation threshold for big numbers
144149
// CAUTION: cannot be more that 1e9 !!!!
145150
#ifndef ARDUINOJSON_POSITIVE_EXPONENTIATION_THRESHOLD

src/ArduinoJson/Json/TextFormatter.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,21 @@ class TextFormatter {
5454
void writeFloat(T value) {
5555
if (isnan(value)) return writeRaw(ARDUINOJSON_ENABLE_NAN ? "NaN" : "null");
5656

57+
#if ARDUINOJSON_ENABLE_INFINITY
5758
if (value < 0.0) {
5859
writeRaw('-');
5960
value = -value;
6061
}
6162

6263
if (isinf(value)) return writeRaw("Infinity");
64+
#else
65+
if (isinf(value)) return writeRaw("null");
66+
67+
if (value < 0.0) {
68+
writeRaw('-');
69+
value = -value;
70+
}
71+
#endif
6372

6473
FloatParts<T> parts(value);
6574

src/ArduinoJson/Namespace.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@
1010

1111
#define ARDUINOJSON_DO_CONCAT(A, B) A##B
1212
#define ARDUINOJSON_CONCAT2(A, B) ARDUINOJSON_DO_CONCAT(A, B)
13+
#define ARDUINOJSON_CONCAT3(A, B, C) \
14+
ARDUINOJSON_CONCAT2(A, ARDUINOJSON_CONCAT2(B, C))
1315
#define ARDUINOJSON_CONCAT4(A, B, C, D) \
1416
ARDUINOJSON_CONCAT2(ARDUINOJSON_CONCAT2(A, B), ARDUINOJSON_CONCAT2(C, D))
1517
#define ARDUINOJSON_CONCAT8(A, B, C, D, E, F, G, H) \
1618
ARDUINOJSON_CONCAT2(ARDUINOJSON_CONCAT4(A, B, C, D), \
1719
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)))
20+
#define ARDUINOJSON_CONCAT10(A, B, C, D, E, F, G, H, I, J) \
21+
ARDUINOJSON_CONCAT8(A, B, C, D, E, F, G, ARDUINOJSON_CONCAT3(H, I, J))
2122

22-
#define ARDUINOJSON_NAMESPACE \
23-
ARDUINOJSON_CONCAT9(ArduinoJson, ARDUINOJSON_VERSION_MAJOR, \
24-
ARDUINOJSON_VERSION_MINOR, ARDUINOJSON_VERSION_REVISION, \
25-
_, ARDUINOJSON_USE_LONG_LONG, ARDUINOJSON_USE_DOUBLE, \
26-
ARDUINOJSON_DECODE_UNICODE, ARDUINOJSON_ENABLE_NAN)
23+
#define ARDUINOJSON_NAMESPACE \
24+
ARDUINOJSON_CONCAT10( \
25+
ArduinoJson, ARDUINOJSON_VERSION_MAJOR, ARDUINOJSON_VERSION_MINOR, \
26+
ARDUINOJSON_VERSION_REVISION, _, ARDUINOJSON_USE_LONG_LONG, \
27+
ARDUINOJSON_USE_DOUBLE, ARDUINOJSON_DECODE_UNICODE, \
28+
ARDUINOJSON_ENABLE_NAN, ARDUINOJSON_ENABLE_INFINITY)

src/ArduinoJson/Numbers/parseNumber.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,16 @@ inline ParsedNumber<TFloat, TUInt> parseNumber(const char *s) {
7171
s++;
7272
break;
7373
}
74+
7475
#if ARDUINOJSON_ENABLE_NAN
7576
if (*s == 'n' || *s == 'N') return traits::nan();
7677
#endif
78+
79+
#if ARDUINOJSON_ENABLE_INFINITY
7780
if (*s == 'i' || *s == 'I')
7881
return is_negative ? -traits::inf() : traits::inf();
82+
#endif
83+
7984
if (!isdigit(*s) && *s != '.') return return_type();
8085

8186
mantissa_t mantissa = 0;

test/MixedConfiguration/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ set(CMAKE_CXX_STANDARD 11)
88
add_executable(MixedConfigurationTests
99
decode_unicode_0.cpp
1010
decode_unicode_1.cpp
11+
enable_nan_0.cpp
12+
enable_nan_1.cpp
13+
enable_infinity_0.cpp
F438 14+
enable_infinity_1.cpp
1115
use_double_0.cpp
1216
use_double_1.cpp
1317
use_long_long_0.cpp
1418
use_long_long_1.cpp
15-
enable_nan_0.cpp
16-
enable_nan_1.cpp
1719
)
1820

1921
target_link_libraries(MixedConfigurationTests catch)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#define ARDUINOJSON_ENABLE_INFINITY 0
2+
#include <ArduinoJson.h>
3+
4+
#include <catch.hpp>
5+
#include <limits>
6+
7+
static void assertParseFails(const char* json) {
8+
DynamicJsonDocument doc(4096);
9+
auto err = deserializeJson(doc, json);
10+
11+
REQUIRE(err == DeserializationError::InvalidInput);
12+
}
13+
14+
static void assertJsonEquals(const JsonDocument& doc,
15+
std::string expectedJson) {
16+
std::string actualJson;
17+
serializeJson(doc, actualJson);
18+
REQUIRE(actualJson == expectedJson);
19+
}
20+
21+
TEST_CASE("ARDUINOJSON_ENABLE_INFINITY == 0") {
22+
SECTION("serializeJson()") {
23+
DynamicJsonDocument doc(4096);
24+
doc.add(std::numeric_limits<double>::infinity());
25+
doc.add(-std::numeric_limits<double>::infinity());
26+
27+
assertJsonEquals(doc, "[null,null]");
28+
}
29+
30+
SECTION("deserializeJson()") {
31+
assertParseFails("{\"X\":Infinity}");
32+
assertParseFails("{\"X\":-Infinity}");
33+
assertParseFails("{\"X\":+Infinity}");
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#define ARDUINOJSON_ENABLE_INFINITY 1
2+
#include <ArduinoJson.h>
3+
4+
#include <catch.hpp>
5+
#include <limits>
6+
7+
namespace my {
8+
using ARDUINOJSON_NAMESPACE::isinf;
9+
} // namespace my
10+
11+
TEST_CASE("ARDUINOJSON_ENABLE_INFINITY == 1") {
12+
DynamicJsonDocument doc(4096);
13+
14+
SECTION("serializeJson()") {
15+
doc.add(std::numeric_limits<double>::infinity());
16+
doc.add(-std::numeric_limits<double>::infinity());
17+
18+
std::string json;
19+
serializeJson(doc, json);
20+
21+
REQUIRE(json == "[Infinity,-Infinity]");
22+
}
23+
24+
SECTION("deserializeJson()") {
25+
auto err = deserializeJson(doc, "[Infinity,-Infinity,+Infinity]");
26+
float a = doc[0];
27+
float b = doc[1];
28+
float c = doc[2];
29+
30+
REQUIRE(err == DeserializationError::Ok);
31+
REQUIRE(my::isinf(a));
32+
REQUIRE(a > 0);
33+
REQUIRE(my::isinf(b));
34+
REQUIRE(b < 0);
35+
REQUIRE(my::isinf(c));
36+
REQUIRE(c > 0);
37+
}
38+
}

0 commit comments

Comments
 (0)
0