mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-14 19:16:35 +02:00
Return JsonArray
and JsonObject
by value (closes #309)
This commit is contained in:
30
CHANGELOG.md
30
CHANGELOG.md
@ -1,6 +1,36 @@
|
||||
ArduinoJson: change log
|
||||
=======================
|
||||
|
||||
HEAD
|
||||
----
|
||||
|
||||
* Return `JsonArray` and `JsonObject` by value instead of reference (issue #309)
|
||||
* Replaced `success()` with `isNull()`
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
>
|
||||
> Old code:
|
||||
>
|
||||
> ```c++
|
||||
> JsonObject& obj = doc.to<JsonObject>();
|
||||
> JsonArray& arr = obj.createNestedArray("key");
|
||||
> if (!arr.success()) {
|
||||
> Serial.println("No enough memory");
|
||||
> return;
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> New code:
|
||||
>
|
||||
> ```c++
|
||||
> JsonObject obj = doc.to<JsonObject>();
|
||||
> JsonArray arr = obj.createNestedArray("key");
|
||||
> if (arr.isNull()) {
|
||||
> Serial.println("No enough memory");
|
||||
> return;
|
||||
> }
|
||||
> ```
|
||||
|
||||
v6.0.1-beta
|
||||
-----------
|
||||
|
||||
|
@ -63,7 +63,7 @@ char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302
|
||||
DynamicJsonDocument doc;
|
||||
deserializeJson(doc, json);
|
||||
|
||||
JsonObject& root = doc.as<JsonObject>();
|
||||
JsonObjectRef root = doc.as<JsonObject>();
|
||||
const char* sensor = root["sensor"];
|
||||
long time = root["time"];
|
||||
double latitude = root["data"][0];
|
||||
@ -79,11 +79,11 @@ Here is a program that generates a JSON document with ArduinoJson:
|
||||
```c++
|
||||
DynamicJsonDocument doc;
|
||||
|
||||
JsonObject& root = doc.to<JsonObject>();
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
root["sensor"] = "gps";
|
||||
root["time"] = 1351824120;
|
||||
|
||||
JsonArray& data = root.createNestedArray("data");
|
||||
JsonArray data = root.createNestedArray("data");
|
||||
data.add(48.756080);
|
||||
data.add(2.302038);
|
||||
|
||||
|
@ -40,7 +40,7 @@ void loadConfiguration(const char *filename, Config &config) {
|
||||
Serial.println(F("Failed to read file, using default configuration"));
|
||||
|
||||
// Get the root object in the document
|
||||
JsonObject &root = doc.as<JsonObject>();
|
||||
JsonObject root = doc.as<JsonObject>();
|
||||
|
||||
// Copy values from the JsonObject to the Config
|
||||
config.port = root["port"] | 2731;
|
||||
@ -70,7 +70,7 @@ void saveConfiguration(const char *filename, const Config &config) {
|
||||
StaticJsonDocument<256> doc;
|
||||
|
||||
// Make our document contain an object
|
||||
JsonObject &root = doc.to<JsonObject>();
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
// Set the values in the object
|
||||
root["hostname"] = config.hostname;
|
||||
@ -132,15 +132,4 @@ void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// The website arduinojson.org contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization or deserialization problem.
|
||||
// Please check it out at: https://arduinojson.org/
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a case study of a project that has
|
||||
// a complex configuration with nested members.
|
||||
// Contrary to this example, the project in the book uses the SPIFFS filesystem.
|
||||
// Please check it out at: https://arduinojson.org/book/
|
||||
// Visit https://arduinojson.org/v6/example/config/ for more.
|
||||
|
@ -24,7 +24,7 @@ void setup() {
|
||||
// DynamicJsonDocument doc(200);
|
||||
|
||||
// Make our document be an object
|
||||
JsonObject& root = doc.to<JsonObject>();
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
// Add values in the object
|
||||
//
|
||||
@ -35,7 +35,7 @@ void setup() {
|
||||
|
||||
// Add an array.
|
||||
//
|
||||
JsonArray& data = root.createNestedArray("data");
|
||||
JsonArray data = root.createNestedArray("data");
|
||||
data.add(48.756080);
|
||||
data.add(2.302038);
|
||||
|
||||
@ -61,15 +61,4 @@ void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// The website arduinojson.org contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization problem.
|
||||
// Please check it out at: https://arduinojson.org/
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
|
||||
// It begins with a simple example, like the one above, and then adds more
|
||||
// features like serializing directly to a file or an HTTP request.
|
||||
// Please check it out at: https://arduinojson.org/book/
|
||||
// Visit https://arduinojson.org/v6/example/generator/ for more.
|
||||
|
@ -84,7 +84,7 @@ void setup() {
|
||||
}
|
||||
|
||||
// Extract values
|
||||
JsonObject& root = doc.as<JsonObject>();
|
||||
JsonObject root = doc.as<JsonObject>();
|
||||
Serial.println(F("Response:"));
|
||||
Serial.println(root["sensor"].as<char*>());
|
||||
Serial.println(root["time"].as<char*>());
|
||||
@ -99,16 +99,4 @@ void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// The website arduinojson.org contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization problem.
|
||||
// Please check it out at: https://arduinojson.org/
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on deserialization
|
||||
// showing how to parse the response from Yahoo Weather. In the last chapter,
|
||||
// it shows how to parse the huge documents from OpenWeatherMap
|
||||
// and Weather Underground.
|
||||
// Please check it out at: https://arduinojson.org/book/
|
||||
// Visit https://arduinojson.org/v6/example/http-client/ for more.
|
||||
|
@ -42,7 +42,7 @@ void setup() {
|
||||
}
|
||||
|
||||
// Get the root object in the document
|
||||
JsonObject& root = doc.as<JsonObject>();
|
||||
JsonObject root = doc.as<JsonObject>();
|
||||
|
||||
// Fetch values.
|
||||
//
|
||||
@ -64,15 +64,4 @@ void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// The website arduinojson.org contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// deserialization problem.
|
||||
// Please check it out at: https://arduinojson.org/
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on deserialization.
|
||||
// It begins with a simple example, like the one above, and then adds more
|
||||
// features like deserializing directly from a file or an HTTP request.
|
||||
// Please check it out at: https://arduinojson.org/book/
|
||||
// Visit https://arduinojson.org/v6/example/parser/ for more.
|
||||
|
@ -56,10 +56,10 @@ void loop() {
|
||||
StaticJsonDocument<500> doc;
|
||||
|
||||
// Make our document represent an object
|
||||
JsonObject& root = doc.to<JsonObject>();
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
// Create the "analog" array
|
||||
JsonArray& analogValues = root.createNestedArray("analog");
|
||||
JsonArray analogValues = root.createNestedArray("analog");
|
||||
for (int pin = 0; pin < 6; pin++) {
|
||||
// Read the analog input
|
||||
int value = analogRead(pin);
|
||||
@ -69,7 +69,7 @@ void loop() {
|
||||
}
|
||||
|
||||
// Create the "digital" array
|
||||
JsonArray& digitalValues = root.createNestedArray("digital");
|
||||
JsonArray digitalValues = root.createNestedArray("digital");
|
||||
for (int pin = 0; pin < 14; pin++) {
|
||||
// Read the digital input
|
||||
int value = digitalRead(pin);
|
||||
@ -95,15 +95,4 @@ void loop() {
|
||||
client.stop();
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// The website arduinojson.org contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization problem.
|
||||
// Please check it out at: https://arduinojson.org/
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
|
||||
// It begins with a simple example, then adds more features like serializing
|
||||
// directly to a file or an HTTP client.
|
||||
// Please check it out at: https://arduinojson.org/book/
|
||||
// Visit https://arduinojson.org/v6/example/http-server/ for more.
|
||||
|
@ -48,10 +48,10 @@ void loop() {
|
||||
StaticJsonDocument<500> doc;
|
||||
|
||||
// Make our document represent an object
|
||||
JsonObject& root = doc.to<JsonObject>();
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
// Create the "analog" array
|
||||
JsonArray& analogValues = root.createNestedArray("analog");
|
||||
JsonArray analogValues = root.createNestedArray("analog");
|
||||
for (int pin = 0; pin < 6; pin++) {
|
||||
// Read the analog input
|
||||
int value = analogRead(pin);
|
||||
@ -61,7 +61,7 @@ void loop() {
|
||||
}
|
||||
|
||||
// Create the "digital" array
|
||||
JsonArray& digitalValues = root.createNestedArray("digital");
|
||||
JsonArray digitalValues = root.createNestedArray("digital");
|
||||
for (int pin = 0; pin < 14; pin++) {
|
||||
// Read the digital input
|
||||
int value = digitalRead(pin);
|
||||
@ -87,15 +87,4 @@ void loop() {
|
||||
delay(10000);
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// The website arduinojson.org contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization problem.
|
||||
// Please check it out at: https://arduinojson.org/
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
|
||||
// It begins with a simple example, then adds more features like serializing
|
||||
// directly to a file or any stream.
|
||||
// Please check it out at: https://arduinojson.org/book/
|
||||
// Visit https://arduinojson.org/v6/example/udp-beacon/ for more.
|
||||
|
@ -55,7 +55,7 @@ void setup() {
|
||||
}
|
||||
|
||||
// Get the root object in the document
|
||||
JsonObject& root = doc.as<JsonObject>();
|
||||
JsonObject root = doc.as<JsonObject>();
|
||||
|
||||
// Fetch values.
|
||||
//
|
||||
@ -77,15 +77,4 @@ void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// The website arduinojson.org contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// deserialization problem.
|
||||
// Please check it out at: https://arduinojson.org/
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on deserialization.
|
||||
// It begins with a simple example, like the one above, and then adds more
|
||||
// features like deserializing directly from a file or an HTTP request.
|
||||
// Please check it out at: https://arduinojson.org/book/
|
||||
// Visit https://arduinojson.org/v6/example/msgpack-parser/ for more.
|
||||
|
@ -21,7 +21,7 @@ void setup() {
|
||||
// JsonBuffer.
|
||||
deserializeJson(doc, F("{\"sensor\":\"gps\",\"time\":1351824120,"
|
||||
"\"data\":[48.756080,2.302038]}"));
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
// You can use a Flash String to get an element of a JsonObject
|
||||
// No duplication is done.
|
||||
@ -56,15 +56,4 @@ void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// The website arduinojson.org contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any memory
|
||||
// problem.
|
||||
// Please check it out at: https://arduinojson.org/
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a quick C++ course that explains
|
||||
// how your microcontroller stores strings in memory. It also tells why you
|
||||
// should not abuse Flash strings with ArduinoJson.
|
||||
// Please check it out at: https://arduinojson.org/book/
|
||||
// Visit https://arduinojson.org/v6/example/progmem/ for more.
|
||||
|
@ -18,7 +18,7 @@ void setup() {
|
||||
String input =
|
||||
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
||||
deserializeJson(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
// You can use a String to get an element of a JsonObject
|
||||
// No duplication is done.
|
||||
@ -62,14 +62,4 @@ void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// The website arduinojson.org contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any problem.
|
||||
// Please check it out at: https://arduinojson.org/
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a quick C++ course that explains
|
||||
// how your microcontroller stores strings in memory. On several occasions, it
|
||||
// shows how you can avoid String in your program.
|
||||
// Please check it out at: https://arduinojson.org/book/
|
||||
// Visit https://arduinojson.org/v6/example/string/ for more.
|
||||
|
@ -7,13 +7,20 @@
|
||||
#include "ArduinoJson/version.hpp"
|
||||
|
||||
#include "ArduinoJson/DynamicJsonDocument.hpp"
|
||||
#include "ArduinoJson/StaticJsonDocument.hpp"
|
||||
|
||||
#include "ArduinoJson/JsonObjectImpl.hpp"
|
||||
|
||||
#include "ArduinoJson/JsonArray.hpp"
|
||||
#include "ArduinoJson/JsonObject.hpp"
|
||||
|
||||
#include "ArduinoJson/JsonArrayImpl.hpp"
|
||||
#include "ArduinoJson/JsonArraySubscript.hpp"
|
||||
#include "ArduinoJson/JsonObjectSubscript.hpp"
|
||||
#include "ArduinoJson/JsonVariantImpl.hpp"
|
||||
|
||||
#include "ArduinoJson/Json/JsonDeserializer.hpp"
|
||||
#include "ArduinoJson/Json/JsonSerializer.hpp"
|
||||
#include "ArduinoJson/Json/PrettyJsonSerializer.hpp"
|
||||
#include "ArduinoJson/MsgPack/MsgPackDeserializer.hpp"
|
||||
#include "ArduinoJson/MsgPack/MsgPackSerializer.hpp"
|
||||
#include "ArduinoJson/StaticJsonDocument.hpp"
|
||||
|
||||
#include "ArduinoJson/JsonArrayImpl.hpp"
|
||||
#include "ArduinoJson/JsonObjectImpl.hpp"
|
||||
#include "ArduinoJson/JsonVariantImpl.hpp"
|
||||
|
@ -19,24 +19,5 @@ struct JsonVariantAs<char*> {
|
||||
typedef const char* type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct JsonVariantAs<JsonArray> {
|
||||
typedef JsonArray& type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct JsonVariantAs<const JsonArray> {
|
||||
typedef const JsonArray& type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct JsonVariantAs<JsonObject> {
|
||||
typedef JsonObject& type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct JsonVariantAs<const JsonObject> {
|
||||
typedef const JsonObject& type;
|
||||
};
|
||||
}
|
||||
}
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
||||
|
@ -8,20 +8,19 @@
|
||||
#include "JsonInteger.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
// Forward declarations
|
||||
class JsonArray;
|
||||
class JsonObject;
|
||||
|
||||
namespace Internals {
|
||||
// Forward declarations
|
||||
struct JsonArrayData;
|
||||
struct JsonObjectData;
|
||||
|
||||
// A union that defines the actual content of a JsonVariant.
|
||||
// The enum JsonVariantType determines which member is in use.
|
||||
union JsonVariantContent {
|
||||
JsonFloat asFloat; // used for double and float
|
||||
JsonUInt asInteger; // used for bool, char, short, int and longs
|
||||
const char* asString; // asString can be null
|
||||
JsonArray* asArray; // asArray cannot be null
|
||||
JsonObject* asObject; // asObject cannot be null
|
||||
JsonArrayData* asArray; // asArray cannot be null
|
||||
JsonObjectData* asObject; // asObject cannot be null
|
||||
};
|
||||
}
|
||||
}
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
||||
|
@ -5,8 +5,6 @@
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
class JsonArray;
|
||||
class JsonObject;
|
||||
|
||||
namespace Internals {
|
||||
|
||||
@ -19,9 +17,9 @@ enum JsonVariantType {
|
||||
JSON_BOOLEAN, // JsonVariant stores a bool
|
||||
JSON_POSITIVE_INTEGER, // JsonVariant stores an JsonUInt
|
||||
JSON_NEGATIVE_INTEGER, // JsonVariant stores an JsonUInt that must be negated
|
||||
JSON_ARRAY, // JsonVariant stores a pointer to a JsonArray
|
||||
JSON_OBJECT, // JsonVariant stores a pointer to a JsonObject
|
||||
JSON_ARRAY, // JsonVariant stores a pointer to a JsonArrayData
|
||||
JSON_OBJECT, // JsonVariant stores a pointer to a JsonObjectData
|
||||
JSON_FLOAT // JsonVariant stores a JsonFloat
|
||||
};
|
||||
}
|
||||
}
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
||||
|
@ -13,7 +13,7 @@ namespace Internals {
|
||||
|
||||
// A singly linked list of T.
|
||||
// The linked list is composed of ListNode<T>.
|
||||
// It is derived by JsonArray and JsonObject
|
||||
// It is derived by JsonArrayData and JsonObjectData
|
||||
template <typename T>
|
||||
class List {
|
||||
public:
|
||||
@ -22,23 +22,10 @@ class List {
|
||||
typedef ListIterator<T> iterator;
|
||||
typedef ListConstIterator<T> const_iterator;
|
||||
|
||||
// Creates an empty List<T> attached to a JsonBuffer.
|
||||
// The JsonBuffer allows to allocate new nodes.
|
||||
// When buffer is NULL, the List is not able to grow and success() returns
|
||||
// false. This is used to identify bad memory allocations and parsing
|
||||
// failures.
|
||||
explicit List(JsonBuffer *buf) : _buffer(buf), _firstNode(NULL) {}
|
||||
|
||||
// Returns true if the object is valid
|
||||
// Would return false in the following situation:
|
||||
// - the memory allocation failed (StaticJsonBuffer was too small)
|
||||
// - the JSON parsing failed
|
||||
bool success() const {
|
||||
return _buffer != NULL;
|
||||
}
|
||||
|
||||
// Returns the numbers of elements in the list.
|
||||
// For a JsonObject, it would return the number of key-value pairs
|
||||
// For a JsonObjectData, it would return the number of key-value pairs
|
||||
size_t size() const {
|
||||
size_t nodeCount = 0;
|
||||
for (node_type *node = _firstNode; node; node = node->next) nodeCount++;
|
||||
@ -87,14 +74,13 @@ class List {
|
||||
JsonBuffer &buffer() const {
|
||||
return *_buffer;
|
||||
}
|
||||
JsonBuffer *_buffer; // TODO!!
|
||||
|
||||
protected:
|
||||
void clear() {
|
||||
_firstNode = 0;
|
||||
}
|
||||
|
||||
JsonBuffer *_buffer;
|
||||
|
||||
private:
|
||||
node_type *_firstNode;
|
||||
};
|
||||
|
@ -1,24 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
// A type that is meant to be used by reference only (JsonArray and JsonObject)
|
||||
class ReferenceType {
|
||||
public:
|
||||
bool operator==(const ReferenceType& other) const {
|
||||
// two JsonArray are equal if they are the same instance
|
||||
// (we don't compare the content)
|
||||
return this == &other;
|
||||
}
|
||||
|
||||
bool operator!=(const ReferenceType& other) const {
|
||||
return this != &other;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -32,28 +32,26 @@ class DynamicJsonDocument {
|
||||
return _root.as<T>();
|
||||
}
|
||||
|
||||
// JsonObject& to<JsonObject>()
|
||||
// JsonObject to<JsonObject>()
|
||||
template <typename T>
|
||||
typename Internals::enable_if<Internals::is_same<T, JsonObject>::value,
|
||||
JsonObject&>::type
|
||||
JsonObject>::type
|
||||
to() {
|
||||
clear();
|
||||
JsonObject* object = new (&_buffer) JsonObject(&_buffer);
|
||||
if (!object) return JsonObject::invalid();
|
||||
JsonObject object(&_buffer);
|
||||
_root = object;
|
||||
return *object;
|
||||
return object;
|
||||
}
|
||||
|
||||
// JsonArray& to<JsonArray>()
|
||||
// JsonArray to<JsonArray>()
|
||||
template <typename T>
|
||||
typename Internals::enable_if<Internals::is_same<T, JsonArray>::value,
|
||||
JsonArray&>::type
|
||||
JsonArray>::type
|
||||
to() {
|
||||
clear();
|
||||
JsonArray* array = new (&_buffer) JsonArray(&_buffer);
|
||||
if (!array) return JsonArray::invalid();
|
||||
JsonArray array(&_buffer);
|
||||
_root = array;
|
||||
return *array;
|
||||
return array;
|
||||
}
|
||||
|
||||
// JsonVariant& to<JsonVariant>()
|
||||
|
@ -66,8 +66,8 @@ class JsonDeserializer {
|
||||
DeserializationError parseArray(JsonVariant &variant) {
|
||||
if (_nestingLimit == 0) return DeserializationError::TooDeep;
|
||||
|
||||
JsonArray *array = new (_buffer) JsonArray(_buffer);
|
||||
if (!array) return DeserializationError::NoMemory;
|
||||
JsonArray array(_buffer);
|
||||
if (array.isNull()) return DeserializationError::NoMemory;
|
||||
variant = array;
|
||||
|
||||
// Check opening braket
|
||||
@ -88,7 +88,7 @@ class JsonDeserializer {
|
||||
err = parse(value);
|
||||
_nestingLimit++;
|
||||
if (err) return err;
|
||||
if (!array->add(value)) return DeserializationError::NoMemory;
|
||||
if (!array.add(value)) return DeserializationError::NoMemory;
|
||||
|
||||
// 2 - Skip spaces
|
||||
err = skipSpacesAndComments();
|
||||
@ -103,8 +103,8 @@ class JsonDeserializer {
|
||||
DeserializationError parseObject(JsonVariant &variant) {
|
||||
if (_nestingLimit == 0) return DeserializationError::TooDeep;
|
||||
|
||||
JsonObject *object = new (_buffer) JsonObject(_buffer);
|
||||
if (!object) return DeserializationError::NoMemory;
|
||||
JsonObject object(_buffer);
|
||||
if (object.isNull()) return DeserializationError::NoMemory;
|
||||
variant = object;
|
||||
|
||||
// Check opening brace
|
||||
@ -126,9 +126,7 @@ class JsonDeserializer {
|
||||
|
||||
// Skip spaces
|
||||
err = skipSpacesAndComments();
|
||||
if (err) return err;
|
||||
|
||||
// Colon
|
||||
if (err) return err; // Colon
|
||||
if (!eat(':')) return DeserializationError::InvalidInput;
|
||||
|
||||
// Parse value
|
||||
@ -137,7 +135,7 @@ class JsonDeserializer {
|
||||
err = parse(value);
|
||||
_nestingLimit++;
|
||||
if (err) return err;
|
||||
if (!object->set(key, value)) return DeserializationError::NoMemory;
|
||||
if (!object.set(key, value)) return DeserializationError::NoMemory;
|
||||
|
||||
// Skip spaces
|
||||
err = skipSpacesAndComments();
|
||||
|
@ -75,7 +75,9 @@ class JsonSerializer {
|
||||
_writer.writeBoolean(value);
|
||||
}
|
||||
|
||||
void acceptUndefined() {}
|
||||
void acceptNull() {
|
||||
_writer.writeRaw("null");
|
||||
}
|
||||
|
||||
size_t bytesWritten() const {
|
||||
return _writer.bytesWritten();
|
||||
|
@ -13,13 +13,6 @@
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
// Writes the JSON tokens to a Print implementation
|
||||
// This class is used by:
|
||||
// - JsonArray::writeTo()
|
||||
// - JsonObject::writeTo()
|
||||
// - JsonVariant::writeTo()
|
||||
// Its derived by PrettyJsonWriter that overrides some members to add
|
||||
// indentation.
|
||||
template <typename Print>
|
||||
class JsonWriter {
|
||||
public:
|
||||
|
@ -4,47 +4,33 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Data/List.hpp"
|
||||
#include "Data/ReferenceType.hpp"
|
||||
#include "Data/ValueSaver.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
#include "Memory/JsonBufferAllocated.hpp"
|
||||
#include "Polyfills/type_traits.hpp"
|
||||
#include "Strings/StringTraits.hpp"
|
||||
|
||||
// Returns the size (in bytes) of an array with n elements.
|
||||
// Can be very handy to determine the size of a StaticJsonBuffer.
|
||||
#define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \
|
||||
(sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(JsonArray::node_type))
|
||||
#include "./JsonArrayData.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
// Forward declarations
|
||||
class JsonObject;
|
||||
class JsonBuffer;
|
||||
|
||||
namespace Internals {
|
||||
class JsonArraySubscript;
|
||||
}
|
||||
|
||||
class JsonArray : public Internals::ReferenceType,
|
||||
public Internals::NonCopyable,
|
||||
public Internals::List<JsonVariant>,
|
||||
public Internals::JsonBufferAllocated {
|
||||
class JsonArray {
|
||||
friend class JsonVariant;
|
||||
|
||||
public:
|
||||
explicit JsonArray(Internals::JsonBuffer *buf) throw()
|
||||
: Internals::List<JsonVariant>(buf) {}
|
||||
typedef Internals::JsonArrayData::iterator iterator;
|
||||
typedef Internals::JsonArrayData::const_iterator const_iterator;
|
||||
|
||||
// Gets the value at the specified index
|
||||
const Internals::JsonArraySubscript operator[](size_t index) const;
|
||||
|
||||
// Gets or sets the value at specified index
|
||||
Internals::JsonArraySubscript operator[](size_t index);
|
||||
JsonArray() : _data(0) {}
|
||||
JsonArray(Internals::JsonArrayData* arr) : _data(arr) {}
|
||||
JsonArray(Internals::JsonBuffer* buf)
|
||||
: _data(new (buf) Internals::JsonArrayData(buf)) {}
|
||||
|
||||
// Adds the specified value at the end of the array.
|
||||
//
|
||||
// bool add(TValue);
|
||||
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
|
||||
// std::string, String, JsonArray, JsonObject
|
||||
// std::string, String, JsonArrayData, JsonObject
|
||||
template <typename T>
|
||||
bool add(const T& value) {
|
||||
return add_impl<const T&>(value);
|
||||
@ -56,73 +42,23 @@ class JsonArray : public Internals::ReferenceType,
|
||||
bool add(T* value) {
|
||||
return add_impl<T*>(value);
|
||||
}
|
||||
//
|
||||
// bool add(TValue value, uint8_t decimals);
|
||||
// TValue = float, double
|
||||
template <typename T>
|
||||
DEPRECATED("Second argument is not supported anymore")
|
||||
bool add(T value, uint8_t) {
|
||||
return add_impl<const JsonVariant &>(JsonVariant(value));
|
||||
|
||||
iterator begin() {
|
||||
if (!_data) return iterator();
|
||||
return _data->begin();
|
||||
}
|
||||
|
||||
// Sets the value at specified index.
|
||||
//
|
||||
// bool add(size_t index, const TValue&);
|
||||
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
|
||||
// std::string, String, JsonArray, JsonObject
|
||||
template <typename T>
|
||||
bool set(size_t index, const T &value) {
|
||||
return set_impl<const T &>(index, value);
|
||||
}
|
||||
//
|
||||
// bool add(size_t index, TValue);
|
||||
// TValue = char*, const char*, const FlashStringHelper*
|
||||
template <typename T>
|
||||
bool set(size_t index, T *value) {
|
||||
return set_impl<T *>(index, value);
|
||||
}
|
||||
//
|
||||
// bool set(size_t index, TValue value, uint8_t decimals);
|
||||
// TValue = float, double
|
||||
template <typename T>
|
||||
typename Internals::enable_if<Internals::is_floating_point<T>::value,
|
||||
bool>::type
|
||||
set(size_t index, T value, uint8_t decimals) {
|
||||
return set_impl<const JsonVariant &>(index, JsonVariant(value, decimals));
|
||||
const_iterator begin() const {
|
||||
if (!_data) return const_iterator();
|
||||
return _data->begin();
|
||||
}
|
||||
|
||||
// Gets the value at the specified index.
|
||||
template <typename T>
|
||||
typename Internals::JsonVariantAs<T>::type get(size_t index) const {
|
||||
const_iterator it = begin() += index;
|
||||
return it != end() ? it->as<T>() : Internals::JsonVariantDefault<T>::get();
|
||||
iterator end() {
|
||||
return iterator();
|
||||
}
|
||||
|
||||
// Check the type of the value at specified index.
|
||||
template <typename T>
|
||||
bool is(size_t index) const {
|
||||
const_iterator it = begin() += index;
|
||||
return it != end() ? it->is<T>() : false;
|
||||
}
|
||||
|
||||
// Creates a JsonArray and adds a reference at the end of the array.
|
||||
JsonArray &createNestedArray();
|
||||
|
||||
// Creates a JsonObject and adds a reference at the end of the array.
|
||||
JsonObject &createNestedObject();
|
||||
|
||||
// Removes element at specified index.
|
||||
void remove(size_t index) {
|
||||
remove(begin() += index);
|
||||
}
|
||||
using Internals::List<JsonVariant>::remove;
|
||||
|
||||
// Returns a reference an invalid JsonArray.
|
||||
// This object is meant to replace a NULL pointer.
|
||||
// This is used when memory allocation or JSON parsing fail.
|
||||
static JsonArray &invalid() {
|
||||
static JsonArray instance(NULL);
|
||||
return instance;
|
||||
const_iterator end() const {
|
||||
return const_iterator();
|
||||
}
|
||||
|
||||
// Imports a 1D array
|
||||
@ -146,7 +82,7 @@ class JsonArray : public Internals::ReferenceType,
|
||||
bool copyFrom(T (&array)[N1][N2]) {
|
||||
bool ok = true;
|
||||
for (size_t i = 0; i < N1; i++) {
|
||||
JsonArray &nestedArray = createNestedArray();
|
||||
JsonArray nestedArray = createNestedArray();
|
||||
for (size_t j = 0; j < N2; j++) {
|
||||
ok &= nestedArray.add(array[i][j]);
|
||||
}
|
||||
@ -172,15 +108,83 @@ class JsonArray : public Internals::ReferenceType,
|
||||
// Exports a 2D array
|
||||
template <typename T, size_t N1, size_t N2>
|
||||
void copyTo(T (&array)[N1][N2]) const {
|
||||
if (!_data) return;
|
||||
size_t i = 0;
|
||||
for (const_iterator it = begin(); it != end() && i < N1; ++it) {
|
||||
it->as<JsonArray>().copyTo(array[i++]);
|
||||
}
|
||||
}
|
||||
|
||||
JsonArray createNestedArray();
|
||||
JsonObject createNestedObject();
|
||||
|
||||
Internals::JsonArraySubscript operator[](size_t index);
|
||||
|
||||
const Internals::JsonArraySubscript operator[](size_t index) const;
|
||||
|
||||
bool operator==(const JsonArray& rhs) const {
|
||||
return _data == rhs._data;
|
||||
}
|
||||
|
||||
// Gets the value at the specified index.
|
||||
template <typename T>
|
||||
typename Internals::JsonVariantAs<T>::type get(size_t index) const {
|
||||
const_iterator it = begin() += index;
|
||||
return it != end() ? it->as<T>() : Internals::JsonVariantDefault<T>::get();
|
||||
}
|
||||
|
||||
// Check the type of the value at specified index.
|
||||
template <typename T>
|
||||
bool is(size_t index) const {
|
||||
const_iterator it = begin() += index;
|
||||
return it != end() ? it->is<T>() : false;
|
||||
}
|
||||
|
||||
// Removes element at specified position.
|
||||
void remove(iterator it) {
|
||||
if (!_data) return;
|
||||
_data->remove(it);
|
||||
}
|
||||
|
||||
// Removes element at specified index.
|
||||
void remove(size_t index) {
|
||||
remove(begin() += index);
|
||||
}
|
||||
|
||||
// Sets the value at specified index.
|
||||
//
|
||||
// bool add(size_t index, const TValue&);
|
||||
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
|
||||
// std::string, String, JsonArrayData, JsonObject
|
||||
template <typename T>
|
||||
bool set(size_t index, const T& value) {
|
||||
if (!_data) return false;
|
||||
return set_impl<const T&>(index, value);
|
||||
}
|
||||
//
|
||||
// bool add(size_t index, TValue);
|
||||
// TValue = char*, const char*, const FlashStringHelper*
|
||||
template <typename T>
|
||||
bool set(size_t index, T* value) {
|
||||
if (!_data) return false;
|
||||
return set_impl<T*>(index, value);
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
if (!_data) return 0;
|
||||
return _data->size();
|
||||
}
|
||||
|
||||
bool isNull() const {
|
||||
return _data == 0;
|
||||
}
|
||||
|
||||
template <typename Visitor>
|
||||
void visit(Visitor& visitor) const {
|
||||
if (_data)
|
||||
return visitor.acceptArray(*this);
|
||||
else
|
||||
visitor.acceptNull();
|
||||
}
|
||||
|
||||
private:
|
||||
@ -188,23 +192,17 @@ class JsonArray : public Internals::ReferenceType,
|
||||
bool set_impl(size_t index, TValueRef value) {
|
||||
iterator it = begin() += index;
|
||||
if (it == end()) return false;
|
||||
return Internals::ValueSaver<TValueRef>::save(_buffer, *it, value);
|
||||
return Internals::ValueSaver<TValueRef>::save(_data->_buffer, *it, value);
|
||||
}
|
||||
|
||||
template <typename TValueRef>
|
||||
bool add_impl(TValueRef value) {
|
||||
iterator it = Internals::List<JsonVariant>::add();
|
||||
if (!_data) return false;
|
||||
iterator it = _data->add();
|
||||
if (it == end()) return false;
|
||||
return Internals::ValueSaver<TValueRef>::save(_buffer, *it, value);
|
||||
return Internals::ValueSaver<TValueRef>::save(_data->_buffer, *it, value);
|
||||
}
|
||||
};
|
||||
|
||||
namespace Internals {
|
||||
template <>
|
||||
struct JsonVariantDefault<JsonArray> {
|
||||
static JsonArray &get() {
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
Internals::JsonArrayData* _data;
|
||||
};
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
||||
|
27
src/ArduinoJson/JsonArrayData.hpp
Normal file
27
src/ArduinoJson/JsonArrayData.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Data/List.hpp"
|
||||
#include "Data/ValueSaver.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
#include "Memory/JsonBufferAllocated.hpp"
|
||||
#include "Polyfills/type_traits.hpp"
|
||||
#include "Strings/StringTraits.hpp"
|
||||
|
||||
// Returns the size (in bytes) of an array with n elements.
|
||||
// Can be very handy to determine the size of a StaticJsonBuffer.
|
||||
#define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \
|
||||
(sizeof(ArduinoJson::Internals::JsonArrayData) + \
|
||||
(NUMBER_OF_ELEMENTS) * \
|
||||
sizeof(ArduinoJson::Internals::JsonArrayData::node_type))
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
struct JsonArrayData : List<JsonVariant>, JsonBufferAllocated {
|
||||
explicit JsonArrayData(JsonBuffer *buf) throw() : List<JsonVariant>(buf) {}
|
||||
};
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
@ -5,24 +5,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonArraySubscript.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
inline JsonArray &JsonArray::createNestedArray() {
|
||||
JsonArray *array = new (_buffer) JsonArray(_buffer);
|
||||
if (!array) return JsonArray::invalid();
|
||||
|
||||
add(array);
|
||||
return *array;
|
||||
inline JsonArray JsonArray::createNestedArray() {
|
||||
if (!_data) return JsonArray();
|
||||
JsonArray array(_data->_buffer);
|
||||
if (!array.isNull()) add(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
inline JsonObject &JsonArray::createNestedObject() {
|
||||
JsonObject *object = new (_buffer) JsonObject(_buffer);
|
||||
if (!object) return JsonObject::invalid();
|
||||
|
||||
add(object);
|
||||
return *object;
|
||||
inline JsonObject JsonArray::createNestedObject() {
|
||||
if (!_data) return JsonObject();
|
||||
JsonObject object(_data->_buffer);
|
||||
if (!object.isNull()) add(object);
|
||||
return object;
|
||||
}
|
||||
} // namespace ArduinoJson
|
||||
|
@ -16,7 +16,7 @@ namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
|
||||
public:
|
||||
FORCE_INLINE JsonArraySubscript(JsonArray& array, size_t index)
|
||||
FORCE_INLINE JsonArraySubscript(JsonArray array, size_t index)
|
||||
: _array(array), _index(index) {}
|
||||
|
||||
FORCE_INLINE JsonArraySubscript& operator=(const JsonArraySubscript& src) {
|
||||
@ -43,8 +43,8 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
|
||||
return *this;
|
||||
}
|
||||
|
||||
FORCE_INLINE bool success() const {
|
||||
return _index < _array.size();
|
||||
FORCE_INLINE bool isNull() const {
|
||||
return _index >= _array.size();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@ -80,7 +80,7 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
|
||||
}
|
||||
|
||||
private:
|
||||
JsonArray& _array;
|
||||
JsonArray _array;
|
||||
const size_t _index;
|
||||
};
|
||||
|
||||
@ -103,7 +103,7 @@ inline Internals::JsonArraySubscript JsonArray::operator[](size_t index) {
|
||||
|
||||
inline const Internals::JsonArraySubscript JsonArray::operator[](
|
||||
size_t index) const {
|
||||
return Internals::JsonArraySubscript(*const_cast<JsonArray*>(this), index);
|
||||
return Internals::JsonArraySubscript(*this, index);
|
||||
}
|
||||
} // namespace ArduinoJson
|
||||
|
||||
|
@ -4,110 +4,82 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Data/List.hpp"
|
||||
#include "Data/ReferenceType.hpp"
|
||||
#include "Data/ValueSaver.hpp"
|
||||
#include "JsonPair.hpp"
|
||||
#include "Memory/JsonBufferAllocated.hpp"
|
||||
#include "Polyfills/type_traits.hpp"
|
||||
#include "Strings/StringTraits.hpp"
|
||||
|
||||
// Returns the size (in bytes) of an object with n elements.
|
||||
// Can be very handy to determine the size of a StaticJsonBuffer.
|
||||
#define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \
|
||||
(sizeof(JsonObject) + (NUMBER_OF_ELEMENTS) * sizeof(JsonObject::node_type))
|
||||
#include "./JsonObjectData.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
// Forward declarations
|
||||
class JsonArray;
|
||||
class JsonBuffer;
|
||||
namespace Internals {
|
||||
template <typename>
|
||||
class JsonObjectSubscript;
|
||||
}
|
||||
class JsonObject {
|
||||
friend class JsonVariant;
|
||||
|
||||
class JsonObject : public Internals::ReferenceType,
|
||||
public Internals::NonCopyable,
|
||||
public Internals::List<JsonPair>,
|
||||
public Internals::JsonBufferAllocated {
|
||||
public:
|
||||
// Create an empty JsonArray attached to the specified JsonBuffer.
|
||||
// You should not use this constructor directly.
|
||||
explicit JsonObject(Internals::JsonBuffer* buf) throw()
|
||||
: Internals::List<JsonPair>(buf) {}
|
||||
typedef Internals::JsonObjectData::iterator iterator;
|
||||
typedef Internals::JsonObjectData::const_iterator const_iterator;
|
||||
|
||||
// Gets or sets the value associated with the specified key.
|
||||
//
|
||||
// JsonObjectSubscript operator[](TKey)
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TString>
|
||||
Internals::JsonObjectSubscript<const TString&> operator[](
|
||||
const TString& key) {
|
||||
return Internals::JsonObjectSubscript<const TString&>(*this, key);
|
||||
}
|
||||
//
|
||||
// JsonObjectSubscript operator[](TKey)
|
||||
// TKey = char*, const char*, char[], const char[N], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
Internals::JsonObjectSubscript<TString*> operator[](TString* key) {
|
||||
return Internals::JsonObjectSubscript<TString*>(*this, key);
|
||||
JsonObject() : _data(0) {}
|
||||
JsonObject(Internals::JsonObjectData* object) : _data(object) {}
|
||||
JsonObject(Internals::JsonBuffer* buf)
|
||||
: _data(new (buf) Internals::JsonObjectData(buf)) {}
|
||||
|
||||
iterator begin() {
|
||||
if (!_data) return iterator();
|
||||
return _data->begin();
|
||||
}
|
||||
|
||||
// Gets the value associated with the specified key.
|
||||
//
|
||||
// const JsonObjectSubscript operator[](TKey) const;
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TString>
|
||||
const Internals::JsonObjectSubscript<const TString&> operator[](
|
||||
const TString& key) const {
|
||||
return Internals::JsonObjectSubscript<const TString&>(
|
||||
*const_cast<JsonObject*>(this), key);
|
||||
}
|
||||
//
|
||||
// const JsonObjectSubscript operator[](TKey) const;
|
||||
// TKey = const char*, const char[N], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
const Internals::JsonObjectSubscript<TString*> operator[](
|
||||
TString* key) const {
|
||||
return Internals::JsonObjectSubscript<TString*>(
|
||||
*const_cast<JsonObject*>(this), key);
|
||||
const_iterator begin() const {
|
||||
if (!_data) return const_iterator();
|
||||
return _data->begin();
|
||||
}
|
||||
|
||||
// Sets the specified key with the specified value.
|
||||
// Tells weither the specified key is present and associated with a value.
|
||||
//
|
||||
// bool set(TKey, TValue);
|
||||
// bool containsKey(TKey);
|
||||
// TKey = const std::string&, const String&
|
||||
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
|
||||
// std::string, String, JsonArray, JsonObject
|
||||
template <typename TValue, typename TString>
|
||||
bool set(const TString& key, const TValue& value) {
|
||||
return set_impl<const TString&, const TValue&>(key, value);
|
||||
template <typename TString>
|
||||
bool containsKey(const TString& key) const {
|
||||
return containsKey_impl<const TString&>(key);
|
||||
}
|
||||
//
|
||||
// bool set(TKey, TValue);
|
||||
// bool containsKey(TKey);
|
||||
// TKey = char*, const char*, char[], const char[], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
bool containsKey(TString* key) const {
|
||||
return containsKey_impl<TString*>(key);
|
||||
}
|
||||
|
||||
iterator end() {
|
||||
return iterator();
|
||||
}
|
||||
|
||||
const_iterator end() const {
|
||||
return const_iterator();
|
||||
}
|
||||
|
||||
// Creates and adds a JsonArray.
|
||||
//
|
||||
// JsonArray createNestedArray(TKey);
|
||||
// TKey = const std::string&, const String&
|
||||
// TValue = char*, const char*, const FlashStringHelper*
|
||||
template <typename TValue, typename TString>
|
||||
bool set(const TString& key, TValue* value) {
|
||||
return set_impl<const TString&, TValue*>(key, value);
|
||||
template <typename TString>
|
||||
JsonArray createNestedArray(const TString& key);
|
||||
// JsonArray createNestedArray(TKey);
|
||||
// TKey = char*, const char*, char[], const char[], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
JsonArray createNestedArray(TString* key);
|
||||
|
||||
// Creates and adds a JsonObject.
|
||||
//
|
||||
// JsonObject createNestedObject(TKey);
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TString>
|
||||
JsonObject createNestedObject(const TString& key) {
|
||||
if (!_data) return JsonObject();
|
||||
return createNestedObject_impl<const TString&>(key);
|
||||
}
|
||||
//
|
||||
// bool set(TKey, const TValue&);
|
||||
// TKey = char*, const char*, const FlashStringHelper*
|
||||
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
|
||||
// std::string, String, JsonArray, JsonObject
|
||||
template <typename TValue, typename TString>
|
||||
bool set(TString* key, const TValue& value) {
|
||||
return set_impl<TString*, const TValue&>(key, value);
|
||||
}
|
||||
//
|
||||
// bool set(TKey, TValue);
|
||||
// TKey = char*, const char*, const FlashStringHelper*
|
||||
// TValue = char*, const char*, const FlashStringHelper*
|
||||
template <typename TValue, typename TString>
|
||||
bool set(TString* key, TValue* value) {
|
||||
return set_impl<TString*, TValue*>(key, value);
|
||||
// JsonObject createNestedObject(TKey);
|
||||
// TKey = char*, const char*, char[], const char[], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
JsonObject createNestedObject(TString* key) {
|
||||
return createNestedObject_impl<TString*>(key);
|
||||
}
|
||||
|
||||
// Gets the value associated with the specified key.
|
||||
@ -152,51 +124,48 @@ class JsonObject : public Internals::ReferenceType,
|
||||
return is_impl<TString*, TValue>(key);
|
||||
}
|
||||
|
||||
// Creates and adds a JsonArray.
|
||||
// Gets or sets the value associated with the specified key.
|
||||
//
|
||||
// JsonArray& createNestedArray(TKey);
|
||||
// JsonObjectSubscript operator[](TKey)
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TString>
|
||||
JsonArray& createNestedArray(const TString& key) {
|
||||
return createNestedArray_impl<const TString&>(key);
|
||||
Internals::JsonObjectSubscript<const TString&> operator[](
|
||||
const TString& key) {
|
||||
return Internals::JsonObjectSubscript<const TString&>(*this, key);
|
||||
}
|
||||
// JsonArray& createNestedArray(TKey);
|
||||
// TKey = char*, const char*, char[], const char[], const FlashStringHelper*
|
||||
//
|
||||
// JsonObjectSubscript operator[](TKey)
|
||||
// TKey = char*, const char*, char[], const char[N], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
JsonArray& createNestedArray(TString* key) {
|
||||
return createNestedArray_impl<TString*>(key);
|
||||
Internals::JsonObjectSubscript<TString*> operator[](TString* key) {
|
||||
return Internals::JsonObjectSubscript<TString*>(*this, key);
|
||||
}
|
||||
|
||||
// Creates and adds a JsonObject.
|
||||
// Gets the value associated with the specified key.
|
||||
//
|
||||
// JsonObject& createNestedObject(TKey);
|
||||
// const JsonObjectSubscript operator[](TKey) const;
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TString>
|
||||
JsonObject& createNestedObject(const TString& key) {
|
||||
return createNestedObject_impl<const TString&>(key);
|
||||
const Internals::JsonObjectSubscript<const TString&> operator[](
|
||||
const TString& key) const {
|
||||
return Internals::JsonObjectSubscript<const TString&>(*this, key);
|
||||
}
|
||||
//
|
||||
// JsonObject& createNestedObject(TKey);
|
||||
// TKey = char*, const char*, char[], const char[], const FlashStringHelper*
|
||||
// const JsonObjectSubscript operator[](TKey) const;
|
||||
// TKey = const char*, const char[N], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
JsonObject& createNestedObject(TString* key) {
|
||||
return createNestedObject_impl<TString*>(key);
|
||||
const Internals::JsonObjectSubscript<TString*> operator[](
|
||||
TString* key) const {
|
||||
return Internals::JsonObjectSubscript<TString*>(*this, key);
|
||||
}
|
||||
|
||||
// Tells weither the specified key is present and associated with a value.
|
||||
//
|
||||
// bool containsKey(TKey);
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TString>
|
||||
bool containsKey(const TString& key) const {
|
||||
return findKey<const TString&>(key) != end();
|
||||
bool operator==(const JsonObject& rhs) const {
|
||||
return _data == rhs._data;
|
||||
}
|
||||
//
|
||||
// bool containsKey(TKey);
|
||||
// TKey = char*, const char*, char[], const char[], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
bool containsKey(TString* key) const {
|
||||
return findKey<TString*>(key) != end();
|
||||
|
||||
void remove(iterator it) {
|
||||
if (!_data) return;
|
||||
_data->remove(it);
|
||||
}
|
||||
|
||||
// Removes the specified key and the associated value.
|
||||
@ -205,33 +174,81 @@ class JsonObject : public Internals::ReferenceType,
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TString>
|
||||
void remove(const TString& key) {
|
||||
remove(findKey<const TString&>(key));
|
||||
remove_impl<const TString&>(key);
|
||||
}
|
||||
//
|
||||
// void remove(TKey);
|
||||
// TKey = char*, const char*, char[], const char[], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
void remove(TString* key) {
|
||||
remove(findKey<TString*>(key));
|
||||
remove_impl<TString*>(key);
|
||||
}
|
||||
|
||||
// Sets the specified key with the specified value.
|
||||
//
|
||||
// bool set(TKey, TValue);
|
||||
// TKey = const std::string&, const String&
|
||||
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
|
||||
// std::string, String, JsonArray, JsonObject
|
||||
template <typename TValue, typename TString>
|
||||
bool set(const TString& key, const TValue& value) {
|
||||
return set_impl<const TString&, const TValue&>(key, value);
|
||||
}
|
||||
//
|
||||
// void remove(iterator)
|
||||
using Internals::List<JsonPair>::remove;
|
||||
// bool set(TKey, TValue);
|
||||
// TKey = const std::string&, const String&
|
||||
// TValue = char*, const char*, const FlashStringHelper*
|
||||
template <typename TValue, typename TString>
|
||||
bool set(const TString& key, TValue* value) {
|
||||
return set_impl<const TString&, TValue*>(key, value);
|
||||
}
|
||||
//
|
||||
// bool set(TKey, const TValue&);
|
||||
// TKey = char*, const char*, const FlashStringHelper*
|
||||
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
|
||||
// std::string, String, JsonArray, JsonObject
|
||||
template <typename TValue, typename TString>
|
||||
bool set(TString* key, const TValue& value) {
|
||||
return set_impl<TString*, const TValue&>(key, value);
|
||||
}
|
||||
//
|
||||
// bool set(TKey, TValue);
|
||||
// TKey = char*, const char*, const FlashStringHelper*
|
||||
// TValue = char*, const char*, const FlashStringHelper*
|
||||
template <typename TValue, typename TString>
|
||||
bool set(TString* key, TValue* value) {
|
||||
return set_impl<TString*, TValue*>(key, value);
|
||||
}
|
||||
|
||||
// Returns a reference an invalid JsonObject.
|
||||
// This object is meant to replace a NULL pointer.
|
||||
// This is used when memory allocation or JSON parsing fail.
|
||||
static JsonObject& invalid() {
|
||||
static JsonObject instance(NULL);
|
||||
return instance;
|
||||
size_t size() const {
|
||||
if (!_data) return 0;
|
||||
return _data->size();
|
||||
}
|
||||
|
||||
bool isNull() const {
|
||||
return _data == 0;
|
||||
}
|
||||
|
||||
template <typename Visitor>
|
||||
void visit(Visitor& visitor) const {
|
||||
return visitor.acceptObject(*this);
|
||||
if (_data)
|
||||
visitor.acceptObject(*this);
|
||||
else
|
||||
return visitor.acceptNull();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename TStringRef>
|
||||
bool containsKey_impl(TStringRef key) const {
|
||||
return findKey<TStringRef>(key) != end();
|
||||
}
|
||||
|
||||
template <typename TStringRef>
|
||||
JsonArray createNestedArray_impl(TStringRef key);
|
||||
|
||||
template <typename TStringRef>
|
||||
JsonObject createNestedObject_impl(TStringRef key);
|
||||
|
||||
// Returns the list node that matches the specified key.
|
||||
template <typename TStringRef>
|
||||
iterator findKey(TStringRef key) {
|
||||
@ -254,26 +271,6 @@ class JsonObject : public Internals::ReferenceType,
|
||||
: Internals::JsonVariantDefault<TValue>::get();
|
||||
}
|
||||
|
||||
template <typename TStringRef, typename TValueRef>
|
||||
bool set_impl(TStringRef key, TValueRef value) {
|
||||
// ignore null key
|
||||
if (Internals::StringTraits<TStringRef>::is_null(key)) return false;
|
||||
|
||||
// search a matching key
|
||||
iterator it = findKey<TStringRef>(key);
|
||||
if (it == end()) {
|
||||
// add the key
|
||||
it = Internals::List<JsonPair>::add();
|
||||
if (it == end()) return false;
|
||||
bool key_ok =
|
||||
Internals::ValueSaver<TStringRef>::save(_buffer, it->key, key);
|
||||
if (!key_ok) return false;
|
||||
}
|
||||
|
||||
// save the value
|
||||
return Internals::ValueSaver<TValueRef>::save(_buffer, it->value, value);
|
||||
}
|
||||
|
||||
template <typename TStringRef, typename TValue>
|
||||
bool is_impl(TStringRef key) const {
|
||||
const_iterator it = findKey<TStringRef>(key);
|
||||
@ -281,18 +278,34 @@ class JsonObject : public Internals::ReferenceType,
|
||||
}
|
||||
|
||||
template <typename TStringRef>
|
||||
JsonArray& createNestedArray_impl(TStringRef key);
|
||||
|
||||
template <typename TStringRef>
|
||||
JsonObject& createNestedObject_impl(TStringRef key);
|
||||
};
|
||||
|
||||
namespace Internals {
|
||||
template <>
|
||||
struct JsonVariantDefault<JsonObject> {
|
||||
static JsonObject& get() {
|
||||
return JsonObject::invalid();
|
||||
void remove_impl(TStringRef key) {
|
||||
if (!_data) return;
|
||||
_data->remove(findKey<TStringRef>(key));
|
||||
}
|
||||
|
||||
template <typename TStringRef, typename TValueRef>
|
||||
bool set_impl(TStringRef key, TValueRef value) {
|
||||
if (!_data) return false;
|
||||
|
||||
// ignore null key
|
||||
if (Internals::StringTraits<TStringRef>::is_null(key)) return false;
|
||||
|
||||
// search a matching key
|
||||
iterator it = findKey<TStringRef>(key);
|
||||
if (it == end()) {
|
||||
// add the key
|
||||
it = _data->add();
|
||||
if (it == end()) return false;
|
||||
bool key_ok =
|
||||
Internals::ValueSaver<TStringRef>::save(_data->_buffer, it->key, key);
|
||||
if (!key_ok) return false;
|
||||
}
|
||||
|
||||
// save the value
|
||||
return Internals::ValueSaver<TValueRef>::save(_data->_buffer, it->value,
|
||||
value);
|
||||
}
|
||||
|
||||
Internals::JsonObjectData* _data;
|
||||
};
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
||||
|
27
src/ArduinoJson/JsonObjectData.hpp
Normal file
27
src/ArduinoJson/JsonObjectData.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Data/List.hpp"
|
||||
#include "Data/ValueSaver.hpp"
|
||||
#include "JsonPair.hpp"
|
||||
#include "Memory/JsonBufferAllocated.hpp"
|
||||
#include "Polyfills/type_traits.hpp"
|
||||
#include "Strings/StringTraits.hpp"
|
||||
|
||||
// Returns the size (in bytes) of an object with n elements.
|
||||
// Can be very handy to determine the size of a StaticJsonBuffer.
|
||||
#define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \
|
||||
(sizeof(ArduinoJson::Internals::JsonObjectData) + \
|
||||
(NUMBER_OF_ELEMENTS) * \
|
||||
sizeof(ArduinoJson::Internals::JsonObjectData::node_type))
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
struct JsonObjectData : List<JsonPair>, JsonBufferAllocated {
|
||||
explicit JsonObjectData(JsonBuffer* buf) throw() : List<JsonPair>(buf) {}
|
||||
};
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
@ -6,23 +6,32 @@
|
||||
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
#include "JsonObjectSubscript.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
template <typename TStringRef>
|
||||
inline JsonArray &JsonObject::createNestedArray_impl(TStringRef key) {
|
||||
JsonArray *array = new (_buffer) JsonArray(_buffer);
|
||||
if (!array) return JsonArray::invalid();
|
||||
set(key, array);
|
||||
return *array;
|
||||
template <typename TString>
|
||||
inline JsonArray JsonObject::createNestedArray(const TString& key) {
|
||||
return createNestedArray_impl<const TString&>(key);
|
||||
}
|
||||
|
||||
template <typename TString>
|
||||
inline JsonArray JsonObject::createNestedArray(TString* key) {
|
||||
return createNestedArray_impl<TString*>(key);
|
||||
}
|
||||
|
||||
template <typename TStringRef>
|
||||
inline JsonObject &JsonObject::createNestedObject_impl(TStringRef key) {
|
||||
JsonObject *object = new (_buffer) JsonObject(_buffer);
|
||||
if (!object) return JsonObject::invalid();
|
||||
set(key, object);
|
||||
return *object;
|
||||
inline JsonArray JsonObject::createNestedArray_impl(TStringRef key) {
|
||||
if (!_data) return JsonArray();
|
||||
JsonArray array(_data->_buffer);
|
||||
if (!array.isNull()) set(key, array);
|
||||
return array;
|
||||
}
|
||||
|
||||
template <typename TStringRef>
|
||||
inline JsonObject JsonObject::createNestedObject_impl(TStringRef key) {
|
||||
if (!_data) return JsonObject();
|
||||
JsonObject object(_data->_buffer);
|
||||
if (!object.isNull()) set(key, object);
|
||||
return object;
|
||||
}
|
||||
} // namespace ArduinoJson
|
||||
|
@ -22,7 +22,7 @@ class JsonObjectSubscript
|
||||
typedef JsonObjectSubscript<TStringRef> this_type;
|
||||
|
||||
public:
|
||||
FORCE_INLINE JsonObjectSubscript(JsonObject& object, TStringRef key)
|
||||
FORCE_INLINE JsonObjectSubscript(JsonObject object, TStringRef key)
|
||||
: _object(object), _key(key) {}
|
||||
|
||||
FORCE_INLINE this_type &operator=(const this_type &src) {
|
||||
@ -50,8 +50,8 @@ class JsonObjectSubscript
|
||||
return *this;
|
||||
}
|
||||
|
||||
FORCE_INLINE bool success() const {
|
||||
return _object.containsKey(_key);
|
||||
FORCE_INLINE bool isNull() const {
|
||||
return !_object.containsKey(_key);
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
@ -88,9 +88,42 @@ class JsonObjectSubscript
|
||||
}
|
||||
|
||||
private:
|
||||
JsonObject& _object;
|
||||
JsonObject _object;
|
||||
TStringRef _key;
|
||||
};
|
||||
|
||||
template <typename TImpl>
|
||||
template <typename TString>
|
||||
inline typename enable_if<StringTraits<TString>::has_equals,
|
||||
const JsonObjectSubscript<const TString &> >::type
|
||||
JsonVariantSubscripts<TImpl>::operator[](const TString &key) const {
|
||||
return impl()->template as<JsonObject>()[key];
|
||||
}
|
||||
|
||||
template <typename TImpl>
|
||||
template <typename TString>
|
||||
inline typename enable_if<StringTraits<TString>::has_equals,
|
||||
JsonObjectSubscript<const TString &> >::type
|
||||
JsonVariantSubscripts<TImpl>::operator[](const TString &key) {
|
||||
return impl()->template as<JsonObject>()[key];
|
||||
}
|
||||
|
||||
template <typename TImpl>
|
||||
template <typename TString>
|
||||
inline typename enable_if<StringTraits<const TString *>::has_equals,
|
||||
JsonObjectSubscript<const TString *> >::type
|
||||
JsonVariantSubscripts<TImpl>::operator[](const TString *key) {
|
||||
return impl()->template as<JsonObject>()[key];
|
||||
}
|
||||
|
||||
template <typename TImpl>
|
||||
template <typename TString>
|
||||
inline typename enable_if<StringTraits<TString *>::has_equals,
|
||||
const JsonObjectSubscript<const TString *> >::type
|
||||
JsonVariantSubscripts<TImpl>::operator[](const TString *key) const {
|
||||
return impl()->template as<JsonObject>()[key];
|
||||
}
|
||||
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
||||
|
||||
|
@ -8,9 +8,9 @@
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
// A key value pair for JsonObject.
|
||||
// A key value pair for JsonObjectData.
|
||||
struct JsonPair {
|
||||
const char* key;
|
||||
JsonVariant value;
|
||||
};
|
||||
}
|
||||
} // namespace ArduinoJson
|
||||
|
@ -104,25 +104,8 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
|
||||
_content.asString = value;
|
||||
}
|
||||
|
||||
// Create a JsonVariant containing a reference to an array.
|
||||
// CAUTION: we are lying about constness, because the array can be modified if
|
||||
// the variant is converted back to a JsonArray&
|
||||
JsonVariant(const JsonArray &array);
|
||||
|
||||
// Create a JsonVariant containing a reference to an object.
|
||||
// CAUTION: we are lying about constness, because the object can be modified
|
||||
// if the variant is converted back to a JsonObject&
|
||||
JsonVariant(const JsonObject &object);
|
||||
|
||||
JsonVariant(JsonArray *array) {
|
||||
_content.asArray = array;
|
||||
_type = Internals::JSON_ARRAY;
|
||||
}
|
||||
|
||||
JsonVariant(JsonObject *object) {
|
||||
_content.asObject = object;
|
||||
_type = Internals::JSON_OBJECT;
|
||||
}
|
||||
JsonVariant(JsonArray array);
|
||||
JsonVariant(JsonObject object);
|
||||
|
||||
// Get the variant as the specified type.
|
||||
//
|
||||
@ -179,48 +162,23 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
|
||||
return s;
|
||||
}
|
||||
//
|
||||
// JsonArray& as<JsonArray> const;
|
||||
// JsonArray& as<JsonArray&> const;
|
||||
// JsonArray as<JsonArray>() const;
|
||||
// const JsonArray as<const JsonArray>() const;
|
||||
template <typename T>
|
||||
typename Internals::enable_if<
|
||||
Internals::is_same<typename Internals::remove_reference<T>::type,
|
||||
Internals::is_same<typename Internals::remove_const<T>::type,
|
||||
JsonArray>::value,
|
||||
JsonArray &>::type
|
||||
as() const {
|
||||
return variantAsArray();
|
||||
}
|
||||
JsonArray>::type
|
||||
as() const;
|
||||
//
|
||||
// const JsonArray& as<const JsonArray&> const;
|
||||
// JsonObject as<JsonObject>() const;
|
||||
// const JsonObject as<const JsonObject>() const;
|
||||
template <typename T>
|
||||
typename Internals::enable_if<
|
||||
Internals::is_same<typename Internals::remove_reference<T>::type,
|
||||
const JsonArray>::value,
|
||||
const JsonArray &>::type
|
||||
as() const {
|
||||
return variantAsArray();
|
||||
}
|
||||
//
|
||||
// JsonObject& as<JsonObject> const;
|
||||
// JsonObject& as<JsonObject&> const;
|
||||
template <typename T>
|
||||
typename Internals::enable_if<
|
||||
Internals::is_same<typename Internals::remove_reference<T>::type,
|
||||
Internals::is_same<typename Internals::remove_const<T>::type,
|
||||
JsonObject>::value,
|
||||
JsonObject &>::type
|
||||
as() const {
|
||||
return variantAsObject();
|
||||
}
|
||||
//
|
||||
// JsonObject& as<const JsonObject> const;
|
||||
// JsonObject& as<const JsonObject&> const;
|
||||
template <typename T>
|
||||
typename Internals::enable_if<
|
||||
Internals::is_same<typename Internals::remove_reference<T>::type,
|
||||
const JsonObject>::value,
|
||||
const JsonObject &>::type
|
||||
as() const {
|
||||
return variantAsObject();
|
||||
}
|
||||
T>::type
|
||||
as() const;
|
||||
//
|
||||
// JsonVariant as<JsonVariant> const;
|
||||
template <typename T>
|
||||
@ -275,13 +233,10 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
|
||||
}
|
||||
//
|
||||
// bool is<JsonArray> const;
|
||||
// bool is<JsonArray&> const;
|
||||
// bool is<const JsonArray&> const;
|
||||
// bool is<const JsonArray> const;
|
||||
template <typename T>
|
||||
typename Internals::enable_if<
|
||||
Internals::is_same<
|
||||
typename Internals::remove_const<
|
||||
typename Internals::remove_reference<T>::type>::type,
|
||||
Internals::is_same<typename Internals::remove_const<T>::type,
|
||||
JsonArray>::value,
|
||||
bool>::type
|
||||
is() const {
|
||||
@ -289,13 +244,10 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
|
||||
}
|
||||
//
|
||||
// bool is<JsonObject> const;
|
||||
// bool is<JsonObject&> const;
|
||||
// bool is<const JsonObject&> const;
|
||||
// bool is<const JsonObject> const;
|
||||
template <typename T>
|
||||
typename Internals::enable_if<
|
||||
Internals::is_same<
|
||||
typename Internals::remove_const<
|
||||
typename Internals::remove_reference<T>::type>::type,
|
||||
Internals::is_same<typename Internals::remove_const<T>::type,
|
||||
JsonObject>::value,
|
||||
bool>::type
|
||||
is() const {
|
||||
@ -303,8 +255,8 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
|
||||
}
|
||||
|
||||
// Returns true if the variant has a value
|
||||
bool success() const {
|
||||
return _type != Internals::JSON_UNDEFINED;
|
||||
bool isNull() const {
|
||||
return _type == Internals::JSON_UNDEFINED;
|
||||
}
|
||||
|
||||
template <typename Visitor>
|
||||
@ -315,10 +267,10 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
|
||||
return visitor.acceptFloat(_content.asFloat);
|
||||
|
||||
case JSON_ARRAY:
|
||||
return visitor.acceptArray(*_content.asArray);
|
||||
return visitor.acceptArray(_content.asArray);
|
||||
|
||||
case JSON_OBJECT:
|
||||
return visitor.acceptObject(*_content.asObject);
|
||||
return visitor.acceptObject(_content.asObject);
|
||||
|
||||
case JSON_STRING:
|
||||
return visitor.acceptString(_content.asString);
|
||||
@ -336,13 +288,13 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
|
||||
return visitor.acceptBoolean(_content.asInteger != 0);
|
||||
|
||||
default: // JSON_UNDEFINED
|
||||
return visitor.acceptUndefined();
|
||||
return visitor.acceptNull();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
JsonArray &variantAsArray() const;
|
||||
JsonObject &variantAsObject() const;
|
||||
JsonArray variantAsArray() const;
|
||||
JsonObject variantAsObject() const;
|
||||
const char *variantAsString() const;
|
||||
template <typename T>
|
||||
T variantAsFloat() const;
|
||||
|
@ -13,21 +13,6 @@ namespace Internals {
|
||||
template <typename TImpl>
|
||||
class JsonVariantCasts {
|
||||
public:
|
||||
// Gets the variant as an array.
|
||||
// Returns a reference to the JsonArray or JsonArray::invalid() if the
|
||||
// variant
|
||||
// is not an array.
|
||||
FORCE_INLINE operator JsonArray &() const {
|
||||
return impl()->template as<JsonArray &>();
|
||||
}
|
||||
|
||||
// Gets the variant as an object.
|
||||
// Returns a reference to the JsonObject or JsonObject::invalid() if the
|
||||
// variant is not an object.
|
||||
FORCE_INLINE operator JsonObject &() const {
|
||||
return impl()->template as<JsonObject &>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
FORCE_INLINE operator T() const {
|
||||
return impl()->template as<T>();
|
||||
|
@ -9,6 +9,9 @@
|
||||
#include "Strings/StringTraits.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
class JsonArray;
|
||||
class JsonObject;
|
||||
|
||||
namespace Internals {
|
||||
|
||||
template <typename TImpl>
|
||||
|
@ -5,8 +5,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "Configuration.hpp"
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
#include "JsonArrayData.hpp"
|
||||
#include "JsonObjectData.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
#include "Numbers/isFloat.hpp"
|
||||
#include "Numbers/isInteger.hpp"
|
||||
@ -17,32 +17,50 @@
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
inline JsonVariant::JsonVariant(const JsonArray &array) {
|
||||
if (array.success()) {
|
||||
inline JsonVariant::JsonVariant(JsonArray array) {
|
||||
if (!array.isNull()) {
|
||||
_type = Internals::JSON_ARRAY;
|
||||
_content.asArray = const_cast<JsonArray *>(&array);
|
||||
_content.asArray = array._data;
|
||||
} else {
|
||||
_type = Internals::JSON_UNDEFINED;
|
||||
}
|
||||
}
|
||||
|
||||
inline JsonVariant::JsonVariant(const JsonObject &object) {
|
||||
if (object.success()) {
|
||||
inline JsonVariant::JsonVariant(JsonObject object) {
|
||||
if (!object.isNull()) {
|
||||
_type = Internals::JSON_OBJECT;
|
||||
_content.asObject = const_cast<JsonObject *>(&object);
|
||||
_content.asObject = object._data;
|
||||
} else {
|
||||
_type = Internals::JSON_UNDEFINED;
|
||||
}
|
||||
}
|
||||
|
||||
inline JsonArray &JsonVariant::variantAsArray() const {
|
||||
if (_type == Internals::JSON_ARRAY) return *_content.asArray;
|
||||
return JsonArray::invalid();
|
||||
template <typename T>
|
||||
inline typename Internals::enable_if<
|
||||
Internals::is_same<typename Internals::remove_const<T>::type,
|
||||
JsonArray>::value,
|
||||
JsonArray>::type
|
||||
JsonVariant::as() const {
|
||||
return variantAsArray();
|
||||
}
|
||||
|
||||
inline JsonObject &JsonVariant::variantAsObject() const {
|
||||
if (_type == Internals::JSON_OBJECT) return *_content.asObject;
|
||||
return JsonObject::invalid();
|
||||
template <typename T>
|
||||
inline typename Internals::enable_if<
|
||||
Internals::is_same<typename Internals::remove_const<T>::type,
|
||||
JsonObject>::value,
|
||||
T>::type
|
||||
JsonVariant::as() const {
|
||||
return variantAsObject();
|
||||
}
|
||||
|
||||
inline JsonArray JsonVariant::variantAsArray() const {
|
||||
if (_type == Internals::JSON_ARRAY) return _content.asArray;
|
||||
return JsonArray();
|
||||
}
|
||||
|
||||
inline JsonObject JsonVariant::variantAsObject() const {
|
||||
if (_type == Internals::JSON_OBJECT) return _content.asObject;
|
||||
return JsonObject();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
@ -10,6 +10,8 @@
|
||||
#include "Strings/StringTraits.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
class JsonArray;
|
||||
class JsonObject;
|
||||
namespace Internals {
|
||||
|
||||
// Forward declarations.
|
||||
@ -30,14 +32,12 @@ class JsonVariantSubscripts {
|
||||
|
||||
// Mimics an array.
|
||||
// Returns the element at specified index if the variant is an array.
|
||||
// Returns JsonVariant::invalid() if the variant is not an array.
|
||||
FORCE_INLINE const JsonArraySubscript operator[](size_t index) const;
|
||||
FORCE_INLINE JsonArraySubscript operator[](size_t index);
|
||||
|
||||
// Mimics an object.
|
||||
// Returns the value associated with the specified key if the variant is
|
||||
// an object.
|
||||
// Return JsonVariant::invalid() if the variant is not an object.
|
||||
//
|
||||
// const JsonObjectSubscript operator[](TKey) const;
|
||||
// TKey = const std::string&, const String&
|
||||
@ -45,27 +45,21 @@ class JsonVariantSubscripts {
|
||||
FORCE_INLINE
|
||||
typename enable_if<StringTraits<TString>::has_equals,
|
||||
const JsonObjectSubscript<const TString &> >::type
|
||||
operator[](const TString &key) const {
|
||||
return impl()->template as<JsonObject>()[key];
|
||||
}
|
||||
operator[](const TString &key) const;
|
||||
//
|
||||
// const JsonObjectSubscript operator[](TKey) const;
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TString>
|
||||
FORCE_INLINE typename enable_if<StringTraits<TString>::has_equals,
|
||||
JsonObjectSubscript<const TString &> >::type
|
||||
operator[](const TString &key) {
|
||||
return impl()->template as<JsonObject>()[key];
|
||||
}
|
||||
operator[](const TString &key);
|
||||
//
|
||||
// JsonObjectSubscript operator[](TKey);
|
||||
// TKey = const char*, const char[N], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
FORCE_INLINE typename enable_if<StringTraits<const TString *>::has_equals,
|
||||
JsonObjectSubscript<const TString *> >::type
|
||||
operator[](const TString *key) {
|
||||
return impl()->template as<JsonObject>()[key];
|
||||
}
|
||||
operator[](const TString *key);
|
||||
//
|
||||
// JsonObjectSubscript operator[](TKey);
|
||||
// TKey = const char*, const char[N], const FlashStringHelper*
|
||||
@ -73,9 +67,7 @@ class JsonVariantSubscripts {
|
||||
FORCE_INLINE
|
||||
typename enable_if<StringTraits<TString *>::has_equals,
|
||||
const JsonObjectSubscript<const TString *> >::type
|
||||
operator[](const TString *key) const {
|
||||
return impl()->template as<JsonObject>()[key];
|
||||
}
|
||||
operator[](const TString *key) const;
|
||||
|
||||
private:
|
||||
const TImpl *impl() const {
|
||||
|
@ -9,7 +9,6 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "../Configuration.hpp"
|
||||
#include "../Polyfills/NonCopyable.hpp"
|
||||
#include "../Polyfills/attributes.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
@ -17,7 +16,7 @@ namespace Internals {
|
||||
// Handle the memory management (done in derived classes) and calls the parser.
|
||||
// This abstract class is implemented by StaticJsonBuffer which implements a
|
||||
// fixed memory allocation.
|
||||
class JsonBuffer : NonCopyable {
|
||||
class JsonBuffer {
|
||||
public:
|
||||
// Allocates n bytes in the JsonBuffer.
|
||||
// Return a pointer to the allocated memory or NULL if allocation fails.
|
||||
|
@ -240,13 +240,13 @@ class MsgPackDeserializer {
|
||||
}
|
||||
|
||||
DeserializationError readArray(JsonVariant &variant, size_t n) {
|
||||
JsonArray *array = new (_buffer) JsonArray(_buffer);
|
||||
if (!array) return DeserializationError::NoMemory;
|
||||
JsonArray array(_buffer);
|
||||
if (array.isNull()) return DeserializationError::NoMemory;
|
||||
variant = array;
|
||||
return readArray(*array, n);
|
||||
return readArray(array, n);
|
||||
}
|
||||
|
||||
DeserializationError readArray(JsonArray &array, size_t n) {
|
||||
DeserializationError readArray(JsonArray array, size_t n) {
|
||||
if (_nestingLimit == 0) return DeserializationError::TooDeep;
|
||||
--_nestingLimit;
|
||||
for (; n; --n) {
|
||||
@ -267,13 +267,13 @@ class MsgPackDeserializer {
|
||||
}
|
||||
|
||||
DeserializationError readObject(JsonVariant &variant, size_t n) {
|
||||
JsonObject *object = new (_buffer) JsonObject(_buffer);
|
||||
if (!object) return DeserializationError::NoMemory;
|
||||
JsonObject object(_buffer);
|
||||
if (object.isNull()) return DeserializationError::NoMemory;
|
||||
variant = object;
|
||||
return readObject(*object, n);
|
||||
return readObject(object, n);
|
||||
}
|
||||
|
||||
DeserializationError readObject(JsonObject &object, size_t n) {
|
||||
DeserializationError readObject(JsonObject object, size_t n) {
|
||||
if (_nestingLimit == 0) return DeserializationError::TooDeep;
|
||||
--_nestingLimit;
|
||||
for (; n; --n) {
|
||||
|
@ -140,7 +140,7 @@ class MsgPackSerializer {
|
||||
writeByte(value ? 0xC3 : 0xC2);
|
||||
}
|
||||
|
||||
void acceptUndefined() {
|
||||
void acceptNull() {
|
||||
writeByte(0xC0);
|
||||
}
|
||||
|
||||
|
@ -1,23 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
// A type that cannot be copied
|
||||
class NonCopyable {
|
||||
protected:
|
||||
NonCopyable() {}
|
||||
|
||||
private:
|
||||
// copy constructor is private
|
||||
NonCopyable(const NonCopyable&);
|
||||
|
||||
// copy operator is private
|
||||
NonCopyable& operator=(const NonCopyable&);
|
||||
};
|
||||
}
|
||||
}
|
@ -33,28 +33,26 @@ class StaticJsonDocument {
|
||||
return _root.as<T>();
|
||||
}
|
||||
|
||||
// JsonObject& to<JsonObject>()
|
||||
// JsonObject to<JsonObject>()
|
||||
template <typename T>
|
||||
typename Internals::enable_if<Internals::is_same<T, JsonObject>::value,
|
||||
JsonObject&>::type
|
||||
JsonObject>::type
|
||||
to() {
|
||||
clear();
|
||||
JsonObject* object = new (&_buffer) JsonObject(&_buffer);
|
||||
if (!object) return JsonObject::invalid();
|
||||
JsonObject object(&_buffer);
|
||||
_root = object;
|
||||
return *object;
|
||||
return object;
|
||||
}
|
||||
|
||||
// JsonArray& to<JsonArray>()
|
||||
// JsonArray to<JsonArray>()
|
||||
template <typename T>
|
||||
typename Internals::enable_if<Internals::is_same<T, JsonArray>::value,
|
||||
JsonArray&>::type
|
||||
JsonArray>::type
|
||||
to() {
|
||||
clear();
|
||||
JsonArray* array = new (&_buffer) JsonArray(&_buffer);
|
||||
if (!array) return JsonArray::invalid();
|
||||
JsonArray array(&_buffer);
|
||||
_root = array;
|
||||
return *array;
|
||||
return array;
|
||||
}
|
||||
|
||||
// JsonVariant to<JsonVariant>()
|
||||
|
@ -20,7 +20,7 @@ TEST_CASE("Gbathree") {
|
||||
"\"measlights\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,"
|
||||
"15,15]],\"measlights2\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],"
|
||||
"[15,15,15,15]],\"altc\":[2,2,2,2],\"altd\":[2,2,2,2]}");
|
||||
JsonObject& root = doc.as<JsonObject>();
|
||||
JsonObject root = doc.as<JsonObject>();
|
||||
|
||||
SECTION("Success") {
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
@ -81,8 +81,8 @@ TEST_CASE("Gbathree") {
|
||||
SECTION("Pulses") {
|
||||
// "pulses":[50,50,50]
|
||||
|
||||
JsonArray& array = root["pulses"];
|
||||
REQUIRE(array.success());
|
||||
JsonArray array = root["pulses"];
|
||||
REQUIRE(array.isNull() == false);
|
||||
|
||||
REQUIRE(3 == array.size());
|
||||
|
||||
@ -94,8 +94,8 @@ TEST_CASE("Gbathree") {
|
||||
SECTION("Act") {
|
||||
// "act":[2,1,2,2]
|
||||
|
||||
JsonArray& array = root["act"];
|
||||
REQUIRE(array.success());
|
||||
JsonArray array = root["act"];
|
||||
REQUIRE(array.isNull() == false);
|
||||
|
||||
REQUIRE(4 == array.size());
|
||||
REQUIRE(2 == array[0]);
|
||||
@ -107,12 +107,12 @@ TEST_CASE("Gbathree") {
|
||||
SECTION("Detectors") {
|
||||
// "detectors":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]]
|
||||
|
||||
JsonArray& array = root["detectors"];
|
||||
REQUIRE(array.success());
|
||||
JsonArray array = root["detectors"];
|
||||
REQUIRE(array.isNull() == false);
|
||||
REQUIRE(4 == array.size());
|
||||
|
||||
for (size_t i = 0; i < 4; i++) {
|
||||
JsonArray& nestedArray = array[i];
|
||||
JsonArray nestedArray = array[i];
|
||||
REQUIRE(4 == nestedArray.size());
|
||||
|
||||
for (size_t j = 0; j < 4; j++) {
|
||||
@ -124,8 +124,8 @@ TEST_CASE("Gbathree") {
|
||||
SECTION("Alta") {
|
||||
// alta:[2,2,2,2]
|
||||
|
||||
JsonArray& array = root["alta"];
|
||||
REQUIRE(array.success());
|
||||
JsonArray array = root["alta"];
|
||||
REQUIRE(array.isNull() == false);
|
||||
|
||||
REQUIRE(4 == array.size());
|
||||
|
||||
@ -137,8 +137,8 @@ TEST_CASE("Gbathree") {
|
||||
SECTION("Altb") {
|
||||
// altb:[2,2,2,2]
|
||||
|
||||
JsonArray& array = root["altb"];
|
||||
REQUIRE(array.success());
|
||||
JsonArray array = root["altb"];
|
||||
REQUIRE(array.isNull() == false);
|
||||
|
||||
REQUIRE(4 == array.size());
|
||||
|
||||
@ -150,12 +150,12 @@ TEST_CASE("Gbathree") {
|
||||
SECTION("Measlights") {
|
||||
// "measlights":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]]
|
||||
|
||||
JsonArray& array = root["measlights"];
|
||||
REQUIRE(array.success());
|
||||
JsonArray array = root["measlights"];
|
||||
REQUIRE(array.isNull() == false);
|
||||
REQUIRE(4 == array.size());
|
||||
|
||||
for (size_t i = 0; i < 4; i++) {
|
||||
JsonArray& nestedArray = array[i];
|
||||
JsonArray nestedArray = array[i];
|
||||
|
||||
REQUIRE(4 == nestedArray.size());
|
||||
|
||||
@ -168,12 +168,12 @@ TEST_CASE("Gbathree") {
|
||||
SECTION("Measlights2") {
|
||||
// "measlights2":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]]
|
||||
|
||||
JsonArray& array = root["measlights2"];
|
||||
REQUIRE(array.success());
|
||||
JsonArray array = root["measlights2"];
|
||||
REQUIRE(array.isNull() == false);
|
||||
REQUIRE(4 == array.size());
|
||||
|
||||
for (size_t i = 0; i < 4; i++) {
|
||||
JsonArray& nestedArray = array[i];
|
||||
JsonArray nestedArray = array[i];
|
||||
REQUIRE(4 == nestedArray.size());
|
||||
|
||||
for (size_t j = 0; j < 4; j++) {
|
||||
@ -185,8 +185,8 @@ TEST_CASE("Gbathree") {
|
||||
SECTION("Altc") {
|
||||
// altc:[2,2,2,2]
|
||||
|
||||
JsonArray& array = root["altc"];
|
||||
REQUIRE(array.success());
|
||||
JsonArray array = root["altc"];
|
||||
REQUIRE(array.isNull() == false);
|
||||
|
||||
REQUIRE(4 == array.size());
|
||||
|
||||
@ -198,8 +198,8 @@ TEST_CASE("Gbathree") {
|
||||
SECTION("Altd") {
|
||||
// altd:[2,2,2,2]
|
||||
|
||||
JsonArray& array = root["altd"];
|
||||
REQUIRE(array.success());
|
||||
JsonArray array = root["altd"];
|
||||
REQUIRE(array.isNull() == false);
|
||||
|
||||
REQUIRE(4 == array.size());
|
||||
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
TEST_CASE("JsonArray::add()") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& _array = doc.to<JsonArray>();
|
||||
JsonArray _array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("int") {
|
||||
_array.add(123);
|
||||
@ -40,30 +40,30 @@ TEST_CASE("JsonArray::add()") {
|
||||
|
||||
SECTION("nested array") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonArray& arr = doc2.to<JsonArray>();
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
|
||||
_array.add(arr);
|
||||
|
||||
REQUIRE(&arr == &_array[0].as<JsonArray&>());
|
||||
REQUIRE(_array[0].is<JsonArray&>());
|
||||
REQUIRE(arr == _array[0].as<JsonArray>());
|
||||
REQUIRE(_array[0].is<JsonArray>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonObject& obj = doc2.to<JsonObject>();
|
||||
JsonObject obj = doc2.to<JsonObject>();
|
||||
|
||||
_array.add(obj);
|
||||
|
||||
REQUIRE(&obj == &_array[0].as<JsonObject&>());
|
||||
REQUIRE(_array[0].is<JsonObject&>());
|
||||
REQUIRE(obj == _array[0].as<JsonObject>());
|
||||
REQUIRE(_array[0].is<JsonObject>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
const char* str = "hello";
|
||||
DynamicJsonDocument doc2;
|
||||
JsonArray& arr = doc2.to<JsonArray>();
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
arr.add(str);
|
||||
|
||||
_array.add(arr[0]);
|
||||
@ -74,7 +74,7 @@ TEST_CASE("JsonArray::add()") {
|
||||
SECTION("object subscript") {
|
||||
const char* str = "hello";
|
||||
DynamicJsonDocument doc2;
|
||||
JsonObject& obj = doc2.to<JsonObject>();
|
||||
JsonObject obj = doc2.to<JsonObject>();
|
||||
obj["x"] = str;
|
||||
|
||||
_array.add(obj["x"]);
|
||||
|
@ -7,10 +7,10 @@
|
||||
|
||||
TEST_CASE("JsonArray basics") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("SuccessIsTrue") {
|
||||
REQUIRE(array.success());
|
||||
SECTION("isNull()") {
|
||||
REQUIRE(array.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("InitialSizeIsZero") {
|
||||
@ -18,12 +18,12 @@ TEST_CASE("JsonArray basics") {
|
||||
}
|
||||
|
||||
SECTION("CreateNestedArray") {
|
||||
JsonArray& arr = array.createNestedArray();
|
||||
REQUIRE(&arr == &array[0].as<JsonArray&>());
|
||||
JsonArray arr = array.createNestedArray();
|
||||
REQUIRE(arr == array[0].as<JsonArray>());
|
||||
}
|
||||
|
||||
SECTION("CreateNestedObject") {
|
||||
JsonObject& obj = array.createNestedObject();
|
||||
REQUIRE(&obj == &array[0].as<JsonObject&>());
|
||||
JsonObject obj = array.createNestedObject();
|
||||
REQUIRE(obj == array[0].as<JsonObject>());
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
TEST_CASE("JsonArray::copyFrom()") {
|
||||
SECTION("OneDimension") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
char json[32];
|
||||
int source[] = {1, 2, 3};
|
||||
|
||||
@ -22,7 +22,7 @@ TEST_CASE("JsonArray::copyFrom()") {
|
||||
SECTION("OneDimension_JsonBufferTooSmall") {
|
||||
const size_t SIZE = JSON_ARRAY_SIZE(2);
|
||||
StaticJsonDocument<SIZE> doc;
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
char json[32];
|
||||
int source[] = {1, 2, 3};
|
||||
|
||||
@ -35,7 +35,7 @@ TEST_CASE("JsonArray::copyFrom()") {
|
||||
|
||||
SECTION("TwoDimensions") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
char json[32];
|
||||
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
||||
|
||||
@ -50,7 +50,7 @@ TEST_CASE("JsonArray::copyFrom()") {
|
||||
const size_t SIZE =
|
||||
JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
|
||||
StaticJsonDocument<SIZE> doc;
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
char json[32];
|
||||
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
||||
|
||||
|
@ -12,7 +12,7 @@ TEST_CASE("JsonArray::copyTo()") {
|
||||
char json[] = "[1,2,3]";
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
|
||||
int destination[4] = {0};
|
||||
size_t result = array.copyTo(destination);
|
||||
@ -28,7 +28,7 @@ TEST_CASE("JsonArray::copyTo()") {
|
||||
char json[] = "[1,2,3]";
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
|
||||
int destination[2] = {0};
|
||||
size_t result = array.copyTo(destination);
|
||||
@ -43,7 +43,7 @@ TEST_CASE("JsonArray::copyTo()") {
|
||||
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
|
||||
int destination[3][2] = {{0}};
|
||||
array.copyTo(destination);
|
||||
|
@ -7,28 +7,29 @@
|
||||
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("JsonArray::invalid()") {
|
||||
TEST_CASE("Undefined JsonArray") {
|
||||
JsonArray array;
|
||||
|
||||
SECTION("SubscriptFails") {
|
||||
REQUIRE_FALSE(JsonArray::invalid()[0].success());
|
||||
REQUIRE(array[0].isNull());
|
||||
}
|
||||
|
||||
SECTION("AddFails") {
|
||||
JsonArray& array = JsonArray::invalid();
|
||||
array.add(1);
|
||||
REQUIRE(0 == array.size());
|
||||
}
|
||||
|
||||
SECTION("CreateNestedArrayFails") {
|
||||
REQUIRE_FALSE(JsonArray::invalid().createNestedArray().success());
|
||||
REQUIRE(array.createNestedArray().isNull());
|
||||
}
|
||||
|
||||
SECTION("CreateNestedObjectFails") {
|
||||
REQUIRE_FALSE(JsonArray::invalid().createNestedObject().success());
|
||||
REQUIRE(array.createNestedObject().isNull());
|
||||
}
|
||||
|
||||
SECTION("PrintToWritesBrackets") {
|
||||
char buffer[32];
|
||||
serializeJson(JsonArray::invalid(), buffer, sizeof(buffer));
|
||||
REQUIRE_THAT(buffer, Equals("[]"));
|
||||
serializeJson(array, buffer, sizeof(buffer));
|
||||
REQUIRE_THAT(buffer, Equals("null"));
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
template <typename TIterator>
|
||||
static void run_iterator_test() {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(2)> doc;
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
array.add(12);
|
||||
array.add(34);
|
||||
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
TEST_CASE("JsonArray::remove()") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& _array = doc.to<JsonArray>();
|
||||
JsonArray _array = doc.to<JsonArray>();
|
||||
_array.add(1);
|
||||
_array.add(2);
|
||||
_array.add(3);
|
||||
|
@ -9,7 +9,7 @@ using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("JsonArray::set()") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& _array = doc.to<JsonArray>();
|
||||
JsonArray _array = doc.to<JsonArray>();
|
||||
_array.add(0);
|
||||
|
||||
SECTION("int") {
|
||||
@ -42,29 +42,29 @@ TEST_CASE("JsonArray::set()") {
|
||||
|
||||
SECTION("nested array") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonArray& arr = doc2.to<JsonArray>();
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
|
||||
_array.set(0, arr);
|
||||
|
||||
REQUIRE(&arr == &_array[0].as<JsonArray&>());
|
||||
REQUIRE(_array[0].is<JsonArray&>());
|
||||
REQUIRE(arr == _array[0].as<JsonArray>());
|
||||
REQUIRE(_array[0].is<JsonArray>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonObject& obj = doc2.to<JsonObject>();
|
||||
JsonObject obj = doc2.to<JsonObject>();
|
||||
|
||||
_array.set(0, obj);
|
||||
|
||||
REQUIRE(&obj == &_array[0].as<JsonObject&>());
|
||||
REQUIRE(_array[0].is<JsonObject&>());
|
||||
REQUIRE(obj == _array[0].as<JsonObject>());
|
||||
REQUIRE(_array[0].is<JsonObject>());
|
||||
REQUIRE_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonArray& arr = doc2.to<JsonArray>();
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
|
||||
_array.set(0, arr[0]);
|
||||
@ -74,7 +74,7 @@ TEST_CASE("JsonArray::set()") {
|
||||
|
||||
SECTION("object subscript") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonObject& obj = doc2.to<JsonObject>();
|
||||
JsonObject obj = doc2.to<JsonObject>();
|
||||
obj["x"] = "hello";
|
||||
|
||||
_array.set(0, obj["x"]);
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
TEST_CASE("JsonArray::size()") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& _array = doc.to<JsonArray>();
|
||||
JsonArray _array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("increases after add()") {
|
||||
_array.add("hello");
|
||||
|
@ -12,7 +12,7 @@ static void eraseString(std::string &str) {
|
||||
|
||||
TEST_CASE("std::string") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray &array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("add()") {
|
||||
std::string value("hello");
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
TEST_CASE("JsonArray::operator[]") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& _array = doc.to<JsonArray>();
|
||||
JsonArray _array = doc.to<JsonArray>();
|
||||
_array.add(0);
|
||||
|
||||
SECTION("int") {
|
||||
@ -53,35 +53,33 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
|
||||
SECTION("nested array") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonArray& arr = doc2.to<JsonArray>();
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
|
||||
_array[0] = arr;
|
||||
|
||||
REQUIRE(&arr == &_array[0].as<JsonArray&>());
|
||||
REQUIRE(&arr == &_array[0].as<JsonArray>()); // <- short hand
|
||||
REQUIRE(&arr == &_array[0].as<const JsonArray&>());
|
||||
REQUIRE(&arr == &_array[0].as<const JsonArray>()); // <- short hand
|
||||
REQUIRE(true == _array[0].is<JsonArray&>());
|
||||
REQUIRE(arr == _array[0].as<JsonArray>());
|
||||
REQUIRE(arr == _array[0].as<JsonArray>()); // <- short hand
|
||||
// REQUIRE(arr == _array[0].as<const JsonArray>());
|
||||
// REQUIRE(arr == _array[0].as<const JsonArray>()); // <- short hand
|
||||
REQUIRE(true == _array[0].is<JsonArray>());
|
||||
REQUIRE(false == _array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonObject& obj = doc2.to<JsonObject>();
|
||||
JsonObject obj = doc2.to<JsonObject>();
|
||||
|
||||
_array[0] = obj;
|
||||
|
||||
REQUIRE(&obj == &_array[0].as<JsonObject&>());
|
||||
REQUIRE(&obj == &_array[0].as<JsonObject>()); // <- short hand
|
||||
REQUIRE(&obj == &_array[0].as<const JsonObject&>());
|
||||
REQUIRE(&obj == &_array[0].as<const JsonObject>()); // <- short hand
|
||||
REQUIRE(true == _array[0].is<JsonObject&>());
|
||||
REQUIRE(obj == _array[0].as<JsonObject>());
|
||||
REQUIRE(obj == _array[0].as<const JsonObject>()); // <- short hand
|
||||
REQUIRE(true == _array[0].is<JsonObject>());
|
||||
REQUIRE(false == _array[0].is<int>());
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonArray& arr = doc2.to<JsonArray>();
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
const char* str = "hello";
|
||||
|
||||
arr.add(str);
|
||||
@ -94,7 +92,7 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
SECTION("object subscript") {
|
||||
const char* str = "hello";
|
||||
DynamicJsonDocument doc2;
|
||||
JsonObject& obj = doc2.to<JsonObject>();
|
||||
JsonObject obj = doc2.to<JsonObject>();
|
||||
|
||||
obj["x"] = str;
|
||||
|
||||
|
@ -10,7 +10,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("An empty array") {
|
||||
DeserializationError err = deserializeJson(doc, "[]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(0 == arr.size());
|
||||
@ -19,7 +19,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
SECTION("Spaces") {
|
||||
SECTION("Before the opening bracket") {
|
||||
DeserializationError err = deserializeJson(doc, " []");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(0 == arr.size());
|
||||
@ -27,7 +27,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("Before first value") {
|
||||
DeserializationError err = deserializeJson(doc, "[ \t\r\n42]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == arr.size());
|
||||
@ -36,7 +36,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("After first value") {
|
||||
DeserializationError err = deserializeJson(doc, "[42 \t\r\n]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == arr.size());
|
||||
@ -47,7 +47,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
SECTION("Values types") {
|
||||
SECTION("On integer") {
|
||||
DeserializationError err = deserializeJson(doc, "[42]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == arr.size());
|
||||
@ -56,7 +56,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("Two integers") {
|
||||
DeserializationError err = deserializeJson(doc, "[42,84]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(2 == arr.size());
|
||||
@ -66,7 +66,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("Double") {
|
||||
DeserializationError err = deserializeJson(doc, "[4.2,1e2]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(2 == arr.size());
|
||||
@ -76,7 +76,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("Unsigned long") {
|
||||
DeserializationError err = deserializeJson(doc, "[4294967295]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == arr.size());
|
||||
@ -85,7 +85,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("Boolean") {
|
||||
DeserializationError err = deserializeJson(doc, "[true,false]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(2 == arr.size());
|
||||
@ -95,7 +95,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("Null") {
|
||||
DeserializationError err = deserializeJson(doc, "[null,null]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(2 == arr.size());
|
||||
@ -108,7 +108,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
SECTION("Double quotes") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "[ \"hello\" , \"world\" ]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(2 == arr.size());
|
||||
@ -118,7 +118,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("Single quotes") {
|
||||
DeserializationError err = deserializeJson(doc, "[ 'hello' , 'world' ]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(2 == arr.size());
|
||||
@ -128,7 +128,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("No quotes") {
|
||||
DeserializationError err = deserializeJson(doc, "[ hello , world ]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(2 == arr.size());
|
||||
@ -138,7 +138,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("Double quotes (empty strings)") {
|
||||
DeserializationError err = deserializeJson(doc, "[\"\",\"\"]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(2 == arr.size());
|
||||
@ -148,7 +148,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("Single quotes (empty strings)") {
|
||||
DeserializationError err = deserializeJson(doc, "[\'\',\'\']");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(2 == arr.size());
|
||||
@ -179,7 +179,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
SECTION("Before opening bracket") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "/*COMMENT*/ [\"hello\"]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == arr.size());
|
||||
@ -189,7 +189,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
SECTION("After opening bracket") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "[/*COMMENT*/ \"hello\"]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == arr.size());
|
||||
@ -198,7 +198,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("Before closing bracket") {
|
||||
DeserializationError err = deserializeJson(doc, "[\"hello\"/*COMMENT*/]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == arr.size());
|
||||
@ -207,7 +207,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("After closing bracket") {
|
||||
DeserializationError err = deserializeJson(doc, "[\"hello\"]/*COMMENT*/");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == arr.size());
|
||||
@ -217,7 +217,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
SECTION("Before comma") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "[\"hello\"/*COMMENT*/,\"world\"]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(2 == arr.size());
|
||||
@ -228,7 +228,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
SECTION("After comma") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "[\"hello\",/*COMMENT*/ \"world\"]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(2 == arr.size());
|
||||
@ -256,7 +256,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
SECTION("Before opening bracket") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "//COMMENT\n\t[\"hello\"]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == arr.size());
|
||||
@ -265,7 +265,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("After opening bracket") {
|
||||
DeserializationError err = deserializeJson(doc, "[//COMMENT\n\"hello\"]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == arr.size());
|
||||
@ -275,7 +275,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
SECTION("Before closing bracket") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "[\"hello\"//COMMENT\r\n]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == arr.size());
|
||||
@ -284,7 +284,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
|
||||
SECTION("After closing bracket") {
|
||||
DeserializationError err = deserializeJson(doc, "[\"hello\"]//COMMENT\n");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == arr.size());
|
||||
@ -294,7 +294,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
SECTION("Before comma") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "[\"hello\"//COMMENT\n,\"world\"]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(2 == arr.size());
|
||||
@ -305,7 +305,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
SECTION("After comma") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "[\"hello\",//COMMENT\n\"world\"]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(2 == arr.size());
|
||||
@ -372,17 +372,17 @@ TEST_CASE("deserialize JSON array") {
|
||||
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, jsonString);
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
JsonObject& object1 = arr[0];
|
||||
const JsonObject& object2 = arr[1];
|
||||
JsonObject& object3 = arr[2];
|
||||
JsonObject object1 = arr[0];
|
||||
const JsonObject object2 = arr[1];
|
||||
JsonObject object3 = arr[2];
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
|
||||
REQUIRE(true == object1.success());
|
||||
REQUIRE(true == object2.success());
|
||||
REQUIRE(false == object3.success());
|
||||
REQUIRE(object1.isNull() == false);
|
||||
REQUIRE(object2.isNull() == false);
|
||||
REQUIRE(object3.isNull() == true);
|
||||
|
||||
REQUIRE(2 == object1.size());
|
||||
REQUIRE(2 == object2.size());
|
||||
@ -399,7 +399,7 @@ TEST_CASE("deserialize JSON array") {
|
||||
SECTION("Should clear the JsonArray") {
|
||||
deserializeJson(doc, "[1,2,3,4]");
|
||||
deserializeJson(doc, "[]");
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(arr.size() == 0);
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
|
||||
|
@ -68,7 +68,7 @@ TEST_CASE("deserialize JSON array with a StaticJsonDocument") {
|
||||
deserializeJson(doc, input);
|
||||
deserializeJson(doc, "[]");
|
||||
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
REQUIRE(arr.size() == 0);
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
|
||||
}
|
||||
@ -78,7 +78,7 @@ TEST_CASE("deserialize JSON array with a StaticJsonDocument") {
|
||||
char input[] = "[1,2]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonArray>());
|
||||
|
@ -10,7 +10,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
|
||||
SECTION("An empty object") {
|
||||
DeserializationError err = deserializeJson(doc, "{}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -20,7 +20,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Quotes") {
|
||||
SECTION("Double quotes") {
|
||||
DeserializationError err = deserializeJson(doc, "{\"key\":\"value\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -30,7 +30,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
|
||||
SECTION("Single quotes") {
|
||||
DeserializationError err = deserializeJson(doc, "{'key':'value'}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -40,7 +40,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
|
||||
SECTION("No quotes") {
|
||||
DeserializationError err = deserializeJson(doc, "{key:value}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -50,7 +50,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
|
||||
SECTION("No quotes, allow underscore in key") {
|
||||
DeserializationError err = deserializeJson(doc, "{_k_e_y_:42}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -62,7 +62,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Spaces") {
|
||||
SECTION("Before the key") {
|
||||
DeserializationError err = deserializeJson(doc, "{ \"key\":\"value\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -72,7 +72,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
|
||||
SECTION("After the key") {
|
||||
DeserializationError err = deserializeJson(doc, "{\"key\" :\"value\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -82,7 +82,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
|
||||
SECTION("Before the value") {
|
||||
DeserializationError err = deserializeJson(doc, "{\"key\": \"value\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -92,7 +92,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
|
||||
SECTION("After the value") {
|
||||
DeserializationError err = deserializeJson(doc, "{\"key\":\"value\" }");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -103,7 +103,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Before the colon") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -115,7 +115,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("After the colon") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -129,7 +129,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("String") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -141,7 +141,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Integer") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"key1\":42,\"key2\":-42}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -153,7 +153,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Double") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"key1\":12.345,\"key2\":-7E89}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -165,7 +165,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Booleans") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"key1\":true,\"key2\":false}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -177,7 +177,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Null") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"key1\":null,\"key2\":null}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -190,17 +190,17 @@ TEST_CASE("deserialize JSON object") {
|
||||
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, jsonString);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
JsonArray& array1 = obj["ab"];
|
||||
const JsonArray& array2 = obj["cd"];
|
||||
JsonArray& array3 = obj["ef"];
|
||||
JsonArray array1 = obj["ab"];
|
||||
const JsonArray array2 = obj["cd"];
|
||||
JsonArray array3 = obj["ef"];
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
|
||||
REQUIRE(true == array1.success());
|
||||
REQUIRE(true == array2.success());
|
||||
REQUIRE(false == array3.success());
|
||||
REQUIRE(array1.isNull() == false);
|
||||
REQUIRE(array2.isNull() == false);
|
||||
REQUIRE(array3.isNull() == true);
|
||||
|
||||
REQUIRE(2 == array1.size());
|
||||
REQUIRE(2 == array2.size());
|
||||
@ -278,7 +278,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Before opening brace") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "/*COMMENT*/ {\"hello\":\"world\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -287,7 +287,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("After opening brace") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{/*COMMENT*/\"hello\":\"world\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -296,7 +296,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Before colon") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"hello\"/*COMMENT*/:\"world\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -305,7 +305,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("After colon") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"hello\":/*COMMENT*/\"world\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -314,7 +314,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Before closing brace") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"hello\":\"world\"/*COMMENT*/}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -323,7 +323,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("After closing brace") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}/*COMMENT*/");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -332,7 +332,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Before comma") {
|
||||
DeserializationError err = deserializeJson(
|
||||
doc, "{\"hello\":\"world\"/*COMMENT*/,\"answer\":42}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -342,7 +342,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("After comma") {
|
||||
DeserializationError err = deserializeJson(
|
||||
doc, "{\"hello\":\"world\",/*COMMENT*/\"answer\":42}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -354,7 +354,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Before opening brace") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "//COMMENT\n {\"hello\":\"world\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -363,7 +363,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("After opening brace") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{//COMMENT\n\"hello\":\"world\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -372,7 +372,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Before colon") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"hello\"//COMMENT\n:\"world\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -381,7 +381,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("After colon") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"hello\"://COMMENT\n\"world\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -390,7 +390,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Before closing brace") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"hello\":\"world\"//COMMENT\n}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -399,7 +399,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("After closing brace") {
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}//COMMENT\n");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -408,7 +408,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Before comma") {
|
||||
DeserializationError err = deserializeJson(
|
||||
doc, "{\"hello\":\"world\"//COMMENT\n,\"answer\":42}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -418,7 +418,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("After comma") {
|
||||
DeserializationError err = deserializeJson(
|
||||
doc, "{\"hello\":\"world\",//COMMENT\n\"answer\":42}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -459,7 +459,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
|
||||
SECTION("After closing brace") {
|
||||
DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"}/");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(obj["hello"] == "world");
|
||||
@ -483,7 +483,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
SECTION("Should clear the JsonObject") {
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
deserializeJson(doc, "{}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 0);
|
||||
|
@ -13,7 +13,7 @@ TEST_CASE("deserializeJson(std::istream&)") {
|
||||
std::istringstream json(" [ 42 /* comment */ ] ");
|
||||
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == arr.size());
|
||||
@ -24,7 +24,7 @@ TEST_CASE("deserializeJson(std::istream&)") {
|
||||
std::istringstream json(" { hello : world // comment\n }");
|
||||
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(1 == obj.size());
|
||||
|
@ -28,7 +28,7 @@ TEST_CASE("deserializeJson(const std::string&)") {
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
input[2] = 'X'; // alter the string tomake sure we made a copy
|
||||
|
||||
JsonArray &array = doc.as<JsonArray>();
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
}
|
||||
|
@ -7,9 +7,9 @@
|
||||
|
||||
TEST_CASE("JsonObject basics") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("SuccessIsTrue") {
|
||||
REQUIRE(obj.success());
|
||||
SECTION("isNull()") {
|
||||
REQUIRE(obj.isNull() == false);
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
TEST_CASE("JsonObject::containsKey()") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("ContainsKeyReturnsFalseForNonExistingKey") {
|
||||
obj.set("hello", 42);
|
||||
|
@ -9,7 +9,7 @@ using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("JsonObject::get()") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("GetConstCharPointer_GivenStringLiteral") {
|
||||
obj.set("hello", "world");
|
||||
|
@ -8,10 +8,10 @@
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("JsonObject::invalid()") {
|
||||
JsonObject& obj = JsonObject::invalid();
|
||||
JsonObject obj;
|
||||
|
||||
SECTION("SubscriptFails") {
|
||||
REQUIRE_FALSE(obj["key"].success());
|
||||
REQUIRE(obj["key"].isNull());
|
||||
}
|
||||
|
||||
SECTION("AddFails") {
|
||||
@ -20,16 +20,16 @@ TEST_CASE("JsonObject::invalid()") {
|
||||
}
|
||||
|
||||
SECTION("CreateNestedArrayFails") {
|
||||
REQUIRE_FALSE(obj.createNestedArray("hello").success());
|
||||
REQUIRE(obj.createNestedArray("hello").isNull());
|
||||
}
|
||||
|
||||
SECTION("CreateNestedObjectFails") {
|
||||
REQUIRE_FALSE(obj.createNestedObject("world").success());
|
||||
REQUIRE(obj.createNestedObject("world").isNull());
|
||||
}
|
||||
|
||||
SECTION("PrintToWritesBraces") {
|
||||
SECTION("serialize to 'null'") {
|
||||
char buffer[32];
|
||||
serializeJson(obj, buffer, sizeof(buffer));
|
||||
REQUIRE_THAT(buffer, Equals("{}"));
|
||||
REQUIRE_THAT(buffer, Equals("null"));
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("JsonObject::begin()/end()") {
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["ab"] = 12;
|
||||
obj["cd"] = 34;
|
||||
|
||||
@ -35,7 +35,7 @@ TEST_CASE("JsonObject::begin()/end()") {
|
||||
}
|
||||
|
||||
SECTION("ConstIterator") {
|
||||
const JsonObject& const_object = obj;
|
||||
const JsonObject const_object = obj;
|
||||
JsonObject::const_iterator it = const_object.begin();
|
||||
|
||||
REQUIRE(const_object.end() != it);
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
TEST_CASE("JsonObject::remove()") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("SizeDecreased_WhenValuesAreRemoved") {
|
||||
obj["hello"] = 1;
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
TEST_CASE("JsonObject::set()") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("int") {
|
||||
obj.set("hello", 123);
|
||||
@ -44,29 +44,29 @@ TEST_CASE("JsonObject::set()") {
|
||||
|
||||
SECTION("nested array") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonArray& arr = doc2.to<JsonArray>();
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
|
||||
obj.set("hello", arr);
|
||||
|
||||
REQUIRE(&arr == &obj["hello"].as<JsonArray>());
|
||||
REQUIRE(obj["hello"].is<JsonArray&>());
|
||||
REQUIRE_FALSE(obj["hello"].is<JsonObject&>());
|
||||
REQUIRE(arr == obj["hello"].as<JsonArray>());
|
||||
REQUIRE(obj["hello"].is<JsonArray>());
|
||||
REQUIRE_FALSE(obj["hello"].is<JsonObject>());
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonObject& obj2 = doc2.to<JsonObject>();
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
|
||||
obj.set("hello", obj2);
|
||||
|
||||
REQUIRE(&obj2 == &obj["hello"].as<JsonObject>());
|
||||
REQUIRE(obj["hello"].is<JsonObject&>());
|
||||
REQUIRE_FALSE(obj["hello"].is<JsonArray&>());
|
||||
REQUIRE(obj2 == obj["hello"].as<JsonObject>());
|
||||
REQUIRE(obj["hello"].is<JsonObject>());
|
||||
REQUIRE_FALSE(obj["hello"].is<JsonArray>());
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonArray& arr = doc2.to<JsonArray>();
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
arr.add(42);
|
||||
|
||||
obj.set("a", arr[0]);
|
||||
@ -76,7 +76,7 @@ TEST_CASE("JsonObject::set()") {
|
||||
|
||||
SECTION("object subscript") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonObject& obj2 = doc2.to<JsonObject>();
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
obj2.set("x", 42);
|
||||
|
||||
obj.set("a", obj2["x"]);
|
||||
@ -86,14 +86,14 @@ TEST_CASE("JsonObject::set()") {
|
||||
|
||||
SECTION("returns true when allocation succeeds") {
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(1) + 15> doc2;
|
||||
JsonObject& obj2 = doc2.to<JsonObject>();
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
|
||||
REQUIRE(true == obj2.set(std::string("hello"), std::string("world")));
|
||||
}
|
||||
|
||||
SECTION("returns false when allocation fails") {
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(1) + 10> doc2;
|
||||
JsonObject& obj2 = doc2.to<JsonObject>();
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
|
||||
REQUIRE(false == obj2.set(std::string("hello"), std::string("world")));
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
TEST_CASE("JsonObject::size()") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("initial size is zero") {
|
||||
REQUIRE(0 == obj.size());
|
||||
|
@ -17,7 +17,7 @@ TEST_CASE("std::string") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
|
||||
deserializeJson(doc, json);
|
||||
JsonObject &obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(std::string("value") == obj[std::string("key")]);
|
||||
}
|
||||
@ -26,13 +26,13 @@ TEST_CASE("std::string") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
|
||||
deserializeJson(doc, json);
|
||||
JsonObject &obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(std::string("value") == obj[std::string("key")]);
|
||||
}
|
||||
|
||||
SECTION("set(key)") {
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
std::string key("hello");
|
||||
obj.set(key, "world");
|
||||
eraseString(key);
|
||||
@ -40,7 +40,7 @@ TEST_CASE("std::string") {
|
||||
}
|
||||
|
||||
SECTION("set(value)") {
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
std::string value("world");
|
||||
obj.set("hello", value);
|
||||
eraseString(value);
|
||||
@ -48,7 +48,7 @@ TEST_CASE("std::string") {
|
||||
}
|
||||
|
||||
SECTION("set(key,value)") {
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
std::string key("hello");
|
||||
std::string value("world");
|
||||
obj.set(key, value);
|
||||
@ -58,9 +58,9 @@ TEST_CASE("std::string") {
|
||||
}
|
||||
|
||||
SECTION("set(JsonArraySubscript)") {
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
DynamicJsonDocument doc2;
|
||||
JsonArray &arr = doc2.to<JsonArray>();
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
arr.add("world");
|
||||
|
||||
obj.set(std::string("hello"), arr[0]);
|
||||
@ -69,9 +69,9 @@ TEST_CASE("std::string") {
|
||||
}
|
||||
|
||||
SECTION("set(JsonObjectSubscript)") {
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
DynamicJsonDocument doc2;
|
||||
JsonObject &obj2 = doc2.to<JsonObject>();
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
obj2.set("x", "world");
|
||||
|
||||
obj.set(std::string("hello"), obj2["x"]);
|
||||
@ -82,7 +82,7 @@ TEST_CASE("std::string") {
|
||||
SECTION("get<T>()") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
deserializeJson(doc, json);
|
||||
JsonObject &obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(std::string("value") == obj.get<const char *>(std::string("key")));
|
||||
}
|
||||
@ -90,13 +90,13 @@ TEST_CASE("std::string") {
|
||||
SECTION("is<T>()") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
deserializeJson(doc, json);
|
||||
JsonObject &obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(true == obj.is<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
SECTION("createNestedObject()") {
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
obj.createNestedObject(key);
|
||||
@ -106,7 +106,7 @@ TEST_CASE("std::string") {
|
||||
}
|
||||
|
||||
SECTION("createNestedArray()") {
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
obj.createNestedArray(key);
|
||||
@ -118,12 +118,12 @@ TEST_CASE("std::string") {
|
||||
SECTION("containsKey()") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
deserializeJson(doc, json);
|
||||
JsonObject &obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
REQUIRE(true == obj.containsKey(std::string("key")));
|
||||
}
|
||||
|
||||
SECTION("remove()") {
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["key"] = "value";
|
||||
|
||||
obj.remove(std::string("key"));
|
||||
@ -133,7 +133,7 @@ TEST_CASE("std::string") {
|
||||
|
||||
SECTION("operator[], set key") {
|
||||
std::string key("hello");
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj[key] = "world";
|
||||
eraseString(key);
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -141,7 +141,7 @@ TEST_CASE("std::string") {
|
||||
|
||||
SECTION("operator[], set value") {
|
||||
std::string value("world");
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"] = value;
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -149,7 +149,7 @@ TEST_CASE("std::string") {
|
||||
|
||||
SECTION("memoryUsage() increases when adding a new key") {
|
||||
std::string key1("hello"), key2("world");
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
obj[key1] = 1;
|
||||
size_t sizeBefore = doc.memoryUsage();
|
||||
@ -161,7 +161,7 @@ TEST_CASE("std::string") {
|
||||
|
||||
SECTION("memoryUsage() remains when adding the same key") {
|
||||
std::string key("hello");
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
obj[key] = 1;
|
||||
size_t sizeBefore = doc.memoryUsage();
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
TEST_CASE("JsonObject::operator[]") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("int") {
|
||||
obj["hello"] = 123;
|
||||
@ -53,41 +53,37 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
|
||||
SECTION("array") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonArray& arr = doc2.to<JsonArray>();
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
|
||||
obj["hello"] = arr;
|
||||
|
||||
REQUIRE(&arr == &obj["hello"].as<JsonArray&>());
|
||||
REQUIRE(&arr == &obj["hello"].as<JsonArray>()); // <- short hand
|
||||
REQUIRE(&arr == &obj["hello"].as<const JsonArray&>());
|
||||
REQUIRE(&arr == &obj["hello"].as<const JsonArray>()); // <- short hand
|
||||
REQUIRE(true == obj["hello"].is<JsonArray&>());
|
||||
REQUIRE(arr == obj["hello"].as<JsonArray>());
|
||||
REQUIRE(arr == obj["hello"].as<JsonArray>()); // <- short hand
|
||||
// REQUIRE(arr == obj["hello"].as<const JsonArray>());
|
||||
// REQUIRE(arr == obj["hello"].as<const JsonArray>()); // <- short hand
|
||||
REQUIRE(true == obj["hello"].is<JsonArray>());
|
||||
REQUIRE(true == obj["hello"].is<JsonArray>());
|
||||
REQUIRE(true == obj["hello"].is<const JsonArray&>());
|
||||
REQUIRE(true == obj["hello"].is<const JsonArray>());
|
||||
REQUIRE(false == obj["hello"].is<JsonObject&>());
|
||||
REQUIRE(true == obj["hello"].is<const JsonArray>());
|
||||
REQUIRE(false == obj["hello"].is<JsonObject>());
|
||||
}
|
||||
|
||||
SECTION("object") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonObject& obj2 = doc2.to<JsonObject>();
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
|
||||
obj["hello"] = obj2;
|
||||
|
||||
REQUIRE(&obj2 == &obj["hello"].as<JsonObject&>());
|
||||
REQUIRE(&obj2 == &obj["hello"].as<JsonObject>()); // <- short hand
|
||||
REQUIRE(&obj2 == &obj["hello"].as<const JsonObject&>());
|
||||
REQUIRE(&obj2 == &obj["hello"].as<const JsonObject>()); // <- short hand
|
||||
REQUIRE(true == obj["hello"].is<JsonObject&>());
|
||||
REQUIRE(obj2 == obj["hello"].as<JsonObject>());
|
||||
REQUIRE(obj2 == obj["hello"].as<const JsonObject>());
|
||||
REQUIRE(true == obj["hello"].is<JsonObject>());
|
||||
REQUIRE(true == obj["hello"].is<const JsonObject&>());
|
||||
REQUIRE(true == obj["hello"].is<const JsonObject>());
|
||||
REQUIRE(false == obj["hello"].is<JsonArray&>());
|
||||
REQUIRE(false == obj["hello"].is<JsonArray>());
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonArray& arr = doc2.to<JsonArray>();
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
arr.add(42);
|
||||
|
||||
obj["a"] = arr[0];
|
||||
@ -97,7 +93,7 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
|
||||
SECTION("object subscript") {
|
||||
DynamicJsonDocument doc2;
|
||||
JsonObject& obj2 = doc2.to<JsonObject>();
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
obj2.set("x", 42);
|
||||
|
||||
obj["a"] = obj2["x"];
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void check(JsonArray &array, std::string expected) {
|
||||
static void check(JsonArray array, std::string expected) {
|
||||
std::string actual;
|
||||
size_t actualLen = serializeJson(array, actual);
|
||||
REQUIRE(expected == actual);
|
||||
@ -16,7 +16,7 @@ static void check(JsonArray &array, std::string expected) {
|
||||
|
||||
TEST_CASE("serializeJson(JsonArray)") {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(2)> doc;
|
||||
JsonArray &array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("Empty") {
|
||||
check(array, "[]");
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void check(JsonArray& array, std::string expected) {
|
||||
static void check(JsonArray array, std::string expected) {
|
||||
std::string actual;
|
||||
size_t actualLen = serializeJsonPretty(array, actual);
|
||||
size_t measuredLen = measureJsonPretty(array);
|
||||
@ -16,7 +16,7 @@ static void check(JsonArray& array, std::string expected) {
|
||||
|
||||
TEST_CASE("serializeJsonPretty(JsonArray)") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("Empty") {
|
||||
check(array, "[]");
|
||||
@ -54,11 +54,11 @@ TEST_CASE("serializeJsonPretty(JsonArray)") {
|
||||
}
|
||||
|
||||
SECTION("NestedArrays") {
|
||||
JsonArray& nested1 = array.createNestedArray();
|
||||
JsonArray nested1 = array.createNestedArray();
|
||||
nested1.add(1);
|
||||
nested1.add(2);
|
||||
|
||||
JsonObject& nested2 = array.createNestedObject();
|
||||
JsonObject nested2 = array.createNestedObject();
|
||||
nested2["key"] = 3;
|
||||
|
||||
check(array,
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
#include <string>
|
||||
|
||||
void check(const JsonObject &obj, const std::string &expected) {
|
||||
void check(const JsonObject obj, const std::string &expected) {
|
||||
char actual[256];
|
||||
size_t actualLen = serializeJson(obj, actual);
|
||||
size_t measuredLen = measureJson(obj);
|
||||
@ -18,7 +18,7 @@ void check(const JsonObject &obj, const std::string &expected) {
|
||||
|
||||
TEST_CASE("serializeJson(JsonObject)") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("EmptyObject") {
|
||||
check(obj, "{}");
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
#include <string>
|
||||
|
||||
void check(const JsonObject &obj, const std::string expected) {
|
||||
void check(const JsonObject obj, const std::string expected) {
|
||||
char json[256];
|
||||
|
||||
size_t actualLen = serializeJsonPretty(obj, json);
|
||||
@ -19,7 +19,7 @@ void check(const JsonObject &obj, const std::string expected) {
|
||||
|
||||
TEST_CASE("serializeJsonPretty(JsonObject)") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
SECTION("EmptyObject") {
|
||||
check(obj, "{}");
|
||||
@ -57,10 +57,10 @@ TEST_CASE("serializeJsonPretty(JsonObject)") {
|
||||
}
|
||||
|
||||
SECTION("NestedContainers") {
|
||||
JsonObject &nested1 = obj.createNestedObject("key1");
|
||||
JsonObject nested1 = obj.createNestedObject("key1");
|
||||
nested1["a"] = 1;
|
||||
|
||||
JsonArray &nested2 = obj.createNestedArray("key2");
|
||||
JsonArray nested2 = obj.createNestedArray("key2");
|
||||
nested2.add(2);
|
||||
|
||||
check(obj,
|
||||
|
@ -14,11 +14,11 @@ void check(JsonVariant variant, const std::string &expected) {
|
||||
}
|
||||
|
||||
TEST_CASE("serializeJson(JsonVariant)") {
|
||||
SECTION("Empty") {
|
||||
check(JsonVariant(), "");
|
||||
SECTION("Undefined") {
|
||||
check(JsonVariant(), "null");
|
||||
}
|
||||
|
||||
SECTION("Null") {
|
||||
SECTION("Null string") {
|
||||
check(static_cast<char *>(0), "null");
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ TEST_CASE("operator<<(std::ostream)") {
|
||||
}
|
||||
|
||||
SECTION("JsonObject") {
|
||||
JsonObject& object = doc.to<JsonObject>();
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["key"] = "value";
|
||||
|
||||
os << object;
|
||||
@ -36,7 +36,7 @@ TEST_CASE("operator<<(std::ostream)") {
|
||||
}
|
||||
|
||||
SECTION("JsonObjectSubscript") {
|
||||
JsonObject& object = doc.to<JsonObject>();
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["key"] = "value";
|
||||
|
||||
os << object["key"];
|
||||
@ -45,7 +45,7 @@ TEST_CASE("operator<<(std::ostream)") {
|
||||
}
|
||||
|
||||
SECTION("JsonArray") {
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
array.add("value");
|
||||
|
||||
os << array;
|
||||
@ -54,7 +54,7 @@ TEST_CASE("operator<<(std::ostream)") {
|
||||
}
|
||||
|
||||
SECTION("JsonArraySubscript") {
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
array.add("value");
|
||||
|
||||
os << array[0];
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
TEST_CASE("serialize JsonArray to std::string") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray &array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
|
||||
@ -28,7 +28,7 @@ TEST_CASE("serialize JsonArray to std::string") {
|
||||
|
||||
TEST_CASE("serialize JsonObject to std::string") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject &obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["key"] = "value";
|
||||
|
||||
SECTION("object") {
|
||||
|
@ -7,10 +7,10 @@ add_executable(JsonVariantTests
|
||||
compare.cpp
|
||||
copy.cpp
|
||||
is.cpp
|
||||
isnull.cpp
|
||||
or.cpp
|
||||
set_get.cpp
|
||||
subscript.cpp
|
||||
success.cpp
|
||||
undefined.cpp
|
||||
)
|
||||
|
||||
|
@ -193,7 +193,7 @@ TEST_CASE("JsonVariant::as()") {
|
||||
|
||||
SECTION("ObjectAsString") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
obj["key"] = "value";
|
||||
|
||||
@ -203,7 +203,7 @@ TEST_CASE("JsonVariant::as()") {
|
||||
|
||||
SECTION("ArrayAsString") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& arr = doc.to<JsonArray>();
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add(4);
|
||||
arr.add(2);
|
||||
|
||||
@ -213,19 +213,19 @@ TEST_CASE("JsonVariant::as()") {
|
||||
|
||||
SECTION("ArrayAsJsonArray") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& arr = doc.to<JsonArray>();
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
|
||||
JsonVariant variant = arr;
|
||||
REQUIRE(&arr == &variant.as<JsonArray&>());
|
||||
REQUIRE(&arr == &variant.as<JsonArray>()); // <- shorthand
|
||||
REQUIRE(arr == variant.as<JsonArray>());
|
||||
REQUIRE(arr == variant.as<JsonArray>()); // <- shorthand
|
||||
}
|
||||
|
||||
SECTION("ObjectAsJsonObject") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
JsonVariant variant = obj;
|
||||
REQUIRE(&obj == &variant.as<JsonObject&>());
|
||||
REQUIRE(&obj == &variant.as<JsonObject>()); // <- shorthand
|
||||
REQUIRE(obj == variant.as<JsonObject>());
|
||||
REQUIRE(obj == variant.as<JsonObject>()); // <- shorthand
|
||||
}
|
||||
}
|
||||
|
@ -213,9 +213,9 @@ TEST_CASE("JsonVariant comparisons") {
|
||||
|
||||
SECTION("ArrayInVariant") {
|
||||
DynamicJsonDocument doc1;
|
||||
JsonArray& array1 = doc1.to<JsonArray>();
|
||||
JsonArray array1 = doc1.to<JsonArray>();
|
||||
DynamicJsonDocument doc2;
|
||||
JsonArray& array2 = doc2.to<JsonArray>();
|
||||
JsonArray array2 = doc2.to<JsonArray>();
|
||||
|
||||
JsonVariant variant1 = array1;
|
||||
JsonVariant variant2 = array1;
|
||||
@ -230,9 +230,9 @@ TEST_CASE("JsonVariant comparisons") {
|
||||
|
||||
SECTION("ObjectInVariant") {
|
||||
DynamicJsonDocument doc1;
|
||||
JsonObject& obj1 = doc1.to<JsonObject>();
|
||||
JsonObject obj1 = doc1.to<JsonObject>();
|
||||
DynamicJsonDocument doc2;
|
||||
JsonObject& obj2 = doc2.to<JsonObject>();
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
|
||||
JsonVariant variant1 = obj1;
|
||||
JsonVariant variant2 = obj1;
|
||||
@ -247,10 +247,10 @@ TEST_CASE("JsonVariant comparisons") {
|
||||
|
||||
SECTION("VariantsOfDifferentTypes") {
|
||||
DynamicJsonDocument doc1;
|
||||
JsonObject& obj = doc1.to<JsonObject>();
|
||||
JsonObject obj = doc1.to<JsonObject>();
|
||||
|
||||
DynamicJsonDocument doc2;
|
||||
JsonArray& arr = doc2.to<JsonArray>();
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
JsonVariant variants[] = {
|
||||
true, 42, 666.667, "hello", arr, obj,
|
||||
};
|
||||
|
@ -43,7 +43,7 @@ TEST_CASE("JsonVariant copy") {
|
||||
|
||||
SECTION("ObjectsAreCopiedByReference") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& object = doc.to<JsonObject>();
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
|
||||
_variant1 = object;
|
||||
|
||||
@ -54,7 +54,7 @@ TEST_CASE("JsonVariant copy") {
|
||||
|
||||
SECTION("ArraysAreCopiedByReference") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
_variant1 = array;
|
||||
|
||||
|
@ -7,9 +7,9 @@
|
||||
|
||||
void checkIsArray(JsonVariant var) {
|
||||
REQUIRE(var.is<JsonArray>());
|
||||
REQUIRE(var.is<JsonArray&>());
|
||||
REQUIRE(var.is<JsonArray>());
|
||||
REQUIRE(var.is<const JsonArray>());
|
||||
REQUIRE(var.is<const JsonArray>());
|
||||
REQUIRE(var.is<const JsonArray&>());
|
||||
|
||||
REQUIRE_FALSE(var.is<bool>());
|
||||
REQUIRE_FALSE(var.is<double>());
|
||||
@ -71,7 +71,7 @@ void checkIsString(JsonVariant var) {
|
||||
TEST_CASE("JsonVariant::is()") {
|
||||
SECTION("JsonArray") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
checkIsArray(array);
|
||||
}
|
||||
|
||||
|
@ -5,40 +5,40 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonVariant::success()") {
|
||||
TEST_CASE("JsonVariant::isNull()") {
|
||||
SECTION("ReturnsFalse_WhenUndefined") {
|
||||
JsonVariant variant;
|
||||
REQUIRE(false == variant.success());
|
||||
REQUIRE(variant.isNull() == true);
|
||||
}
|
||||
|
||||
SECTION("ReturnsTrue_WhenInteger") {
|
||||
JsonVariant variant = 0;
|
||||
REQUIRE(true == variant.success());
|
||||
REQUIRE(variant.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("ReturnsTrue_WhenEmptyArray") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
JsonVariant variant = array;
|
||||
REQUIRE(true == variant.success());
|
||||
REQUIRE(variant.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("ReturnsTrue_WhenEmptyObject") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
JsonVariant variant = obj;
|
||||
REQUIRE(true == variant.success());
|
||||
REQUIRE(variant.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("ReturnsFalse_WhenInvalidArray") {
|
||||
JsonVariant variant = JsonArray::invalid();
|
||||
REQUIRE(false == variant.success());
|
||||
JsonVariant variant = JsonArray();
|
||||
REQUIRE(variant.isNull() == true);
|
||||
}
|
||||
|
||||
SECTION("ReturnsFalse_WhenInvalidObject") {
|
||||
JsonVariant variant = JsonObject::invalid();
|
||||
REQUIRE(false == variant.success());
|
||||
JsonVariant variant = JsonObject();
|
||||
REQUIRE(variant.isNull() == true);
|
||||
}
|
||||
}
|
@ -123,8 +123,8 @@ TEST_CASE("JsonVariant set()/get()") {
|
||||
|
||||
SECTION("CanStoreObject") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject &object = doc.to<JsonObject>();
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
|
||||
checkReference<JsonObject>(object);
|
||||
checkValue<JsonObject>(object);
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
TEST_CASE("JsonVariant::operator[]") {
|
||||
SECTION("Array") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
array.add("element at index 0");
|
||||
array.add("element at index 1");
|
||||
|
||||
@ -19,14 +19,14 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
REQUIRE(std::string("element at index 1") == var[1]);
|
||||
REQUIRE(std::string("element at index 0") ==
|
||||
var[static_cast<unsigned char>(0)]); // issue #381
|
||||
REQUIRE_FALSE(var[666].success());
|
||||
REQUIRE_FALSE(var[3].success());
|
||||
REQUIRE_FALSE(var["0"].success());
|
||||
REQUIRE(var[666].isNull());
|
||||
REQUIRE(var[3].isNull());
|
||||
REQUIRE(var["0"].isNull());
|
||||
}
|
||||
|
||||
SECTION("Object") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& object = doc.to<JsonObject>();
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["a"] = "element at key \"a\"";
|
||||
object["b"] = "element at key \"b\"";
|
||||
|
||||
@ -35,27 +35,27 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
REQUIRE(2 == var.size());
|
||||
REQUIRE(std::string("element at key \"a\"") == var["a"]);
|
||||
REQUIRE(std::string("element at key \"b\"") == var["b"]);
|
||||
REQUIRE_FALSE(var["c"].success());
|
||||
REQUIRE_FALSE(var[0].success());
|
||||
REQUIRE(var["c"].isNull());
|
||||
REQUIRE(var[0].isNull());
|
||||
}
|
||||
|
||||
SECTION("Undefined") {
|
||||
JsonVariant var = JsonVariant();
|
||||
REQUIRE(0 == var.size());
|
||||
REQUIRE_FALSE(var["0"].success());
|
||||
REQUIRE_FALSE(var[0].success());
|
||||
REQUIRE(var["0"].isNull());
|
||||
REQUIRE(var[0].isNull());
|
||||
}
|
||||
|
||||
SECTION("String") {
|
||||
JsonVariant var = "hello world";
|
||||
REQUIRE(0 == var.size());
|
||||
REQUIRE_FALSE(var["0"].success());
|
||||
REQUIRE_FALSE(var[0].success());
|
||||
REQUIRE(var["0"].isNull());
|
||||
REQUIRE(var[0].isNull());
|
||||
}
|
||||
|
||||
SECTION("ObjectSetValue") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
JsonVariant var = obj;
|
||||
var["hello"] = "world";
|
||||
REQUIRE(1 == var.size());
|
||||
@ -64,7 +64,7 @@ TEST_CASE("JsonVariant::operator[]") {
|
||||
|
||||
SECTION("ArraySetValue") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& arr = doc.to<JsonArray>();
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
JsonVariant var = arr;
|
||||
var[0] = "world";
|
||||
|
@ -29,26 +29,26 @@ TEST_CASE("JsonVariant undefined") {
|
||||
}
|
||||
|
||||
SECTION("AsArrayReturnInvalid") {
|
||||
REQUIRE(JsonArray::invalid() == variant.as<JsonArray&>());
|
||||
REQUIRE(JsonArray() == variant.as<JsonArray>());
|
||||
}
|
||||
|
||||
SECTION("AsConstArrayReturnInvalid") {
|
||||
REQUIRE(JsonArray::invalid() == variant.as<const JsonArray&>());
|
||||
REQUIRE(JsonArray() == variant.as<const JsonArray>());
|
||||
}
|
||||
|
||||
SECTION("AsObjectReturnInvalid") {
|
||||
REQUIRE(JsonObject::invalid() == variant.as<JsonObject&>());
|
||||
REQUIRE(JsonObject() == variant.as<JsonObject>());
|
||||
}
|
||||
|
||||
SECTION("AsConstObjectReturnInvalid") {
|
||||
REQUIRE(JsonObject::invalid() == variant.as<const JsonObject&>());
|
||||
REQUIRE(JsonObject() == variant.as<const JsonObject>());
|
||||
}
|
||||
|
||||
SECTION("AsArrayWrapperReturnInvalid") {
|
||||
REQUIRE(JsonArray::invalid() == variant.as<JsonArray>());
|
||||
REQUIRE(JsonArray() == variant.as<JsonArray>());
|
||||
}
|
||||
|
||||
SECTION("AsObjectWrapperReturnInvalid") {
|
||||
REQUIRE(JsonObject::invalid() == variant.as<JsonObject>());
|
||||
REQUIRE(JsonObject() == variant.as<JsonObject>());
|
||||
}
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ TEST_CASE("unsigned char[]") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj[key] = "world";
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -113,7 +113,7 @@ TEST_CASE("unsigned char[]") {
|
||||
DynamicJsonDocument doc;
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
REQUIRE(std::string("world") == obj[key]);
|
||||
}
|
||||
#endif
|
||||
@ -123,7 +123,7 @@ TEST_CASE("unsigned char[]") {
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
REQUIRE(std::string("world") == obj.get<char*>(key));
|
||||
}
|
||||
|
||||
@ -131,7 +131,7 @@ TEST_CASE("unsigned char[]") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj.set(key, "world");
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -141,7 +141,7 @@ TEST_CASE("unsigned char[]") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj.set("hello", value);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -151,7 +151,7 @@ TEST_CASE("unsigned char[]") {
|
||||
unsigned char key[] = "world";
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj.set(key, key);
|
||||
|
||||
REQUIRE(std::string("world") == obj["world"]);
|
||||
@ -162,7 +162,7 @@ TEST_CASE("unsigned char[]") {
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
REQUIRE(true == obj.containsKey(key));
|
||||
}
|
||||
|
||||
@ -171,7 +171,7 @@ TEST_CASE("unsigned char[]") {
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
obj.remove(key);
|
||||
|
||||
REQUIRE(0 == obj.size());
|
||||
@ -182,7 +182,7 @@ TEST_CASE("unsigned char[]") {
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
deserializeJson(doc, "{\"hello\":42}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(true == obj.is<int>(key));
|
||||
}
|
||||
@ -191,7 +191,7 @@ TEST_CASE("unsigned char[]") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj.createNestedArray(key);
|
||||
}
|
||||
|
||||
@ -199,7 +199,7 @@ TEST_CASE("unsigned char[]") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj.createNestedObject(key);
|
||||
}
|
||||
}
|
||||
@ -209,7 +209,7 @@ TEST_CASE("unsigned char[]") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"] = value;
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -219,7 +219,7 @@ TEST_CASE("unsigned char[]") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"].set(value);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -231,7 +231,7 @@ TEST_CASE("unsigned char[]") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& arr = doc.to<JsonArray>();
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add(value);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
@ -241,7 +241,7 @@ TEST_CASE("unsigned char[]") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& arr = doc.to<JsonArray>();
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
arr.set(0, value);
|
||||
|
||||
@ -254,7 +254,7 @@ TEST_CASE("unsigned char[]") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& arr = doc.to<JsonArray>();
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
arr[0].set(value);
|
||||
|
||||
@ -265,7 +265,7 @@ TEST_CASE("unsigned char[]") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& arr = doc.to<JsonArray>();
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
arr[0] = value;
|
||||
|
||||
|
@ -126,7 +126,7 @@ TEST_CASE("Variable Length Array") {
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj[vla] = "world";
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -142,7 +142,7 @@ TEST_CASE("Variable Length Array") {
|
||||
DynamicJsonDocument doc;
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
REQUIRE(std::string("world") == obj[vla]);
|
||||
}
|
||||
#endif
|
||||
@ -155,7 +155,7 @@ TEST_CASE("Variable Length Array") {
|
||||
DynamicJsonDocument doc;
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
REQUIRE(std::string("world") == obj.get<char*>(vla));
|
||||
}
|
||||
|
||||
@ -165,7 +165,7 @@ TEST_CASE("Variable Length Array") {
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj.set(vla, "world");
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -177,7 +177,7 @@ TEST_CASE("Variable Length Array") {
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj.set("hello", vla);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
@ -189,7 +189,7 @@ TEST_CASE("Variable Length Array") {
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj.set(vla, vla);
|
||||
|
||||
REQUIRE(std::string("world") == obj["world"]);
|
||||
@ -203,7 +203,7 @@ TEST_CASE("Variable Length Array") {
|
||||
DynamicJsonDocument doc;
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
REQUIRE(true == obj.containsKey(vla));
|
||||
}
|
||||
|
||||
@ -214,7 +214,7 @@ TEST_CASE("Variable Length Array") {
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
obj.remove(vla);
|
||||
|
||||
REQUIRE(0 == obj.size());
|
||||
@ -227,7 +227,7 @@ TEST_CASE("Variable Length Array") {
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
deserializeJson(doc, "{\"hello\":42}");
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(true == obj.is<int>(vla));
|
||||
}
|
||||
@ -238,7 +238,7 @@ TEST_CASE("Variable Length Array") {
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj.createNestedArray(vla);
|
||||
}
|
||||
|
||||
@ -248,7 +248,7 @@ TEST_CASE("Variable Length Array") {
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj.createNestedObject(vla);
|
||||
}
|
||||
}
|
||||
@ -260,7 +260,7 @@ TEST_CASE("Variable Length Array") {
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"] = vla;
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"].as<char*>());
|
||||
@ -272,7 +272,7 @@ TEST_CASE("Variable Length Array") {
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& obj = doc.to<JsonObject>();
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"].set(vla);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"].as<char*>());
|
||||
@ -286,7 +286,7 @@ TEST_CASE("Variable Length Array") {
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& arr = doc.to<JsonArray>();
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add(vla);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
@ -298,7 +298,7 @@ TEST_CASE("Variable Length Array") {
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& arr = doc.to<JsonArray>();
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
arr.set(0, vla);
|
||||
|
||||
@ -313,7 +313,7 @@ TEST_CASE("Variable Length Array") {
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& arr = doc.to<JsonArray>();
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
arr[0].set(vla);
|
||||
|
||||
@ -326,7 +326,7 @@ TEST_CASE("Variable Length Array") {
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& arr = doc.to<JsonArray>();
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
arr[0] = vla;
|
||||
|
||||
|
@ -13,7 +13,7 @@ TEST_CASE("deserialize MsgPack array") {
|
||||
const char* input = "\x90";
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(array.size() == 0);
|
||||
@ -23,7 +23,7 @@ TEST_CASE("deserialize MsgPack array") {
|
||||
const char* input = "\x92\x01\x02";
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(array.size() == 2);
|
||||
@ -37,7 +37,7 @@ TEST_CASE("deserialize MsgPack array") {
|
||||
const char* input = "\xDC\x00\x00";
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(array.size() == 0);
|
||||
@ -47,7 +47,7 @@ TEST_CASE("deserialize MsgPack array") {
|
||||
const char* input = "\xDC\x00\x02\xA5hello\xA5world";
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(array.size() == 2);
|
||||
@ -61,7 +61,7 @@ TEST_CASE("deserialize MsgPack array") {
|
||||
const char* input = "\xDD\x00\x00\x00\x00";
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(array.size() == 0);
|
||||
@ -72,7 +72,7 @@ TEST_CASE("deserialize MsgPack array") {
|
||||
"\xDD\x00\x00\x00\x02\xCA\x00\x00\x00\x00\xCA\x40\x48\xF5\xC3";
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(array.size() == 2);
|
||||
|
@ -13,7 +13,7 @@ TEST_CASE("deserialize MsgPack object") {
|
||||
const char* input = "\x80";
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -24,7 +24,7 @@ TEST_CASE("deserialize MsgPack object") {
|
||||
const char* input = "\x82\xA3one\x01\xA3two\x02";
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -39,7 +39,7 @@ TEST_CASE("deserialize MsgPack object") {
|
||||
const char* input = "\xDE\x00\x00";
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -50,7 +50,7 @@ TEST_CASE("deserialize MsgPack object") {
|
||||
const char* input = "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world";
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -65,7 +65,7 @@ TEST_CASE("deserialize MsgPack object") {
|
||||
const char* input = "\xDF\x00\x00\x00\x00";
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
@ -78,7 +78,7 @@ TEST_CASE("deserialize MsgPack object") {
|
||||
"\xF5\xC3";
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
|
@ -14,7 +14,7 @@ TEST_CASE("deserializeMsgPack(std::istream&)") {
|
||||
DeserializationError err = deserializeMsgPack(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
REQUIRE(arr[0] == 0);
|
||||
REQUIRE(arr[1] == 2);
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ TEST_CASE("deserializeMsgPack(const std::string&)") {
|
||||
DeserializationError err = deserializeMsgPack(doc, input);
|
||||
input[2] = 'X'; // alter the string tomake sure we made a copy
|
||||
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
}
|
||||
@ -39,7 +39,7 @@ TEST_CASE("deserializeMsgPack(const std::string&)") {
|
||||
deserializeMsgPack(doc, std::string("\x92\x00\x02", 3));
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
REQUIRE(arr[0] == 0);
|
||||
REQUIRE(arr[1] == 2);
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
TEST_CASE("serialize MsgPack to various destination types") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject &object = doc.to<JsonObject>();
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["hello"] = "world";
|
||||
const char *expected_result = "\x81\xA5hello\xA5world";
|
||||
const size_t expected_length = 13;
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
TEST_CASE("measureMsgPack()") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject &object = doc.to<JsonObject>();
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["hello"] = "world";
|
||||
|
||||
REQUIRE(measureMsgPack(doc) == 13);
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void check(const JsonArray& array, const char* expected_data,
|
||||
static void check(const JsonArray array, const char* expected_data,
|
||||
size_t expected_len) {
|
||||
std::string expected(expected_data, expected_data + expected_len);
|
||||
std::string actual;
|
||||
@ -16,19 +16,19 @@ static void check(const JsonArray& array, const char* expected_data,
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
static void check(const JsonArray& array, const char (&expected_data)[N]) {
|
||||
static void check(const JsonArray array, const char (&expected_data)[N]) {
|
||||
const size_t expected_len = N - 1;
|
||||
check(array, expected_data, expected_len);
|
||||
}
|
||||
|
||||
// TODO: this function is used by the commented test
|
||||
// static void check(const JsonArray& array, const std::string& expected) {
|
||||
// static void check(const JsonArray array, const std::string& expected) {
|
||||
// check(array, expected.data(), expected.length());
|
||||
// }
|
||||
|
||||
TEST_CASE("serialize MsgPack array") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonArray& array = doc.to<JsonArray>();
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("empty") {
|
||||
check(array, "\x90");
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <stdio.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void check(const JsonObject& object, const char* expected_data,
|
||||
static void check(const JsonObject object, const char* expected_data,
|
||||
size_t expected_len) {
|
||||
std::string expected(expected_data, expected_data + expected_len);
|
||||
std::string actual;
|
||||
@ -17,19 +17,19 @@ static void check(const JsonObject& object, const char* expected_data,
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
static void check(const JsonObject& object, const char (&expected_data)[N]) {
|
||||
static void check(const JsonObject object, const char (&expected_data)[N]) {
|
||||
const size_t expected_len = N - 1;
|
||||
check(object, expected_data, expected_len);
|
||||
}
|
||||
|
||||
// TODO: used by the commented test
|
||||
// static void check(const JsonObject& object, const std::string& expected) {
|
||||
// static void check(const JsonObject object, const std::string& expected) {
|
||||
// check(object, expected.data(), expected.length());
|
||||
//}
|
||||
|
||||
TEST_CASE("serialize MsgPack object") {
|
||||
DynamicJsonDocument doc;
|
||||
JsonObject& object = doc.to<JsonObject>();
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
|
||||
SECTION("empty") {
|
||||
check(object, "\x80");
|
||||
|
Reference in New Issue
Block a user