8000 Removed configurable number of decimal places (issues #288, #427 and … · trinhduc/ArduinoJson@cda05ae · GitHub
[go: up one dir, main page]

Skip to content

Commit cda05ae

Browse files
committed
Removed configurable number of decimal places (issues bblanchon#288, bblanchon#427 and bblanchon#506)
1 parent 639286f commit cda05ae

33 files changed

+441
-385
lines changed

CHANGELOG.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,34 @@ ArduinoJson: change log
44
HEAD
55
----
66

7+
* Removed configurable number of decimal places (issues #288, #427 and #506)
8+
* Changed exponentation thresholds to `1e7` and `1e-5` (issues #288, #427 and #506)
9+
* `JsonVariant::is<double>()` now returns `true` for integers
710
* Fixed error `IsBaseOf is not a member of ArduinoJson::TypeTraits` (issue #495)
811
* Fixed error `forming reference to reference` (issue #495)
912

13+
### BREAKING CHANGES :warning:
14+
15+
| Old syntax | New syntax |
16+
|---------------------------------|---------------------|
17+
| `double_with_n_digits(3.14, 2)` | `3.14` |
18+
| `float_with_n_digits(3.14, 2)` | `3.14f` |
19+
| `obj.set("key", 3.14, 2)` | `obj["key"] = 3.14` |
20+
| `arr.add(3.14, 2)` | `arr.add(3.14)` |
21+
22+
| Input | Old output | New output |
23+
|-----------|------------|------------|
24+
| `3.14159` | `3.14` | `3.14159` |
25+
| `42.0` | `42.00` | `42` |
26+
| `0.0` | `0.00` | `0` |
27+
28+
| Expression | Old result | New result |
29+
|--------------------------------|------------|------------|
30+
| `JsonVariant(42).is<int>()` | `true` | `true` |
31+
| `JsonVariant(42).is<float>()` | `false` | `true` |
32+
| `JsonVariant(42).is<double>()` | `false` | `true` |
33+
34+
1035
v5.9.0
1136
------
1237

@@ -59,7 +84,8 @@ v5.8.0
5984
* Added support for `Stream` (issue #300)
6085
* Reduced memory consumption by not duplicating spaces and comments
6186

62-
**BREAKING CHANGES**:
87+
### BREAKING CHANGES :warning:
88+
6389

6490
`JsonBuffer::parseObject()` and `JsonBuffer::parseArray()` have been pulled down to the derived classes `DynamicJsonBuffer` and `StaticJsonBufferBase`.
6591

@@ -108,7 +134,8 @@ v5.7.0
108134
* Added example `StringExample.ino` to show where `String` can be used
109135
* Increased default nesting limit to 50 when compiled for a computer (issue #349)
110136

111-
**BREAKING CHANGES**:
137+
### BREAKING CHANGES :warning:
138+
112139

113140
The non-template functions `JsonObject::get()` and `JsonArray.get()` have been removed. This means that you need to explicitely tell the type you expect in return.
114141

@@ -235,7 +262,8 @@ v5.0.7
235262
* Made library easier to use from a CMake project: simply `add_subdirectory(ArduinoJson/src)`
236263
* Changed `String` to be a `typedef` of `std::string` (issues #142 and #161)
237264

238-
**BREAKING CHANGES**:
265+
### BREAKING CHANGES :warning:
266+
239267
- `JsonVariant(true).as<String>()` now returns `"true"` instead of `"1"`
240268
- `JsonVariant(false).as<String>()` now returns `"false"` instead of `"0"`
241269

@@ -291,7 +319,8 @@ v5.0.0
291319
* Redesigned `JsonVariant` to leverage converting constructors instead of assignment operators (issue #66)
292320
* Switched to new the library layout (requires Arduino 1.0.6 or above)
293321

294-
**BREAKING CHANGES**:
322+
### BREAKING CHANGES :warning:
323+
295324
- `JsonObject::add()` was renamed to `set()`
296325
- `JsonArray::at()` and `JsonObject::at()` were renamed to `get()`
297326
- Number of digits of floating point value are now set with `double_with_n_digits()`

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ root["sensor"] = "gps";
6868
root["time"] = 1351824120;
6969

7070
JsonArray& data = root.createNestedArray("data");
71-
data.add(48.756080, 6); // 6 is the number of decimals to print
72-
data.add(2.302038, 6); // if not specified, 2 digits are printed
71+
data.add(48.756080);
72+
data.add(2.302038);
7373

7474
root.printTo(Serial);
7575
// This prints:

examples/JsonGeneratorExample/JsonGeneratorExample.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ void setup() {
4444
// It's also possible to create the array separately and add it to the
4545
// JsonObject but it's less efficient.
4646
JsonArray& data = root.createNestedArray("data");
47-
data.add(double_with_n_digits(48.756080, 6));
48-
data.add(double_with_n_digits(2.302038, 6));
47+
data.add(48.756080);
48+
data.add(2.302038);
4949

5050
root.printTo(Serial);
5151
// This prints:

src/ArduinoJson/Configuration.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@
1212
#define ARDUINOJSON_ENABLE_DEPRECATED 1
1313
#endif
1414

15+
// control the exponentiation threshold for big numbers
16+
// CAUTION: cannot be more that 1e9 !!!!
17+
#ifndef ARDUINOJSON_POSITIVE_EXPONENTIATION_THRESHOLD
18+
#define ARDUINOJSON_POSITIVE_EXPONENTIATION_THRESHOLD 1e7
19+
#endif
20+
21+
// control the exponentiation threshold for small numbers
22+
#ifndef ARDUINOJSON_NEGATIVE_EXPONENTIATION_THRESHOLD
23+
#define ARDUINOJSON_NEGATIVE_EXPONENTIATION_THRESHOLD 1e-5
24+
#endif
25+
1526
#ifdef ARDUINO // assume this is an embedded platform
1627

1728
// store using float instead of double to reduce the memory usage (issue #134)

src/ArduinoJson/Data/JsonVariantType.hpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,11 @@ enum JsonVariantType {
2020
JSON_UNPARSED, // JsonVariant contains an unparsed string
2121
JSON_STRING, // JsonVariant stores a const char*
2222
JSON_BOOLEAN, // JsonVariant stores a bool
23-
JSON_POSITIVE_INTEGER, // JsonVariant stores an unsigned long
24-
JSON_NEGATIVE_INTEGER, // JsonVariant stores an unsigned long that must be
25-
// negated
23+
JSON_POSITIVE_INTEGER, // JsonVariant stores an JsonUInt
24+
JSON_NEGATIVE_INTEGER, // JsonVariant stores an JsonUInt that must be negated
2625
JSON_ARRAY, // JsonVariant stores a pointer to a JsonArray
2726
JSON_OBJECT, // JsonVariant stores a pointer to a JsonObject
28-
29-
// The following values are reserved for float values
30-
// Multiple values are used for double, depending on the number of decimal
31-
// digits that must be printed in the JSON output.
32-
// This little trick allow to save one extra member in JsonVariant
33-
JSON_FLOAT_0_DECIMALS
34-
// JSON_FLOAT_1_DECIMAL
35-
// JSON_FLOAT_2_DECIMALS
36-
// ...
27+
JSON_FLOAT // JsonVariant stores a JsonFloat
3728
};
3829
}
3930
}

src/ArduinoJson/JsonArray.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
7676
// bool add(TValue value, uint8_t decimals);
7777
// TValue = float, double
7878
template <typename T>
79-
bool add(T value, uint8_t decimals) {
80-
return add_impl<const JsonVariant &>(JsonVariant(value, decimals));
79+
DEPRECATED("Second argument is not supported anymore")
80+
bool add(T value, uint8_t) {
81+
return add_impl<const JsonVariant &>(JsonVariant(value));
8182
}
8283

8384
// Sets the value at specified index.

src/ArduinoJson/JsonArraySubscript.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
8181
// bool set(TValue, uint8_t decimals);
8282
// TValue = float, double
8383
template <typename TValue>
84-
FORCE_INLINE bool set(const TValue& value, uint8_t decimals) {
85-
return _array.set(_index, value, decimals);
84+
DEPRECATED("Second argument is not supported anymore")
85+
FORCE_INLINE bool set(const TValue& value, uint8_t) {
86+
return _array.set(_index, value);
8687
}
8788

8889
private:

src/ArduinoJson/JsonObject.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,25 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
134134
// TKey = const std::string&, const String&
135135
// TValue = float, double
136136
template <typename TValue, typename TString>
137+
DEPRECATED("Second argument is not supported anymore")
137138
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<TValue>::value &&
138139
!TypeTraits::IsArray<TString>::value,
139140
bool>::type
140-
set(const TString& key, TValue value, uint8_t decimals) {
141-
return set_impl<const TString&, const JsonVariant&>(
142-
key, JsonVariant(value, decimals));
141+
set(const TString& key, TValue value, uint8_t) {
142+
return set_impl<const TString&, const JsonVariant&>(key,
143+
JsonVariant(value));
143144
}
144145
//
145146
// bool set(TKey, TValue, uint8_t decimals);
146147
// TKey = const char*, const char[N], const FlashStringHelper*
147148
// TValue = float, double
148149
template <typename TValue, typename TString>
150+
DEPRECATED("Second argument is not supported anymore")
149151
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<TValue>::value,
150152
bool>::type
151-
set(const TString* key, TValue value, uint8_t decimals) {
152-
return set_impl<const TString*, const JsonVariant&>(
153-
key, JsonVariant(value, decimals));
153+
set(const TString* key, TValue value, uint8_t) {
154+
return set_impl<const TString*, const JsonVariant&>(key,
155+
JsonVariant(value));
154156
}
155157

156158
// Gets the value associated with the specified key.

src/ArduinoJson/JsonObjectSubscript.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ class JsonObjectSubscript
9393
// bool set(TValue, uint8_t decimals);
9494
// TValue = float, double
9595
template <typename TValue>
96-
FORCE_INLINE bool set(const TValue& value, uint8_t decimals) {
97-
return _object.set(_key, value, decimals);
96+
DEPRECATED("Second argument is not supported anymore")
97+
FORCE_INLINE bool set(const TValue& value, uint8_t) {
98+
return _object.set(_key, value);
9899
}
99100

100101
private:

src/ArduinoJson/JsonVariant.hpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,22 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
5656
}
5757

5858
// Create a JsonVariant containing a floating point value.
59-
// The second argument specifies the number of decimal digits to write in
60-
// the JSON string.
61-
// JsonVariant(double value, uint8_t decimals);
62-
// JsonVariant(float value, uint8_t decimals);
59+
// JsonVariant(double value);
60+
// JsonVariant(float value);
6361
template <typename T>
64-
JsonVariant(T value, uint8_t decimals = 2,
62+
JsonVariant(T value, typename TypeTraits::EnableIf<
63+
TypeTraits::IsFloatingPoint<T>::value>::type * = 0) {
64+
using namespace Internals;
65+
_type = JSON_FLOAT;
66+
_content.asFloat = static_cast<JsonFloat>(value);
67+
}
68+
template <typename T>
69+
DEPRECATED("Second argument is not supported anymore")
70+
JsonVariant(T value, uint8_t,
6571
typename TypeTraits::EnableIf<
6672
TypeTraits::IsFloatingPoint<T>::value>::type * = 0) {
6773
using namespace Internals;
68-
_type = static_cast<JsonVariantType>(JSON_FLOAT_0_DECIMALS + decimals);
74+
_type = JSON_FLOAT;
6975
_content.asFloat = static_cast<JsonFloat>(value);
7076
}
7177

@@ -342,11 +348,13 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
342348
Internals::JsonVariantContent _content;
343349
};
344350

345-
inline JsonVariant float_with_n_digits(float value, uint8_t digits) {
346-
return JsonVariant(value, digits);
351+
DEPRECATED("Decimal places are ignored, use the float value instead")
352+
inline JsonVariant float_with_n_digits(float value, uint8_t) {
353+
return JsonVariant(value);
347354
}
348355

349-
inline JsonVariant double_with_n_digits(double value, uint8_t digits) {
350-
return JsonVariant(value, digits);
356+
DEPRECATED("Decimal places are ignored, use the double value instead")
357+
inline JsonVariant double_with_n_digits(double value, uint8_t) {
358+
return JsonVariant(value);
351359
}
352360
}

src/ArduinoJson/JsonVariantImpl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ inline bool JsonVariant::variantIsInteger() const {
117117
inline bool JsonVariant::variantIsFloat() const {
118118
using namespace Internals;
119119

120-
return _type >= JSON_FLOAT_0_DECIMALS ||
120+
return _type == JSON_FLOAT || _type == JSON_POSITIVE_INTEGER || _type == JSON_NEGATIVE_INTEGER ||
121121
(_type == JSON_UNPARSED && Polyfills::isFloat(_content.asString));
122122
}
123123

src/ArduinoJson/Polyfills/isFloat.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,23 @@ inline bool isFloat(const char* s) {
1919
if (!strcmp(s, "NaN")) return true;
2020
if (issign(*s)) s++;
2121
if (!strcmp(s, "Infinity")) return true;
22+
if (*s == '\0') return false;
2223

2324
while (isdigit(*s)) s++;
2425

25-
bool has_dot = *s == '.';
26-
if (has_dot) {
26+
if (*s == '.') {
2727
s++;
2828
while (isdigit(*s)) s++;
2929
}
3030

31-
bool has_exponent = *s == 'e' || *s == 'E';
32-
if (has_exponent) {
31+
if (*s == 'e' || *s == 'E') {
3332
s++;
3433
if (issign(*s)) s++;
3534
if (!isdigit(*s)) return false;
3635
while (isdigit(*s)) s++;
3736
}
3837

39-
return (has_dot || has_exponent) && *s == '\0';
38+
return *s == '\0';
4039
}
4140
}
4241
}

0 commit comments

Comments
 (0)
0