Skip to content

Commit 4a0b0a6

Browse files
committed
2 parents 734bcfc + 0ebb018 commit 4a0b0a6

2 files changed

Lines changed: 41 additions & 41 deletions

File tree

src/CloudStorage.h

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ class BaseCloudStorage {
8686
http::Response response = request.execute();
8787
if(response.statusCode != 200) return false;
8888

89-
StaticJsonBuffer<300> jsonBuffer;
90-
JsonObject& root = jsonBuffer.parseObject(response.body);
89+
DynamicJsonDocument root(1024);
90+
DeserializationError error = deserializeJson(root, response.body);
9191

92-
return !root["error"]; // return isOk
92+
// return isOk
93+
if(error) return false;
94+
return !root["error"];
9395
}
9496

9597
// Method for retrieving key/value pair
@@ -111,11 +113,13 @@ class BaseCloudStorage {
111113
if(response.statusCode != 200) return false;
112114

113115
// Parse response body and extract the wanted value
114-
StaticJsonBuffer<300> jsonBuffer;
115-
JsonObject& root = jsonBuffer.parseObject(response.body);
116+
DynamicJsonDocument root(1024);
117+
DeserializationError error = deserializeJson(root, response.body);
118+
119+
// TODO: handle error
116120
return cloud_storage_utils::ResultWrapper<Ty>(
117121
!root["error"],
118-
getValueByKey<Ty>(root["result"], key)
122+
getValueByKey<Ty>(root["result"].as<JsonObject>(), key)
119123
);
120124
}
121125

@@ -137,10 +141,10 @@ class BaseCloudStorage {
137141
http::Response response = request.execute();
138142
if(response.statusCode != 200) return false;
139143

140-
StaticJsonBuffer<300> jsonBuffer;
141-
JsonObject& root = jsonBuffer.parseObject(response.body);
144+
DynamicJsonDocument root(1024);
145+
DeserializationError error = deserializeJson(root, response.body);
142146

143-
return !root["error"]; // return isOk
147+
return !error && !root["error"]; // return isOk
144148
}
145149

