1 | // Copyright Benoit Blanchon 2014-2016
|
---|
2 | // MIT License
|
---|
3 | //
|
---|
4 | // Arduino JSON library
|
---|
5 | // https://github.com/bblanchon/ArduinoJson
|
---|
6 | // If you like this project, please add a star!
|
---|
7 |
|
---|
8 | #pragma once
|
---|
9 |
|
---|
10 | #include <stddef.h> // for size_t
|
---|
11 | #include <stdint.h> // for uint8_t
|
---|
12 | #include <string.h>
|
---|
13 |
|
---|
14 | #include "Arduino/String.hpp"
|
---|
15 | #include "JsonVariant.hpp"
|
---|
16 |
|
---|
17 | #if defined(__clang__)
|
---|
18 | #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
|
---|
19 | #elif defined(__GNUC__)
|
---|
20 | #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | namespace ArduinoJson {
|
---|
24 | class JsonArray;
|
---|
25 | class JsonObject;
|
---|
26 |
|
---|
27 | // Entry point for using the library.
|
---|
28 | //
|
---|
29 | // Handle the memory management (done in derived classes) and calls the parser.
|
---|
30 | // This abstract class is implemented by StaticJsonBuffer which implements a
|
---|
31 | // fixed memory allocation.
|
---|
32 | class JsonBuffer {
|
---|
33 | public:
|
---|
34 | // CAUTION: NO VIRTUAL DESTRUCTOR!
|
---|
35 | // If we add a virtual constructor the Arduino compiler will add malloc() and
|
---|
36 | // free() to the binary, adding 706 useless bytes.
|
---|
37 | // virtual ~JsonBuffer() {}
|
---|
38 |
|
---|
39 | // Allocates an empty JsonArray.
|
---|
40 | //
|
---|
41 | // Returns a reference to the new JsonArray or JsonArray::invalid() if the
|
---|
42 | // allocation fails.
|
---|
43 | JsonArray &createArray();
|
---|
44 |
|
---|
45 | // Allocates an empty JsonObject.
|
---|
46 | //
|
---|
47 | // Returns a reference to the new JsonObject or JsonObject::invalid() if the
|
---|
48 | // allocation fails.
|
---|
49 | JsonObject &createObject();
|
---|
50 |
|
---|
51 | // Allocates and populate a JsonArray from a JSON string.
|
---|
52 | //
|
---|
53 | // The First argument is a pointer to the JSON string, the memory must be
|
---|
54 | // writable
|
---|
55 | // because the parser will insert null-terminators and replace escaped chars.
|
---|
56 | //
|
---|
57 | // The second argument set the nesting limit (see comment on DEFAULT_LIMIT)
|
---|
58 | //
|
---|
59 | // Returns a reference to the new JsonObject or JsonObject::invalid() if the
|
---|
60 | // allocation fails.
|
---|
61 | JsonArray &parseArray(char *json, uint8_t nestingLimit = DEFAULT_LIMIT);
|
---|
62 |
|
---|
63 | // Same with a const char*.
|
---|
64 | // With this overload, the JsonBuffer will make a copy of the string
|
---|
65 | JsonArray &parseArray(const char *json, uint8_t nesting = DEFAULT_LIMIT) {
|
---|
66 | return parseArray(strdup(json), nesting);
|
---|
67 | }
|
---|
68 |
|
---|
69 | // Same as above with a String class
|
---|
70 | JsonArray &parseArray(const String &json, uint8_t nesting = DEFAULT_LIMIT) {
|
---|
71 | return parseArray(json.c_str(), nesting);
|
---|
72 | }
|
---|
73 |
|
---|
74 | // Allocates and populate a JsonObject from a JSON string.
|
---|
75 | //
|
---|
76 | // The First argument is a pointer to the JSON string, the memory must be
|
---|
77 | // writable
|
---|
78 | // because the parser will insert null-terminators and replace escaped chars.
|
---|
79 | //
|
---|
80 | // The second argument set the nesting limit (see comment on DEFAULT_LIMIT)
|
---|
81 | //
|
---|
82 | // Returns a reference to the new JsonObject or JsonObject::invalid() if the
|
---|
83 | // allocation fails.
|
---|
84 | JsonObject &parseObject(char *json, uint8_t nestingLimit = DEFAULT_LIMIT);
|
---|
85 |
|
---|
86 | // Same with a const char*.
|
---|
87 | // With this overload, the JsonBuffer will make a copy of the string
|
---|
88 | JsonObject &parseObject(const char *json, uint8_t nesting = DEFAULT_LIMIT) {
|
---|
89 | return parseObject(strdup(json), nesting);
|
---|
90 | }
|
---|
91 |
|
---|
92 | // Same as above with a String class
|
---|
93 | JsonObject &parseObject(const String &json, uint8_t nesting = DEFAULT_LIMIT) {
|
---|
94 | return parseObject(json.c_str(), nesting);
|
---|
95 | }
|
---|
96 |
|
---|
97 | // Duplicate a string
|
---|
98 | char *strdup(const char *src) {
|
---|
99 | return src ? strdup(src, strlen(src)) : NULL;
|
---|
100 | }
|
---|
101 | char *strdup(const String &src) { return strdup(src.c_str(), src.length()); }
|
---|
102 |
|
---|
103 | // Allocates n bytes in the JsonBuffer.
|
---|
104 | // Return a pointer to the allocated memory or NULL if allocation fails.
|
---|
105 | virtual void *alloc(size_t size) = 0;
|
---|
106 |
|
---|
107 | protected:
|
---|
108 | // Preserve aligment if nessary
|
---|
109 | static FORCE_INLINE size_t round_size_up(size_t bytes) {
|
---|
110 | #if ARDUINOJSON_ENABLE_ALIGNMENT
|
---|
111 | const size_t x = sizeof(void *) - 1;
|
---|
112 | return (bytes + x) & ~x;
|
---|
113 | #else
|
---|
114 | return bytes;
|
---|
115 | #endif
|
---|
116 | }
|
---|
117 |
|
---|
118 | private:
|
---|
119 | char *strdup(const char *, size_t);
|
---|
120 |
|
---|
121 | // Default value of nesting limit of parseArray() and parseObject().
|
---|
122 | //
|
---|
123 | // The nesting limit is a contain on the level of nesting allowed in the
|
---|
124 | // JSON
|
---|
125 | // string.
|
---|
126 | // If set to 0, only a flat array or objects can be parsed.
|
---|
127 | // If set to 1, the object can contain nested arrays or objects but only 1
|
---|
128 | // level deep.
|
---|
129 | // And bigger values will allow more level of nesting.
|
---|
130 | //
|
---|
131 | // The purpose of this feature is to prevent stack overflow that could
|
---|
132 | // lead to
|
---|
133 | // a security risk.
|
---|
134 | static const uint8_t DEFAULT_LIMIT = 10;
|
---|
135 | };
|
---|
136 | }
|
---|