8000 Merged MsgPackError and JsonError into DeserializationError. · java64/ArduinoJson@4592f23 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4592f23

Browse files
committed
Merged MsgPackError and JsonError into DeserializationError.
Return NotSupported if the JSON input contains "\u".
1 parent ccb5413 commit 4592f23

38 files changed

+573
-635
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ HEAD
3737
>
3838
> ```c++
3939
> DynamicJsonDocument doc;
40-
> JsonError error = deserializeJson(doc, json);
40+
> DeserializationError error = deserializeJson(doc, json);
4141
> if (error) {
4242
>
4343
> }

examples/JsonConfigFile/JsonConfigFile.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void loadConfiguration(const char *filename, Config &config) {
3535
StaticJsonDocument<512> doc;
3636

3737
// Deserialize the JSON document
38-
JsonError error = deserializeJson(doc, file);
38+
DeserializationError error = deserializeJson(doc, file);
3939
if (error)
4040
Serial.println(F("Failed to read file, using default configuration"));
4141

examples/JsonHttpClient/JsonHttpClient.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void setup() {
7676
DynamicJsonDocument doc(capacity);
7777

7878
// Parse JSON object
79-
JsonError error = deserializeJson(doc, client);
79+
DeserializationError error = deserializeJson(doc, client);
8080
if (error) {
8181
Serial.print(F("deserializeJson() failed: "));
8282
Serial.println(error.c_str());

examples/JsonParserExample/JsonParserExample.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void setup() {
3232
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
3333

3434
// Deserialize the JSON document
35-
JsonError error = deserializeJson(doc, json);
35+
DeserializationError error = deserializeJson(doc, json);
3636

3737
// Test if parsing succeeds.
3838
if (error) {

examples/MsgPackParser/MsgPackParser.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void setup() {
4545
// It's a reference to the JsonObject, the actual bytes are inside the
4646
// JsonBuffer with all the other nodes of the object tree.
4747
// Memory is freed when jsonBuffer goes out of scope.
48-
MsgPackError error = deserializeMsgPack(doc, input);
48+
DeserializationError error = deserializeMsgPack(doc, input);
4949

5050
// Test if parsing succeeds.
5151
if (error) {

fuzzing/fuzzer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class memstream : public std::istream {
1818
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
1919
DynamicJsonDocument doc;
2020
memstream json(data, size);
21-
JsonError error = deserializeJson(doc, json);
22-
if (error == JsonError::Ok) {
21+
DeserializationError error = deserializeJson(doc, json);
22+
if (error == DeserializationError::Ok) {
2323
JsonVariant variant = doc.as<JsonVariant>();
2424
variant.as<std::string>(); // <- serialize to JSON
2525
}

src/ArduinoJson/JsonError.hpp renamed to src/ArduinoJson/DeserializationError.hpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,33 @@
1010

1111
namespace ArduinoJson {
1212

13-
class JsonError {
13+
class DeserializationError {
1414
public:
15-
enum Code { Ok, TooDeep, NoMemory, InvalidInput, IncompleteInput };
15+
enum Code {
16+
Ok,
17+
IncompleteInput,
18+
InvalidInput,
19+
NoMemory,
20+
NotSupported,
21+
TooDeep
22+
};
1623

17-
JsonError(Code code) : _code(code) {}
24+
DeserializationError() {}
25+
DeserializationError(Code code) : _code(code) {}
1826

19-
friend bool operator==(const JsonError& err, Code code) {
27+
friend bool operator==(const DeserializationError& err, Code code) {
2028
return err._code == code;
2129
}
2230

23-
friend bool operator==(Code code, const JsonError& err) {
31+
friend bool operator==(Code code, const DeserializationError& err) {
2432
return err._code == code;
2533
}
2634

27-
friend bool operator!=(const JsonError& err, Code code) {
35+
friend bool operator!=(const DeserializationError& err, Code code) {
2836
return err._code != code;
2937
}
3038

31-
friend bool operator!=(Code code, const JsonError& err) {
39+
friend bool operator!=(Code code, const DeserializationError& err) {
3240
return err._code != code;
3341
}
3442

@@ -48,6 +56,8 @@ class JsonError {
4856
return "InvalidInput";
4957
case IncompleteInput:
5058
return "IncompleteInput";
59+
case NotSupported:
60+
return "NotSupported";
5161
default:
5262
return "???";
5363
}
@@ -58,13 +68,14 @@ class JsonError {
5868
};
5969

6070
#if ARDUINOJSON_ENABLE_STD_STREAM
61-
inline std::ostream& operator<<(std::ostream& s, const JsonError& e) {
71+
inline std::ostream& operator<<(std::ostream& s,
72+
const DeserializationError& e) {
6273
s << e.c_str();
6374
return s;
6475
}
6576

66-
inline std::ostream& operator<<(std::ostream& s, JsonError::Code c) {
67-
s << JsonError(c).c_str();
77+
inline std::ostream& operator<<(std::ostream& s, DeserializationError::Code c) {
78+
s << DeserializationError(c).c_str();
6879
return s;
6980
}
7081
#endif

src/ArduinoJson/Json/Deserialization/JsonDeserializer.hpp

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#pragma once
66

7-
#include "../../JsonError.hpp"
7+
#include "../../DeserializationError.hpp"
88
#include "../../JsonVariant.hpp"
99
#include "../../Memory/JsonBuffer.hpp"
1010
#include "../../Reading/Reader.hpp"
@@ -24,8 +24,8 @@ class JsonDeserializer {
2424
_writer(writer),
2525
_nestingLimit(nestingLimit),
2626
_loaded(false) {}
27-
JsonError parse(JsonVariant &variant) {
28-
JsonError err = skipSpacesAndComments();
27+
DeserializationError parse(JsonVariant &variant) {
28+
DeserializationError err = skipSpacesAndComments();
2929
if (err) return err;
3030

3131
switch (current()) {
@@ -64,22 +64,22 @@ class JsonDeserializer {
6464
return true;
6565
}
6666

67-
JsonError parseArray(JsonVariant &variant) {
68-
if (_nestingLimit == 0) return JsonError::TooDeep;
67+
DeserializationError parseArray(JsonVariant &variant) {
68+
if (_nestingLimit == 0) return DeserializationError::TooDeep;
6969

7070
JsonArray *array = new (_buffer) JsonArray(_buffer);
71-
if (!array) return JsonError::NoMemory;
71+
if (!array) return DeserializationError::NoMemory;
7272
variant = array;
7373

7474
// Check opening braket
75-
if (!eat('[')) return JsonError::InvalidInput;
75+
if (!eat('[')) return DeserializationError::InvalidInput;
7676

7777
// Skip spaces
78-
JsonError err = skipSpacesAndComments();
78+
DeserializationError err = skipSpacesAndComments();
7979
if (err) return err;
8080

8181
// Empty array?
82-
if (eat(']')) return JsonError::Ok;
82+
if (eat(']')) return DeserializationError::Ok;
8383

8484
// Read each value
8585
for (;;) {
@@ -89,34 +89,34 @@ class JsonDeserializer {
8989
err = parse(value);
9090
_nestingLimit++;
9191
if (err) return err;
92-
if (!array->add(value)) return JsonError::NoMemory;
92+
if (!array->add(value)) return DeserializationError::NoMemory;
9393

9494
// 2 - Skip spaces
9595
err = skipSpacesAndComments();
9696
if (err) return err;
9797

9898
// 3 - More values?
99-
if (eat(']')) return JsonError::Ok;
100-
if (!eat(',')) return JsonError::InvalidInput;
99+
if (eat(']')) return DeserializationError::Ok;
100+
if (!eat(',')) return DeserializationError::InvalidInput;
101101
}
102102
}
103103

104-
JsonError parseObject(JsonVariant &variant) {
105-
if (_nestingLimit == 0) return JsonError::TooDeep;
104+
DeserializationError parseObject(JsonVariant &variant) {
105+
if (_nestingLimit == 0) return DeserializationError::TooDeep;
106106

107107
JsonObject *object = new (_buffer) JsonObject(_buffer);
108-
if (!object) return JsonError::NoMemory;
108+
if (!object) return DeserializationError::NoMemory;
109109
variant = object;
110110

111111
// Check opening brace
112-
if (!eat('{')) return JsonError::InvalidInput;
112+
if (!eat('{')) return DeserializationError::InvalidInput;
113113

114114
// Skip spaces
115-
JsonError err = skipSpacesAndComments();
115+
DeserializationError err = skipSpacesAndComments();
116116
if (err) return err;
117117

118118
// Empty object?
119-
if (eat('}')) return JsonError::Ok;
119+
if (eat('}')) return DeserializationError::Ok;
120120

121121
// Read each key value pair
122122
for (;;) {
@@ -130,48 +130,48 @@ class JsonDeserializer {
130130
if (err) return err;
131131

132132
// Colon
133-
if (!eat(':')) return JsonError::InvalidInput;
133+
if (!eat(':')) return DeserializationError::InvalidInput;
134134

135135
// Parse value
136136
JsonVariant value;
137137
_nestingLimit--;
138138
err = parse(value);
139139
_nestingLimit++;
140140
if (err) return err;
141-
if (!object->set(key, value)) return JsonError::NoMemory;
141+
if (!object->set(key, value)) return DeserializationError::NoMemory;
142142

143143
// Skip spaces
144144
err = skipSpacesAndComments();
145145
if (err) return err;
146146

147147
// More keys/values?
148-
if (eat('}')) return JsonError::Ok;
149-
if (!eat(',')) return JsonError::InvalidInput;
148+
if (eat('}')) return DeserializationError::Ok;
149+
if (!eat(',')) return DeserializationError::InvalidInput;
150150

151151
// Skip spaces
152152
err = skipSpacesAndComments();
153153
if (err) return err;
154154
}
155155
}
156156

157-
JsonError parseValue(JsonVariant &variant) {
157+
DeserializationError parseValue(JsonVariant &variant) {
158158
bool hasQuotes = isQuote(current());
159159
const char *value;
160-
JsonError error = parseString(&value);
160+
DeserializationError error = parseString(&value);
161161
if (error) return error;
162162
if (hasQuotes) {
163163
variant = value;
164164
} else {
165165
variant = RawJson(value);
166166
}
167-
return JsonError::Ok;
167+
return DeserializationError::Ok;
168168
}
169169

170-
JsonError parseString(const char **result) {
170+
DeserializationError parseString(const char **result) {
171171
typename RemoveReference<TWriter>::type::String str = _writer.startString();
172172

173173
char c = current();
174-
if (c == '\0') return JsonError::IncompleteInput;
174+
if (c == '\0') return DeserializationError::IncompleteInput;
175175

176176
if (isQuote(c)) { // quotes
177177
move();
@@ -181,14 +181,15 @@ class JsonDeserializer {
181181
move();
182182
if (c == stopChar) break;
183183

184-
if (c == '\0') return JsonError::IncompleteInput;
184+
if (c == '\0') return DeserializationError::IncompleteInput;
185185

186186
if (c == '\\') {
187187
c = current();
188-
if (c == 0) return JsonError::IncompleteInput;
188+
if (c == '\0') return DeserializationError::IncompleteInput;
189+
if (c == 'u') return DeserializationError::NotSupported;
189190
// replace char
190191
c = Encoding::unescapeChar(c);
191-
if (c == '\0') return JsonError::InvalidInput;
192+
if (c == '\0') return DeserializationError::InvalidInput;
192193
move();
193194
}
194195

@@ -201,12 +202,12 @@ class JsonDeserializer {
201202
c = current();
202203
} while (canBeInNonQuotedString(c));
203204
} else {
204-
return JsonError::InvalidInput;
205+
return DeserializationError::InvalidInput;
205206
}
206207

207208
*result = str.c_str();
208-
if (*result == NULL) return JsonError::NoMemory;
209-
return JsonError::Ok;
209+
if (*result == NULL) return DeserializationError::NoMemory;
210+
return DeserializationError::Ok;
210211
}
211212

212213
static inline bool isBetween(char c, char min, char max) {
@@ -222,12 +223,12 @@ class JsonDeserializer {
222223
return c == '\'' || c == '\"';
223224
}
224225

225-
JsonError skipSpacesAndComments() {
226+
DeserializationError skipSpacesAndComments() {
226227
for (;;) {
227228
switch (current()) {
228229
// end of string
229230
case '\0':
230-
return JsonError::IncompleteInput;
231+
return DeserializationError::IncompleteInput;
231232

232233
// spaces
233234
case ' ':
@@ -247,7 +248,7 @@ class JsonDeserializer {
247248
bool wasStar = false;
248249
for (;;) {
249250
char c = current();
250-
if (c == '\0') return JsonError::IncompleteInput;
251+
if (c == '\0') return DeserializationError::IncompleteInput;
251252
if (c == '/' && wasStar) {
252253
move();
253254
break;
@@ -264,19 +265,19 @@ class JsonDeserializer {
264265
for (;;) {
265266
move();
266267
char c = current();
267-
if (c == '\0') return JsonError::IncompleteInput;
268+
if (c == '\0') return DeserializationError::IncompleteInput;
268269
if (c == '\n') break;
269270
}
270271
break;
271272

272273
// not a comment, just a '/'
273274
default:
274-
return JsonError::InvalidInput;
275+
return DeserializationError::InvalidInput;
275276
}
276277
break;
277278

278279
default:
279-
return JsonError::Ok;
280+
return DeserializationError::Ok;
280281
}
281282
}
282283
}

0 commit comments

Comments
 (0)
0