146150
// Method for popping values from arrays in the server
@@ -161,10 +165,10 @@ class BaseCloudStorage {
161165
http::Response response = request.execute();
162166
if(response.statusCode != 200) return false;
163167

164-
StaticJsonBuffer<300> jsonBuffer;
165-
JsonObject& root = jsonBuffer.parseObject(response.body);
168+
DynamicJsonDocument root(1024);
169+
DeserializationError error = deserializeJson(root, response.body);
166170

167-
if(root["error"]) {
171+
if(error || root["error"]) {
168172
return false;
169173
}
170174

@@ -266,12 +270,12 @@ class BaseCloudStorage {
266270
//String data(msg.data().c_str());
267271
//Serial.println("Got Msg: " + data);
268272

269-
// parse response json
270-
StaticJsonBuffer<500> jsonBuffer;
271-
JsonObject& root = jsonBuffer.parseObject(msg.data());
273+
// parse response json
274+
DynamicJsonDocument root(1024);
275+
DeserializationError error = deserializeJson(root, msg.data());
272276

273277
String type = root["type"];
274-
bool isError = root["error"];
278+
bool isError = error || root["error"];
275279

276280
// If the message is about the login, proccess it and update state
277281
if(type == "login") {
@@ -314,73 +318,68 @@ class BaseCloudStorage {
314318
template <class Type>
315319
String buildStoreObjectRequestJson(String key, Type value) {
316320
// Compose request json object
317-
StaticJsonBuffer<200> jsonBuffer;
318-
JsonObject& root = jsonBuffer.createObject();
321+
DynamicJsonDocument root(1024);
319322
root["username"] = _username;
320323
root["password"] = _password;
321324
root["key"] = key;
322325
root["value"] = value;
323326

324327
// Return string form of the object
325328
String jsonString;
326-
root.printTo(jsonString);
329+
serializeJson(root, jsonString);
327330
return jsonString;
328331
}
329332

330333
// Utility method for constructing *Get* request json string
331334
String buildGetObjectRequestJson(String key) {
332335
// Compose request json object
333-
StaticJsonBuffer<200> jsonBuffer;
334-
JsonObject& root = jsonBuffer.createObject();
336+
DynamicJsonDocument root(1024);
335337
root["username"] = _username;
336338
root["password"] = _password;
337339
root["key"] = key;
338340

339341
// Return string form of the object
340342
String jsonString;
341-
root.printTo(jsonString);
343+
serializeJson(root, jsonString);
342344
return jsonString;
343345
}
344346

345347
// Utility method for constructing *Add* request json string
346348
template <class Ty>
347349
String buildAddObjectRequestJson(String collectionKey, Ty object) {
348350
// Compose request json object
349-
StaticJsonBuffer<200> jsonBuffer;
350-
JsonObject& root = jsonBuffer.createObject();
351+
DynamicJsonDocument root(1024);
351352
root["username"] = _username;
352353
root["password"] = _password;
353354
root["collection_key"] = collectionKey;
354355
root["value"] = object;
355356

356357
// Return string form of the object
357358
String jsonString;
358-
root.printTo(jsonString);
359+
serializeJson(root, jsonString);
359360
return jsonString;
360361
}
361362

362363
// Utility method for constructing *Pop* request json string
363364
String buildPopRequestJson(String collectionKey, PopFrom popFrom) {
364365
// Compose request json object
365-
StaticJsonBuffer<200> jsonBuffer;
366-
JsonObject& root = jsonBuffer.createObject();
366+
DynamicJsonDocument root(1024);
367367
root["username"] = _username;
368368
root["password"] = _password;
369369
root["collection_key"] = collectionKey;
370370
root["position"] = (popFrom == PopFrom_Start? "first": "last");
371371

372372
// Return string form of the object
373373
String jsonString;
374-
root.printTo(jsonString);
374+
serializeJson(root, jsonString);
375375
return jsonString;
376376
}
377377

378378
// Utility method for constructing generic *Atomic* request json string
379379
template <class Ty>
380380
String buildAtomicRequestJson(String key, String action, Ty value) {
381381
// Compose request json object
382-
StaticJsonBuffer<200> jsonBuffer;
383-
JsonObject& root = jsonBuffer.createObject();
382+
DynamicJsonDocument root(1024);
384383
root["username"] = _username;
385384
root["password"] = _password;
386385
root["key"] = key;
@@ -389,7 +388,7 @@ class BaseCloudStorage {
389388

390389
// Return string form of the object
391390
String jsonString;
392-
root.printTo(jsonString);
391+
serializeJson(root, jsonString);
393392
return jsonString;
394393
}
395394

@@ -428,27 +427,27 @@ class BaseCloudStorage {
428427
if(response.statusCode != 200) return false;
429428

430429
// Parse response body and extract the wanted value
431-
StaticJsonBuffer<300> jsonBuffer;
432-
JsonObject& root = jsonBuffer.parseObject(response.body);
430+
DynamicJsonDocument root(1024);
431+
DeserializationError error = deserializeJson(root, response.body);
432+
// TODO: handle error
433433
return cloud_storage_utils::ResultWrapper<Ty>(
434434
!root["error"],
435-
getValueByKey<Ty>(root["result"], key)
435+
getValueByKey<Ty>(root["result"].as<JsonObject>(), key)
436436
);
437437
}
438438

439439
// Utility method for constructing generic *Aggregate* request json string
440440
String buildAggregationRequestJson(String collectionKey, String action) {
441441
// Compose request json object
442-
StaticJsonBuffer<200> jsonBuffer;
443-
JsonObject& root = jsonBuffer.createObject();
442+
DynamicJsonDocument root(1024);
444443
root["username"] = _username;
445444
root["password"] = _password;
446445
root["collection_key"] = collectionKey;
447446
root["action"] = action;
448447

449448
// Return string form of the object
450449
String jsonString;
451-
root.printTo(jsonString);
450+
serializeJson(root, jsonString);
452451
return jsonString;
453452
}
454453

@@ -472,8 +471,9 @@ class BaseCloudStorage {
472471
if(response.statusCode != 200) return false;
473472

474473
// Parse response body and extract the wanted value
475-
StaticJsonBuffer<300> jsonBuffer;
476-
JsonObject& root = jsonBuffer.parseObject(response.body);
474+
DynamicJsonDocument root(1024);
475+
DeserializationError error = deserializeJson(root, response.body);
476+
// TODO: handle errors
477477
return cloud_storage_utils::ResultWrapper<Ty>(
478478
!root["error"],
479479
root["result"]

src/Utils/AnyValue.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class AnyValue {
88

99
template <class Ty>
1010
Ty as() {
11-
StaticJsonBuffer<500> jsonBuffer;
12-
JsonObject& root = jsonBuffer.parseObject(_jsonData);
11+
DynamicJsonDocument root;
12+
deserializeJson(root, _jsonData);
1313

1414
Ty result = root["result"]["value"];
1515
return result;

0 commit comments

Comments
 (0)