8000 Store parsed token as string and allow conversion between various typ… · phaethom/ArduinoJson@ef2641b · GitHub
[go: up one dir, main page]

Skip to content

Commit ef2641b

Browse files
committed
Store parsed token as string and allow conversion between various types (issues bblanchon#64, bblanchon#69, bblanchon#90, bblanchon#93)
1 parent bce1015 commit ef2641b

29 files changed

+685
-273
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ v5.0 (currently in beta)
99
* Implicitly call `strdup()` for `String` but not for `char*` (issues #84, #87)
1010
* Added support of non standard JSON input (issue #44)
1111
* Added support of comments in JSON input (issue #88)
12+
* Added implicit cast between numerical types (issues #64, #69, #93)
13+
* Added ability to read number values as string (issue #90)
1214
* Redesigned `JsonVariant` to leverage converting constructors instead of assignment operators (issue #66)
1315
* Switched to new the library layout (requires Arduino 1.0.6 or above)
1416

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ It has been written with Arduino in mind, but it isn't linked to Arduino librari
1212
Features
1313
--------
1414

15-
* JSON decoding
15+
* JSON decoding (comments are supported)
1616
* JSON encoding (with optional indentation)
1717
* Elegant API, very easy to use
1818
* Efficient (no malloc, nor copy)
@@ -80,4 +80,4 @@ From GitHub user `zacsketches`:
8080
8181
---
8282

83-
Found this library useful? [Help me back with a donation!](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=donate%40benoitblanchon%2efr&lc=GB&item_name=Benoit%20Blanchon&item_number=Arduino%20JSON&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted) :smile:
83+
Found this library useful? [Help me back with a donation!](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=donate%40benoitblanchon%2efr&lc=GB&item_name=Benoit%20Blanchon&item_number=Arduino%20JSON&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted) :smile:

include/ArduinoJson/Arduino/Print.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <stddef.h>
1212
#include <stdint.h>
1313

14-
// This class reproduces Arduino's Print
14+
// This class reproduces Arduino's Print class
1515
class Print {
1616
public:
1717
virtual ~Print() {}

include/ArduinoJson/Arduino/String.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,20 @@
1010

1111
#include <string>
1212

13-
typedef std::string String;
13+
// This class reproduces Arduino's String class
14+
class String : public std::string {
15+
public:
16+
String(const char *cstr = "") : std::string(cstr) {}
17+
String(const String &str) : std::string(str) {}
18+
explicit String(char c);
19+
explicit String(unsigned char);
20+
explicit String(int);
21+
explicit String(unsigned int);
22+
explicit String(long);
23+
explicit String(unsigned long);
24+
explicit String(float, unsigned char decimalPlaces = 2);
25+
explicit String(double, unsigned char decimalPlaces = 2);
26+
};
1427

1528
#else
1629

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Benoit Blanchon 2014-2015
2+
// MIT License
3+
//
4+
// Arduino JSON library
5+
// https://github.com/bblanchon/ArduinoJson
6+
7+
#pragma once
8+
9+
#include "../Arduino/Print.hpp"
10+
#include "../Arduino/String.hpp"
11+
12+
namespace ArduinoJson {
13+
namespace Internals {
14+
15+
// A Print implementation that allows to write in a String
16+
class DynamicStringBuilder : public Print {
17+
public:
18+
DynamicStringBuilder(String &str) : _str(str) {}
19+
20+
virtual size_t write(uint8_t c) {
21+
_str += c;
22+
return 1;
23+
}
24+
25+
private:
26+
String &_str;
27+
};
28+
}
29+
}

include/ArduinoJson/Internals/JsonParser.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,12 @@ class JsonParser {
2828

2929
private:
3030
bool skip(char charToSkip);
31-
bool skip(const char *wordToSkip);
3231

3332
const char *parseString();
3433
bool parseAnythingTo(JsonVariant *destination);
3534
FORCE_INLINE bool parseAnythingToUnsafe(JsonVariant *destination);
3635

3736
inline bool parseArrayTo(JsonVariant *destination);
38-
inline bool parseBooleanTo(JsonVariant *destination);
39-
inline bool parseNullTo(JsonVariant *destination);
40-
inline bool parseNumberTo(JsonVariant *destination);
4137
inline bool parseObjectTo(JsonVariant *destination);
4238
inline bool parseStringTo(JsonVariant *destination);
4339

include/ArduinoJson/Internals/JsonPrintable.hpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#include "IndentedPrint.hpp"
1111
#include "JsonWriter.hpp"
1212
#include "Prettyfier.hpp"
13-
#include "StringBuilder.hpp"
13+
#include "StaticStringBuilder.hpp"
14+
#include "DynamicStringBuilder.hpp"
1415

1516
#ifdef ARDUINOJSON_ENABLE_STD_STREAM
1617
#include "StreamPrintAdapter.hpp"
@@ -33,15 +34,20 @@ class JsonPrintable {
3334
}
3435

3536
#ifdef ARDUINOJSON_ENABLE_STD_STREAM
36-
std::ostream& printTo(std::ostream &os) const {
37+
std::ostream &printTo(std::ostream &os) const {
3738
StreamPrintAdapter adapter(os);
3839
printTo(adapter);
3940
return os;
4041
}
41-
#endif
42+
#endif
4243

4344
size_t printTo(char *buffer, size_t bufferSize) const {
44-
StringBuilder sb(buffer, bufferSize);
45+
StaticStringBuilder sb(buffer, bufferSize);
46+
return printTo(sb);
47+
}
48+
49+
size_t printTo(String &str) const {
50+
DynamicStringBuilder sb(str);
4551
return printTo(sb);
4652
}
4753

@@ -51,7 +57,7 @@ class JsonPrintable {
5157
}
5258

5359
size_t prettyPrintTo(char *buffer, size_t bufferSize) const {
54-
StringBuilder sb(buffer, bufferSize);
60+
StaticStringBuilder sb(buffer, bufferSize);
5561
return prettyPrintTo(sb);
5662
}
5763

@@ -60,6 +66,11 @@ class JsonPrintable {
6066
return prettyPrintTo(indentedPrint);
6167
}
6268

69+
size_t prettyPrintTo(String &str) const {
70+
DynamicStringBuilder sb(str);
71+
return prettyPrintTo(sb);
72+
}
73+
6374
size_t measureLength() const {
6475
DummyPrint dp;
6576
return printTo(dp);
@@ -75,11 +86,10 @@ class JsonPrintable {
7586
};
7687

7788
#ifdef ARDUINOJSON_ENABLE_STD_STREAM
78-
template<typename T>
79-
inline std::ostream& operator<<(std::ostream& os, const JsonPrintable<T>& v) {
89+
template <typename T>
90+
inline std::ostream &operator<<(std::ostream &os, const JsonPrintable<T> &v) {
8091
return v.printTo(os);
8192
}
8293
#endif
83-
8494
}
8595
}

include/ArduinoJson/Internals/JsonVariantContent.hpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,11 @@ namespace Internals {
1717
// A union that defines the actual content of a JsonVariant.
1818
// The enum JsonVariantType determines which member is in use.
1919
union JsonVariantContent {
20-
bool asBoolean;
2120
double asDouble; // asDouble is also used for float
22-
long asLong; // asLong is also used for char, short and int
21+
long asLong; // asLong is also used for bool, char, short and int
2322
const char* asString; // asString can be null
2423
JsonArray* asArray; // asArray cannot be null
2524
JsonObject* asObject; // asObject cannot be null
26-
27-
template <typename T>
28-
T as() const;
2925
};
3026
}
3127
}
32-
33-
#include "JsonVariantContent.ipp"

include/ArduinoJson/Internals/JsonVariantContent.ipp

Lines changed: 0 additions & 96 deletions
This file was deleted.

include/ArduinoJson/Internals/JsonVariantType.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ namespace Internals {
1616
// The value determines which member of JsonVariantContent is used.
1717
enum JsonVariantType {
1818
JSON_UNDEFINED, // the JsonVariant has not been initialized
19-
JSON_ARRAY, // the JsonVariant stores a pointer to a JsonArray
20-
JSON_OBJECT, // the JsonVariant stores a pointer to a JsonObject
21-
JSON_BOOLEAN, // the JsonVariant stores a bool
19+
JSON_UNPARSED, // the JsonVariant contains an unparsed string
2220
JSON_STRING, // the JsonVariant stores a const char*
21+
JSON_BOOLEAN, // the JsonVariant stores a bool
2322
JSON_LONG, // the JsonVariant stores a long
23+
JSON_ARRAY, // the JsonVariant stores a pointer to a JsonArray
24+
JSON_OBJECT, // the JsonVariant stores a pointer to a JsonObject
2425

2526
// The following values are reserved for double values
2627
// Multiple values are used for double, depending on the number of decimal

0 commit comments

Comments
 (0)
0