Return JsonArray and JsonObject by value (closes #309)

This commit is contained in:
Benoit Blanchon
2018-07-02 09:35:21 +02:00
parent 4fe2b1100e
commit b105e6f7c4
93 changed files with 983 additions and 1091 deletions

View File

@ -1,6 +1,36 @@
ArduinoJson: change log 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 v6.0.1-beta
----------- -----------

View File

@ -63,7 +63,7 @@ char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302
DynamicJsonDocument doc; DynamicJsonDocument doc;
deserializeJson(doc, json); deserializeJson(doc, json);
JsonObject& root = doc.as<JsonObject>(); JsonObjectRef root = doc.as<JsonObject>();
const char* sensor = root["sensor"]; const char* sensor = root["sensor"];
long time = root["time"]; long time = root["time"];
double latitude = root["data"][0]; double latitude = root["data"][0];
@ -79,11 +79,11 @@ Here is a program that generates a JSON document with ArduinoJson:
```c++ ```c++
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& root = doc.to<JsonObject>(); JsonObject root = doc.to<JsonObject>();
root["sensor"] = "gps"; root["sensor"] = "gps";
root["time"] = 1351824120; root["time"] = 1351824120;
JsonArray& data = root.createNestedArray("data"); JsonArray data = root.createNestedArray("data");
data.add(48.756080); data.add(48.756080);
data.add(2.302038); data.add(2.302038);

View File

@ -40,7 +40,7 @@ void loadConfiguration(const char *filename, Config &config) {
Serial.println(F("Failed to read file, using default configuration")); Serial.println(F("Failed to read file, using default configuration"));
// Get the root object in the document // 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 // Copy values from the JsonObject to the Config
config.port = root["port"] | 2731; config.port = root["port"] | 2731;
@ -70,7 +70,7 @@ void saveConfiguration(const char *filename, const Config &config) {
StaticJsonDocument<256> doc; StaticJsonDocument<256> doc;
// Make our document contain an object // Make our document contain an object
JsonObject &root = doc.to<JsonObject>(); JsonObject root = doc.to<JsonObject>();
// Set the values in the object // Set the values in the object
root["hostname"] = config.hostname; root["hostname"] = config.hostname;
@ -132,15 +132,4 @@ void loop() {
// not used in this example // not used in this example
} }
// See also // Visit https://arduinojson.org/v6/example/config/ for more.
// --------
//
// 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/

View File

@ -24,7 +24,7 @@ void setup() {
// DynamicJsonDocument doc(200); // DynamicJsonDocument doc(200);
// Make our document be an object // Make our document be an object
JsonObject& root = doc.to<JsonObject>(); JsonObject root = doc.to<JsonObject>();
// Add values in the object // Add values in the object
// //
@ -35,7 +35,7 @@ void setup() {
// Add an array. // Add an array.
// //
JsonArray& data = root.createNestedArray("data"); JsonArray data = root.createNestedArray("data");
data.add(48.756080); data.add(48.756080);
data.add(2.302038); data.add(2.302038);
@ -61,15 +61,4 @@ void loop() {
// not used in this example // not used in this example
} }
// See also // Visit https://arduinojson.org/v6/example/generator/ for more.
// --------
//
// 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/

View File

@ -84,7 +84,7 @@ void setup() {
} }
// Extract values // Extract values
JsonObject& root = doc.as<JsonObject>(); JsonObject root = doc.as<JsonObject>();
Serial.println(F("Response:")); Serial.println(F("Response:"));
Serial.println(root["sensor"].as<char*>()); Serial.println(root["sensor"].as<char*>());
Serial.println(root["time"].as<char*>()); Serial.println(root["time"].as<char*>());
@ -99,16 +99,4 @@ void loop() {
// not used in this example // not used in this example
} }
// See also // Visit https://arduinojson.org/v6/example/http-client/ for more.
// --------
//
// 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/

View File

@ -42,7 +42,7 @@ void setup() {
} }
// Get the root object in the document // Get the root object in the document
JsonObject& root = doc.as<JsonObject>(); JsonObject root = doc.as<JsonObject>();
// Fetch values. // Fetch values.
// //
@ -64,15 +64,4 @@ void loop() {
// not used in this example // not used in this example
} }
// See also // Visit https://arduinojson.org/v6/example/parser/ for more.
// --------
//
// 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/

View File

@ -56,10 +56,10 @@ void loop() {
StaticJsonDocument<500> doc; StaticJsonDocument<500> doc;
// Make our document represent an object // Make our document represent an object
JsonObject& root = doc.to<JsonObject>(); JsonObject root = doc.to<JsonObject>();
// Create the "analog" array // Create the "analog" array
JsonArray& analogValues = root.createNestedArray("analog"); JsonArray analogValues = root.createNestedArray("analog");
for (int pin = 0; pin < 6; pin++) { for (int pin = 0; pin < 6; pin++) {
// Read the analog input // Read the analog input
int value = analogRead(pin); int value = analogRead(pin);
@ -69,7 +69,7 @@ void loop() {
} }
// Create the "digital" array // Create the "digital" array
JsonArray& digitalValues = root.createNestedArray("digital"); JsonArray digitalValues = root.createNestedArray("digital");
for (int pin = 0; pin < 14; pin++) { for (int pin = 0; pin < 14; pin++) {
// Read the digital input // Read the digital input
int value = digitalRead(pin); int value = digitalRead(pin);
@ -95,15 +95,4 @@ void loop() {
client.stop(); client.stop();
} }
// See also // Visit https://arduinojson.org/v6/example/http-server/ for more.
// --------
//
// 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/

View File

@ -48,10 +48,10 @@ void loop() {
StaticJsonDocument<500> doc; StaticJsonDocument<500> doc;
// Make our document represent an object // Make our document represent an object
JsonObject& root = doc.to<JsonObject>(); JsonObject root = doc.to<JsonObject>();
// Create the "analog" array // Create the "analog" array
JsonArray& analogValues = root.createNestedArray("analog"); JsonArray analogValues = root.createNestedArray("analog");
for (int pin = 0; pin < 6; pin++) { for (int pin = 0; pin < 6; pin++) {
// Read the analog input // Read the analog input
int value = analogRead(pin); int value = analogRead(pin);
@ -61,7 +61,7 @@ void loop() {
} }
// Create the "digital" array // Create the "digital" array
JsonArray& digitalValues = root.createNestedArray("digital"); JsonArray digitalValues = root.createNestedArray("digital");
for (int pin = 0; pin < 14; pin++) { for (int pin = 0; pin < 14; pin++) {
// Read the digital input // Read the digital input
int value = digitalRead(pin); int value = digitalRead(pin);
@ -87,15 +87,4 @@ void loop() {
delay(10000); delay(10000);
} }
// See also // Visit https://arduinojson.org/v6/example/udp-beacon/ for more.
// --------
//
// 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/

View File

@ -55,7 +55,7 @@ void setup() {
} }
// Get the root object in the document // Get the root object in the document
JsonObject& root = doc.as<JsonObject>(); JsonObject root = doc.as<JsonObject>();
// Fetch values. // Fetch values.
// //
@ -77,15 +77,4 @@ void loop() {
// not used in this example // not used in this example
} }
// See also // Visit https://arduinojson.org/v6/example/msgpack-parser/ for more.
// --------
//
// 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/

View File

@ -21,7 +21,7 @@ void setup() {
// JsonBuffer. // JsonBuffer.
deserializeJson(doc, F("{\"sensor\":\"gps\",\"time\":1351824120," deserializeJson(doc, F("{\"sensor\":\"gps\",\"time\":1351824120,"
"\"data\":[48.756080,2.302038]}")); "\"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 // You can use a Flash String to get an element of a JsonObject
// No duplication is done. // No duplication is done.
@ -56,15 +56,4 @@ void loop() {
// not used in this example // not used in this example
} }
// See also // Visit https://arduinojson.org/v6/example/progmem/ for more.
// --------
//
// 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/

View File

@ -18,7 +18,7 @@ void setup() {
String input = String input =
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
deserializeJson(doc, input); 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 // You can use a String to get an element of a JsonObject
// No duplication is done. // No duplication is done.
@ -62,14 +62,4 @@ void loop() {
// not used in this example // not used in this example
} }
// See also // Visit https://arduinojson.org/v6/example/string/ for more.
// --------
//
// 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/

View File

@ -7,13 +7,20 @@
#include "ArduinoJson/version.hpp" #include "ArduinoJson/version.hpp"
#include "ArduinoJson/DynamicJsonDocument.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/JsonDeserializer.hpp"
#include "ArduinoJson/Json/JsonSerializer.hpp" #include "ArduinoJson/Json/JsonSerializer.hpp"
#include "ArduinoJson/Json/PrettyJsonSerializer.hpp" #include "ArduinoJson/Json/PrettyJsonSerializer.hpp"
#include "ArduinoJson/MsgPack/MsgPackDeserializer.hpp" #include "ArduinoJson/MsgPack/MsgPackDeserializer.hpp"
#include "ArduinoJson/MsgPack/MsgPackSerializer.hpp" #include "ArduinoJson/MsgPack/MsgPackSerializer.hpp"
#include "ArduinoJson/StaticJsonDocument.hpp"
#include "ArduinoJson/JsonArrayImpl.hpp"
#include "ArduinoJson/JsonObjectImpl.hpp"
#include "ArduinoJson/JsonVariantImpl.hpp"

View File

@ -19,24 +19,5 @@ struct JsonVariantAs<char*> {
typedef const char* type; typedef const char* type;
}; };
template <> } // namespace Internals
struct JsonVariantAs<JsonArray> { } // namespace ArduinoJson
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;
};
}
}

View File

@ -8,20 +8,19 @@
#include "JsonInteger.hpp" #include "JsonInteger.hpp"
namespace ArduinoJson { namespace ArduinoJson {
// Forward declarations
class JsonArray;
class JsonObject;
namespace Internals { namespace Internals {
// Forward declarations
struct JsonArrayData;
struct JsonObjectData;
// A union that defines the actual content of a JsonVariant. // A union that defines the actual content of a JsonVariant.
// The enum JsonVariantType determines which member is in use. // The enum JsonVariantType determines which member is in use.
union JsonVariantContent { union JsonVariantContent {
JsonFloat asFloat; // used for double and float JsonFloat asFloat; // used for double and float
JsonUInt asInteger; // used for bool, char, short, int and longs JsonUInt asInteger; // used for bool, char, short, int and longs
const char* asString; // asString can be null const char* asString; // asString can be null
JsonArray* asArray; // asArray cannot be null JsonArrayData* asArray; // asArray cannot be null
JsonObject* asObject; // asObject cannot be null JsonObjectData* asObject; // asObject cannot be null
}; };
} } // namespace Internals
} } // namespace ArduinoJson

View File

@ -5,8 +5,6 @@
#pragma once #pragma once
namespace ArduinoJson { namespace ArduinoJson {
class JsonArray;
class JsonObject;
namespace Internals { namespace Internals {
@ -19,9 +17,9 @@ enum JsonVariantType {
JSON_BOOLEAN, // JsonVariant stores a bool JSON_BOOLEAN, // JsonVariant stores a bool
JSON_POSITIVE_INTEGER, // JsonVariant stores an JsonUInt JSON_POSITIVE_INTEGER, // JsonVariant stores an JsonUInt
JSON_NEGATIVE_INTEGER, // JsonVariant stores an JsonUInt that must be negated JSON_NEGATIVE_INTEGER, // JsonVariant stores an JsonUInt that must be negated
JSON_ARRAY, // JsonVariant stores a pointer to a JsonArray JSON_ARRAY, // JsonVariant stores a pointer to a JsonArrayData
JSON_OBJECT, // JsonVariant stores a pointer to a JsonObject JSON_OBJECT, // JsonVariant stores a pointer to a JsonObjectData
JSON_FLOAT // JsonVariant stores a JsonFloat JSON_FLOAT // JsonVariant stores a JsonFloat
}; };
} } // namespace Internals
} } // namespace ArduinoJson

View File

@ -13,7 +13,7 @@ namespace Internals {
// A singly linked list of T. // A singly linked list of T.
// The linked list is composed of ListNode<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> template <typename T>
class List { class List {
public: public:
@ -22,23 +22,10 @@ class List {
typedef ListIterator<T> iterator; typedef ListIterator<T> iterator;
typedef ListConstIterator<T> const_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) {} 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. // 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 size() const {
size_t nodeCount = 0; size_t nodeCount = 0;
for (node_type *node = _firstNode; node; node = node->next) nodeCount++; for (node_type *node = _firstNode; node; node = node->next) nodeCount++;
@ -87,14 +74,13 @@ class List {
JsonBuffer &buffer() const { JsonBuffer &buffer() const {
return *_buffer; return *_buffer;
} }
JsonBuffer *_buffer; // TODO!!
protected: protected:
void clear() { void clear() {
_firstNode = 0; _firstNode = 0;
} }
JsonBuffer *_buffer;
private: private:
node_type *_firstNode; node_type *_firstNode;
}; };

View File

@ -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;
}
};
}
}

View File

@ -32,28 +32,26 @@ class DynamicJsonDocument {
return _root.as<T>(); return _root.as<T>();
} }
// JsonObject& to<JsonObject>() // JsonObject to<JsonObject>()
template <typename T> template <typename T>
typename Internals::enable_if<Internals::is_same<T, JsonObject>::value, typename Internals::enable_if<Internals::is_same<T, JsonObject>::value,
JsonObject&>::type JsonObject>::type
to() { to() {
clear(); clear();
JsonObject* object = new (&_buffer) JsonObject(&_buffer); JsonObject object(&_buffer);
if (!object) return JsonObject::invalid();
_root = object; _root = object;
return *object; return object;
} }
// JsonArray& to<JsonArray>() // JsonArray to<JsonArray>()
template <typename T> template <typename T>
typename Internals::enable_if<Internals::is_same<T, JsonArray>::value, typename Internals::enable_if<Internals::is_same<T, JsonArray>::value,
JsonArray&>::type JsonArray>::type
to() { to() {
clear(); clear();
JsonArray* array = new (&_buffer) JsonArray(&_buffer); JsonArray array(&_buffer);
if (!array) return JsonArray::invalid();
_root = array; _root = array;
return *array; return array;
} }
// JsonVariant& to<JsonVariant>() // JsonVariant& to<JsonVariant>()

View File

@ -66,8 +66,8 @@ class JsonDeserializer {
DeserializationError parseArray(JsonVariant &variant) { DeserializationError parseArray(JsonVariant &variant) {
if (_nestingLimit == 0) return DeserializationError::TooDeep; if (_nestingLimit == 0) return DeserializationError::TooDeep;
JsonArray *array = new (_buffer) JsonArray(_buffer); JsonArray array(_buffer);
if (!array) return DeserializationError::NoMemory; if (array.isNull()) return DeserializationError::NoMemory;
variant = array; variant = array;
// Check opening braket // Check opening braket
@ -88,7 +88,7 @@ class JsonDeserializer {
err = parse(value); err = parse(value);
_nestingLimit++; _nestingLimit++;
if (err) return err; if (err) return err;
if (!array->add(value)) return DeserializationError::NoMemory; if (!array.add(value)) return DeserializationError::NoMemory;
// 2 - Skip spaces // 2 - Skip spaces
err = skipSpacesAndComments(); err = skipSpacesAndComments();
@ -103,8 +103,8 @@ class JsonDeserializer {
DeserializationError parseObject(JsonVariant &variant) { DeserializationError parseObject(JsonVariant &variant) {
if (_nestingLimit == 0) return DeserializationError::TooDeep; if (_nestingLimit == 0) return DeserializationError::TooDeep;
JsonObject *object = new (_buffer) JsonObject(_buffer); JsonObject object(_buffer);
if (!object) return DeserializationError::NoMemory; if (object.isNull()) return DeserializationError::NoMemory;
variant = object; variant = object;
// Check opening brace // Check opening brace
@ -126,9 +126,7 @@ class JsonDeserializer {
// Skip spaces // Skip spaces
err = skipSpacesAndComments(); err = skipSpacesAndComments();
if (err) return err; if (err) return err; // Colon
// Colon
if (!eat(':')) return DeserializationError::InvalidInput; if (!eat(':')) return DeserializationError::InvalidInput;
// Parse value // Parse value
@ -137,7 +135,7 @@ class JsonDeserializer {
err = parse(value); err = parse(value);
_nestingLimit++; _nestingLimit++;
if (err) return err; if (err) return err;
if (!object->set(key, value)) return DeserializationError::NoMemory; if (!object.set(key, value)) return DeserializationError::NoMemory;
// Skip spaces // Skip spaces
err = skipSpacesAndComments(); err = skipSpacesAndComments();

View File

@ -75,7 +75,9 @@ class JsonSerializer {
_writer.writeBoolean(value); _writer.writeBoolean(value);
} }
void acceptUndefined() {} void acceptNull() {
_writer.writeRaw("null");
}
size_t bytesWritten() const { size_t bytesWritten() const {
return _writer.bytesWritten(); return _writer.bytesWritten();

View File

@ -13,13 +13,6 @@
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { 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> template <typename Print>
class JsonWriter { class JsonWriter {
public: public:

View File

@ -4,47 +4,33 @@
#pragma once #pragma once
#include "Data/List.hpp" #include "./JsonArrayData.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))
namespace ArduinoJson { namespace ArduinoJson {
// Forward declarations
class JsonObject; class JsonObject;
class JsonBuffer;
namespace Internals { namespace Internals {
class JsonArraySubscript; class JsonArraySubscript;
} }
class JsonArray : public Internals::ReferenceType, class JsonArray {
public Internals::NonCopyable, friend class JsonVariant;
public Internals::List<JsonVariant>,
public Internals::JsonBufferAllocated {
public: public:
explicit JsonArray(Internals::JsonBuffer *buf) throw() typedef Internals::JsonArrayData::iterator iterator;
: Internals::List<JsonVariant>(buf) {} typedef Internals::JsonArrayData::const_iterator const_iterator;
// Gets the value at the specified index JsonArray() : _data(0) {}
const Internals::JsonArraySubscript operator[](size_t index) const; JsonArray(Internals::JsonArrayData* arr) : _data(arr) {}
JsonArray(Internals::JsonBuffer* buf)
// Gets or sets the value at specified index : _data(new (buf) Internals::JsonArrayData(buf)) {}
Internals::JsonArraySubscript operator[](size_t index);
// Adds the specified value at the end of the array. // Adds the specified value at the end of the array.
// //
// bool add(TValue); // bool add(TValue);
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant, // TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
// std::string, String, JsonArray, JsonObject // std::string, String, JsonArrayData, JsonObject
template <typename T> template <typename T>
bool add(const T& value) { bool add(const T& value) {
return add_impl<const T&>(value); return add_impl<const T&>(value);
@ -56,73 +42,23 @@ class JsonArray : public Internals::ReferenceType,
bool add(T* value) { bool add(T* value) {
return add_impl<T*>(value); return add_impl<T*>(value);
} }
//
// bool add(TValue value, uint8_t decimals); iterator begin() {
// TValue = float, double if (!_data) return iterator();
template <typename T> return _data->begin();
DEPRECATED("Second argument is not supported anymore")
bool add(T value, uint8_t) {
return add_impl<const JsonVariant &>(JsonVariant(value));
} }
// Sets the value at specified index. const_iterator begin() const {
// if (!_data) return const_iterator();
// bool add(size_t index, const TValue&); return _data->begin();
// 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));
} }
// Gets the value at the specified index. iterator end() {
template <typename T> return iterator();
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. const_iterator end() const {
template <typename T> return const_iterator();
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;
} }
// Imports a 1D array // Imports a 1D array
@ -146,7 +82,7 @@ class JsonArray : public Internals::ReferenceType,
bool copyFrom(T (&array)[N1][N2]) { bool copyFrom(T (&array)[N1][N2]) {
bool ok = true; bool ok = true;
for (size_t i = 0; i < N1; i++) { for (size_t i = 0; i < N1; i++) {
JsonArray &nestedArray = createNestedArray(); JsonArray nestedArray = createNestedArray();
for (size_t j = 0; j < N2; j++) { for (size_t j = 0; j < N2; j++) {
ok &= nestedArray.add(array[i][j]); ok &= nestedArray.add(array[i][j]);
} }
@ -172,15 +108,83 @@ class JsonArray : public Internals::ReferenceType,
// Exports a 2D array // Exports a 2D array
template <typename T, size_t N1, size_t N2> template <typename T, size_t N1, size_t N2>
void copyTo(T (&array)[N1][N2]) const { void copyTo(T (&array)[N1][N2]) const {
if (!_data) return;
size_t i = 0; size_t i = 0;
for (const_iterator it = begin(); it != end() && i < N1; ++it) { for (const_iterator it = begin(); it != end() && i < N1; ++it) {
it->as<JsonArray>().copyTo(array[i++]); 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> template <typename Visitor>
void visit(Visitor& visitor) const { void visit(Visitor& visitor) const {
if (_data)
return visitor.acceptArray(*this); return visitor.acceptArray(*this);
else
visitor.acceptNull();
} }
private: private:
@ -188,23 +192,17 @@ class JsonArray : public Internals::ReferenceType,
bool set_impl(size_t index, TValueRef value) { bool set_impl(size_t index, TValueRef value) {
iterator it = begin() += index; iterator it = begin() += index;
if (it == end()) return false; 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> template <typename TValueRef>
bool add_impl(TValueRef value) { bool add_impl(TValueRef value) {
iterator it = Internals::List<JsonVariant>::add(); if (!_data) return false;
iterator it = _data->add();
if (it == end()) return false; if (it == end()) return false;
return Internals::ValueSaver<TValueRef>::save(_buffer, *it, value); return Internals::ValueSaver<TValueRef>::save(_data->_buffer, *it, value);
} }
};
namespace Internals { Internals::JsonArrayData* _data;
template <>
struct JsonVariantDefault<JsonArray> {
static JsonArray &get() {
return JsonArray::invalid();
}
}; };
} // namespace Internals
} // namespace ArduinoJson } // namespace ArduinoJson

View 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

View File

@ -5,24 +5,21 @@
#pragma once #pragma once
#include "JsonArray.hpp" #include "JsonArray.hpp"
#include "JsonArraySubscript.hpp"
#include "JsonObject.hpp" #include "JsonObject.hpp"
namespace ArduinoJson { namespace ArduinoJson {
inline JsonArray &JsonArray::createNestedArray() { inline JsonArray JsonArray::createNestedArray() {
JsonArray *array = new (_buffer) JsonArray(_buffer); if (!_data) return JsonArray();
if (!array) return JsonArray::invalid(); JsonArray array(_data->_buffer);
if (!array.isNull()) add(array);
add(array); return array;
return *array;
} }
inline JsonObject &JsonArray::createNestedObject() { inline JsonObject JsonArray::createNestedObject() {
JsonObject *object = new (_buffer) JsonObject(_buffer); if (!_data) return JsonObject();
if (!object) return JsonObject::invalid(); JsonObject object(_data->_buffer);
if (!object.isNull()) add(object);
add(object); return object;
return *object;
} }
} // namespace ArduinoJson } // namespace ArduinoJson

View File

@ -16,7 +16,7 @@ namespace ArduinoJson {
namespace Internals { namespace Internals {
class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> { class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
public: public:
FORCE_INLINE JsonArraySubscript(JsonArray& array, size_t index) FORCE_INLINE JsonArraySubscript(JsonArray array, size_t index)
: _array(array), _index(index) {} : _array(array), _index(index) {}
FORCE_INLINE JsonArraySubscript& operator=(const JsonArraySubscript& src) { FORCE_INLINE JsonArraySubscript& operator=(const JsonArraySubscript& src) {
@ -43,8 +43,8 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
return *this; return *this;
} }
FORCE_INLINE bool success() const { FORCE_INLINE bool isNull() const {
return _index < _array.size(); return _index >= _array.size();
} }
template <typename T> template <typename T>
@ -80,7 +80,7 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
} }
private: private:
JsonArray& _array; JsonArray _array;
const size_t _index; const size_t _index;
}; };
@ -103,7 +103,7 @@ inline Internals::JsonArraySubscript JsonArray::operator[](size_t index) {
inline const Internals::JsonArraySubscript JsonArray::operator[]( inline const Internals::JsonArraySubscript JsonArray::operator[](
size_t index) const { size_t index) const {
return Internals::JsonArraySubscript(*const_cast<JsonArray*>(this), index); return Internals::JsonArraySubscript(*this, index);
} }
} // namespace ArduinoJson } // namespace ArduinoJson

View File

@ -4,110 +4,82 @@
#pragma once #pragma once
#include "Data/List.hpp" #include "./JsonObjectData.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))
namespace ArduinoJson { namespace ArduinoJson {
// Forward declarations class JsonObject {
class JsonArray; friend class JsonVariant;
class JsonBuffer;
namespace Internals {
template <typename>
class JsonObjectSubscript;
}
class JsonObject : public Internals::ReferenceType,
public Internals::NonCopyable,
public Internals::List<JsonPair>,
public Internals::JsonBufferAllocated {
public: public:
// Create an empty JsonArray attached to the specified JsonBuffer. typedef Internals::JsonObjectData::iterator iterator;
// You should not use this constructor directly. typedef Internals::JsonObjectData::const_iterator const_iterator;
explicit JsonObject(Internals::JsonBuffer* buf) throw()
: Internals::List<JsonPair>(buf) {}
// Gets or sets the value associated with the specified key. JsonObject() : _data(0) {}
// JsonObject(Internals::JsonObjectData* object) : _data(object) {}
// JsonObjectSubscript operator[](TKey) JsonObject(Internals::JsonBuffer* buf)
// TKey = const std::string&, const String& : _data(new (buf) Internals::JsonObjectData(buf)) {}
template <typename TString>
Internals::JsonObjectSubscript<const TString&> operator[]( iterator begin() {
const TString& key) { if (!_data) return iterator();
return Internals::JsonObjectSubscript<const TString&>(*this, key); return _data->begin();
}
//
// 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);
} }
// Gets the value associated with the specified key. const_iterator begin() const {
// if (!_data) return const_iterator();
// const JsonObjectSubscript operator[](TKey) const; return _data->begin();
// 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);
} }
// 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& // TKey = const std::string&, const String&
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant, template <typename TString>
// std::string, String, JsonArray, JsonObject bool containsKey(const TString& key) const {
template <typename TValue, typename TString> return containsKey_impl<const TString&>(key);
bool set(const TString& key, const TValue& value) {
return set_impl<const TString&, const TValue&>(key, value);
} }
// //
// 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& // TKey = const std::string&, const String&
// TValue = char*, const char*, const FlashStringHelper* template <typename TString>
template <typename TValue, typename TString> JsonArray createNestedArray(const TString& key);
bool set(const TString& key, TValue* value) { // JsonArray createNestedArray(TKey);
return set_impl<const TString&, TValue*>(key, value); // 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&); // JsonObject createNestedObject(TKey);
// TKey = char*, const char*, const FlashStringHelper* // TKey = char*, const char*, char[], const char[], const FlashStringHelper*
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant, template <typename TString>
// std::string, String, JsonArray, JsonObject JsonObject createNestedObject(TString* key) {
template <typename TValue, typename TString> return createNestedObject_impl<TString*>(key);
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);
} }
// Gets the value associated with the specified key. // Gets the value associated with the specified key.
@ -152,51 +124,48 @@ class JsonObject : public Internals::ReferenceType,
return is_impl<TString*, TValue>(key); 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& // TKey = const std::string&, const String&
template <typename TString> template <typename TString>
JsonArray& createNestedArray(const TString& key) { Internals::JsonObjectSubscript<const TString&> operator[](
return createNestedArray_impl<const TString&>(key); 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> template <typename TString>
JsonArray& createNestedArray(TString* key) { Internals::JsonObjectSubscript<TString*> operator[](TString* key) {
return createNestedArray_impl<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& // TKey = const std::string&, const String&
template <typename TString> template <typename TString>
JsonObject& createNestedObject(const TString& key) { const Internals::JsonObjectSubscript<const TString&> operator[](
return createNestedObject_impl<const TString&>(key); const TString& key) const {
return Internals::JsonObjectSubscript<const TString&>(*this, key);
} }
// //
// JsonObject& createNestedObject(TKey); // const JsonObjectSubscript operator[](TKey) const;
// TKey = char*, const char*, char[], const char[], const FlashStringHelper* // TKey = const char*, const char[N], const FlashStringHelper*
template <typename TString> template <typename TString>
JsonObject& createNestedObject(TString* key) { const Internals::JsonObjectSubscript<TString*> operator[](
return createNestedObject_impl<TString*>(key); TString* key) const {
return Internals::JsonObjectSubscript<TString*>(*this, key);
} }
// Tells weither the specified key is present and associated with a value. bool operator==(const JsonObject& rhs) const {
// return _data == rhs._data;
// 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 containsKey(TKey); void remove(iterator it) {
// TKey = char*, const char*, char[], const char[], const FlashStringHelper* if (!_data) return;
template <typename TString> _data->remove(it);
bool containsKey(TString* key) const {
return findKey<TString*>(key) != end();
} }
// Removes the specified key and the associated value. // Removes the specified key and the associated value.
@ -205,33 +174,81 @@ class JsonObject : public Internals::ReferenceType,
// TKey = const std::string&, const String& // TKey = const std::string&, const String&
template <typename TString> template <typename TString>
void remove(const TString& key) { void remove(const TString& key) {
remove(findKey<const TString&>(key)); remove_impl<const TString&>(key);
} }
// //
// void remove(TKey); // void remove(TKey);
// TKey = char*, const char*, char[], const char[], const FlashStringHelper* // TKey = char*, const char*, char[], const char[], const FlashStringHelper*
template <typename TString> template <typename TString>
void remove(TString* key) { 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) // bool set(TKey, TValue);
using Internals::List<JsonPair>::remove; // 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. size_t size() const {
// This object is meant to replace a NULL pointer. if (!_data) return 0;
// This is used when memory allocation or JSON parsing fail. return _data->size();
static JsonObject& invalid() { }
static JsonObject instance(NULL);
return instance; bool isNull() const {
return _data == 0;
} }
template <typename Visitor> template <typename Visitor>
void visit(Visitor& visitor) const { void visit(Visitor& visitor) const {
return visitor.acceptObject(*this); if (_data)
visitor.acceptObject(*this);
else
return visitor.acceptNull();
} }
private: 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. // Returns the list node that matches the specified key.
template <typename TStringRef> template <typename TStringRef>
iterator findKey(TStringRef key) { iterator findKey(TStringRef key) {
@ -254,26 +271,6 @@ class JsonObject : public Internals::ReferenceType,
: Internals::JsonVariantDefault<TValue>::get(); : 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> template <typename TStringRef, typename TValue>
bool is_impl(TStringRef key) const { bool is_impl(TStringRef key) const {
const_iterator it = findKey<TStringRef>(key); const_iterator it = findKey<TStringRef>(key);
@ -281,18 +278,34 @@ class JsonObject : public Internals::ReferenceType,
} }
template <typename TStringRef> template <typename TStringRef>
JsonArray& createNestedArray_impl(TStringRef key); void remove_impl(TStringRef key) {
if (!_data) return;
template <typename TStringRef> _data->remove(findKey<TStringRef>(key));
JsonObject& createNestedObject_impl(TStringRef key);
};
namespace Internals {
template <>
struct JsonVariantDefault<JsonObject> {
static JsonObject& get() {
return JsonObject::invalid();
} }
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 } // namespace ArduinoJson

View 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

View File

@ -6,23 +6,32 @@
#include "JsonArray.hpp" #include "JsonArray.hpp"
#include "JsonObject.hpp" #include "JsonObject.hpp"
#include "JsonObjectSubscript.hpp"
namespace ArduinoJson { namespace ArduinoJson {
template <typename TStringRef> template <typename TString>
inline JsonArray &JsonObject::createNestedArray_impl(TStringRef key) { inline JsonArray JsonObject::createNestedArray(const TString& key) {
JsonArray *array = new (_buffer) JsonArray(_buffer); return createNestedArray_impl<const TString&>(key);
if (!array) return JsonArray::invalid(); }
set(key, array);
return *array; template <typename TString>
inline JsonArray JsonObject::createNestedArray(TString* key) {
return createNestedArray_impl<TString*>(key);
} }
template <typename TStringRef> template <typename TStringRef>
inline JsonObject &JsonObject::createNestedObject_impl(TStringRef key) { inline JsonArray JsonObject::createNestedArray_impl(TStringRef key) {
JsonObject *object = new (_buffer) JsonObject(_buffer); if (!_data) return JsonArray();
if (!object) return JsonObject::invalid(); JsonArray array(_data->_buffer);
set(key, object); if (!array.isNull()) set(key, array);
return *object; 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 } // namespace ArduinoJson

View File

@ -22,7 +22,7 @@ class JsonObjectSubscript
typedef JsonObjectSubscript<TStringRef> this_type; typedef JsonObjectSubscript<TStringRef> this_type;
public: public:
FORCE_INLINE JsonObjectSubscript(JsonObject& object, TStringRef key) FORCE_INLINE JsonObjectSubscript(JsonObject object, TStringRef key)
: _object(object), _key(key) {} : _object(object), _key(key) {}
FORCE_INLINE this_type &operator=(const this_type &src) { FORCE_INLINE this_type &operator=(const this_type &src) {
@ -50,8 +50,8 @@ class JsonObjectSubscript
return *this; return *this;
} }
FORCE_INLINE bool success() const { FORCE_INLINE bool isNull() const {
return _object.containsKey(_key); return !_object.containsKey(_key);
} }
template <typename TValue> template <typename TValue>
@ -88,9 +88,42 @@ class JsonObjectSubscript
} }
private: private:
JsonObject& _object; JsonObject _object;
TStringRef _key; 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 Internals
} // namespace ArduinoJson } // namespace ArduinoJson

View File

@ -8,9 +8,9 @@
namespace ArduinoJson { namespace ArduinoJson {
// A key value pair for JsonObject. // A key value pair for JsonObjectData.
struct JsonPair { struct JsonPair {
const char* key; const char* key;
JsonVariant value; JsonVariant value;
}; };
} } // namespace ArduinoJson

View File

@ -104,25 +104,8 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
_content.asString = value; _content.asString = value;
} }
// Create a JsonVariant containing a reference to an array. JsonVariant(JsonArray array);
// CAUTION: we are lying about constness, because the array can be modified if JsonVariant(JsonObject object);
// 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;
}
// Get the variant as the specified type. // Get the variant as the specified type.
// //
@ -179,48 +162,23 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
return s; return s;
} }
// //
// JsonArray& as<JsonArray> const; // JsonArray as<JsonArray>() const;
// JsonArray& as<JsonArray&> const; // const JsonArray as<const JsonArray>() const;
template <typename T> template <typename T>
typename Internals::enable_if< 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>::value,
JsonArray &>::type JsonArray>::type
as() const { as() const;
return variantAsArray();
}
// //
// const JsonArray& as<const JsonArray&> const; // JsonObject as<JsonObject>() const;
// const JsonObject as<const JsonObject>() const;
template <typename T> template <typename T>
typename Internals::enable_if< typename Internals::enable_if<
Internals::is_same<typename Internals::remove_reference<T>::type, Internals::is_same<typename Internals::remove_const<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,
JsonObject>::value, JsonObject>::value,
JsonObject &>::type T>::type
as() const { 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();
}
// //
// JsonVariant as<JsonVariant> const; // JsonVariant as<JsonVariant> const;
template <typename T> template <typename T>
@ -275,13 +233,10 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
} }
// //
// bool is<JsonArray> const; // bool is<JsonArray> const;
// bool is<JsonArray&> const; // bool is<const JsonArray> const;
// bool is<const JsonArray&> const;
template <typename T> template <typename T>
typename Internals::enable_if< typename Internals::enable_if<
Internals::is_same< Internals::is_same<typename Internals::remove_const<T>::type,
typename Internals::remove_const<
typename Internals::remove_reference<T>::type>::type,
JsonArray>::value, JsonArray>::value,
bool>::type bool>::type
is() const { is() const {
@ -289,13 +244,10 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
} }
// //
// bool is<JsonObject> const; // bool is<JsonObject> const;
// bool is<JsonObject&> const; // bool is<const JsonObject> const;
// bool is<const JsonObject&> const;
template <typename T> template <typename T>
typename Internals::enable_if< typename Internals::enable_if<
Internals::is_same< Internals::is_same<typename Internals::remove_const<T>::type,
typename Internals::remove_const<
typename Internals::remove_reference<T>::type>::type,
JsonObject>::value, JsonObject>::value,
bool>::type bool>::type
is() const { is() const {
@ -303,8 +255,8 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
} }
// Returns true if the variant has a value // Returns true if the variant has a value
bool success() const { bool isNull() const {
return _type != Internals::JSON_UNDEFINED; return _type == Internals::JSON_UNDEFINED;
} }
template <typename Visitor> template <typename Visitor>
@ -315,10 +267,10 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
return visitor.acceptFloat(_content.asFloat); return visitor.acceptFloat(_content.asFloat);
case JSON_ARRAY: case JSON_ARRAY:
return visitor.acceptArray(*_content.asArray); return visitor.acceptArray(_content.asArray);
case JSON_OBJECT: case JSON_OBJECT:
return visitor.acceptObject(*_content.asObject); return visitor.acceptObject(_content.asObject);
case JSON_STRING: case JSON_STRING:
return visitor.acceptString(_content.asString); return visitor.acceptString(_content.asString);
@ -336,13 +288,13 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
return visitor.acceptBoolean(_content.asInteger != 0); return visitor.acceptBoolean(_content.asInteger != 0);
default: // JSON_UNDEFINED default: // JSON_UNDEFINED
return visitor.acceptUndefined(); return visitor.acceptNull();
} }
} }
private: private:
JsonArray &variantAsArray() const; JsonArray variantAsArray() const;
JsonObject &variantAsObject() const; JsonObject variantAsObject() const;
const char *variantAsString() const; const char *variantAsString() const;
template <typename T> template <typename T>
T variantAsFloat() const; T variantAsFloat() const;

View File

@ -13,21 +13,6 @@ namespace Internals {
template <typename TImpl> template <typename TImpl>
class JsonVariantCasts { class JsonVariantCasts {
public: 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> template <typename T>
FORCE_INLINE operator T() const { FORCE_INLINE operator T() const {
return impl()->template as<T>(); return impl()->template as<T>();

View File

@ -9,6 +9,9 @@
#include "Strings/StringTraits.hpp" #include "Strings/StringTraits.hpp"
namespace ArduinoJson { namespace ArduinoJson {
class JsonArray;
class JsonObject;
namespace Internals { namespace Internals {
template <typename TImpl> template <typename TImpl>

View File

@ -5,8 +5,8 @@
#pragma once #pragma once
#include "Configuration.hpp" #include "Configuration.hpp"
#include "JsonArray.hpp" #include "JsonArrayData.hpp"
#include "JsonObject.hpp" #include "JsonObjectData.hpp"
#include "JsonVariant.hpp" #include "JsonVariant.hpp"
#include "Numbers/isFloat.hpp" #include "Numbers/isFloat.hpp"
#include "Numbers/isInteger.hpp" #include "Numbers/isInteger.hpp"
@ -17,32 +17,50 @@
namespace ArduinoJson { namespace ArduinoJson {
inline JsonVariant::JsonVariant(const JsonArray &array) { inline JsonVariant::JsonVariant(JsonArray array) {
if (array.success()) { if (!array.isNull()) {
_type = Internals::JSON_ARRAY; _type = Internals::JSON_ARRAY;
_content.asArray = const_cast<JsonArray *>(&array); _content.asArray = array._data;
} else { } else {
_type = Internals::JSON_UNDEFINED; _type = Internals::JSON_UNDEFINED;
} }
} }
inline JsonVariant::JsonVariant(const JsonObject &object) { inline JsonVariant::JsonVariant(JsonObject object) {
if (object.success()) { if (!object.isNull()) {
_type = Internals::JSON_OBJECT; _type = Internals::JSON_OBJECT;
_content.asObject = const_cast<JsonObject *>(&object); _content.asObject = object._data;
} else { } else {
_type = Internals::JSON_UNDEFINED; _type = Internals::JSON_UNDEFINED;
} }
} }
inline JsonArray &JsonVariant::variantAsArray() const { template <typename T>
if (_type == Internals::JSON_ARRAY) return *_content.asArray; inline typename Internals::enable_if<
return JsonArray::invalid(); Internals::is_same<typename Internals::remove_const<T>::type,
JsonArray>::value,
JsonArray>::type
JsonVariant::as() const {
return variantAsArray();
} }
inline JsonObject &JsonVariant::variantAsObject() const { template <typename T>
if (_type == Internals::JSON_OBJECT) return *_content.asObject; inline typename Internals::enable_if<
return JsonObject::invalid(); 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> template <typename T>

View File

@ -10,6 +10,8 @@
#include "Strings/StringTraits.hpp" #include "Strings/StringTraits.hpp"
namespace ArduinoJson { namespace ArduinoJson {
class JsonArray;
class JsonObject;
namespace Internals { namespace Internals {
// Forward declarations. // Forward declarations.
@ -30,14 +32,12 @@ class JsonVariantSubscripts {
// Mimics an array. // Mimics an array.
// Returns the element at specified index if the variant is 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 const JsonArraySubscript operator[](size_t index) const;
FORCE_INLINE JsonArraySubscript operator[](size_t index); FORCE_INLINE JsonArraySubscript operator[](size_t index);
// Mimics an object. // Mimics an object.
// Returns the value associated with the specified key if the variant is // Returns the value associated with the specified key if the variant is
// an object. // an object.
// Return JsonVariant::invalid() if the variant is not an object.
// //
// const JsonObjectSubscript operator[](TKey) const; // const JsonObjectSubscript operator[](TKey) const;
// TKey = const std::string&, const String& // TKey = const std::string&, const String&
@ -45,27 +45,21 @@ class JsonVariantSubscripts {
FORCE_INLINE FORCE_INLINE
typename enable_if<StringTraits<TString>::has_equals, typename enable_if<StringTraits<TString>::has_equals,
const JsonObjectSubscript<const TString &> >::type const JsonObjectSubscript<const TString &> >::type
operator[](const TString &key) const { operator[](const TString &key) const;
return impl()->template as<JsonObject>()[key];
}
// //
// const JsonObjectSubscript operator[](TKey) const; // const JsonObjectSubscript operator[](TKey) const;
// TKey = const std::string&, const String& // TKey = const std::string&, const String&
template <typename TString> template <typename TString>
FORCE_INLINE typename enable_if<StringTraits<TString>::has_equals, FORCE_INLINE typename enable_if<StringTraits<TString>::has_equals,
JsonObjectSubscript<const TString &> >::type JsonObjectSubscript<const TString &> >::type
operator[](const TString &key) { operator[](const TString &key);
return impl()->template as<JsonObject>()[key];
}
// //
// JsonObjectSubscript operator[](TKey); // JsonObjectSubscript operator[](TKey);
// TKey = const char*, const char[N], const FlashStringHelper* // TKey = const char*, const char[N], const FlashStringHelper*
template <typename TString> template <typename TString>
FORCE_INLINE typename enable_if<StringTraits<const TString *>::has_equals, FORCE_INLINE typename enable_if<StringTraits<const TString *>::has_equals,
JsonObjectSubscript<const TString *> >::type JsonObjectSubscript<const TString *> >::type
operator[](const TString *key) { operator[](const TString *key);
return impl()->template as<JsonObject>()[key];
}
// //
// JsonObjectSubscript operator[](TKey); // JsonObjectSubscript operator[](TKey);
// TKey = const char*, const char[N], const FlashStringHelper* // TKey = const char*, const char[N], const FlashStringHelper*
@ -73,9 +67,7 @@ class JsonVariantSubscripts {
FORCE_INLINE FORCE_INLINE
typename enable_if<StringTraits<TString *>::has_equals, typename enable_if<StringTraits<TString *>::has_equals,
const JsonObjectSubscript<const TString *> >::type const JsonObjectSubscript<const TString *> >::type
operator[](const TString *key) const { operator[](const TString *key) const;
return impl()->template as<JsonObject>()[key];
}
private: private:
const TImpl *impl() const { const TImpl *impl() const {

View File

@ -9,7 +9,6 @@
#include <string.h> #include <string.h>
#include "../Configuration.hpp" #include "../Configuration.hpp"
#include "../Polyfills/NonCopyable.hpp"
#include "../Polyfills/attributes.hpp" #include "../Polyfills/attributes.hpp"
namespace ArduinoJson { namespace ArduinoJson {
@ -17,7 +16,7 @@ namespace Internals {
// Handle the memory management (done in derived classes) and calls the parser. // Handle the memory management (done in derived classes) and calls the parser.
// This abstract class is implemented by StaticJsonBuffer which implements a // This abstract class is implemented by StaticJsonBuffer which implements a
// fixed memory allocation. // fixed memory allocation.
class JsonBuffer : NonCopyable { class JsonBuffer {
public: public:
// Allocates n bytes in the JsonBuffer. // Allocates n bytes in the JsonBuffer.
// Return a pointer to the allocated memory or NULL if allocation fails. // Return a pointer to the allocated memory or NULL if allocation fails.

View File

@ -240,13 +240,13 @@ class MsgPackDeserializer {
} }
DeserializationError readArray(JsonVariant &variant, size_t n) { DeserializationError readArray(JsonVariant &variant, size_t n) {
JsonArray *array = new (_buffer) JsonArray(_buffer); JsonArray array(_buffer);
if (!array) return DeserializationError::NoMemory; if (array.isNull()) return DeserializationError::NoMemory;
variant = array; 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; if (_nestingLimit == 0) return DeserializationError::TooDeep;
--_nestingLimit; --_nestingLimit;
for (; n; --n) { for (; n; --n) {
@ -267,13 +267,13 @@ class MsgPackDeserializer {
} }
DeserializationError readObject(JsonVariant &variant, size_t n) { DeserializationError readObject(JsonVariant &variant, size_t n) {
JsonObject *object = new (_buffer) JsonObject(_buffer); JsonObject object(_buffer);
if (!object) return DeserializationError::NoMemory; if (object.isNull()) return DeserializationError::NoMemory;
variant = object; 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; if (_nestingLimit == 0) return DeserializationError::TooDeep;
--_nestingLimit; --_nestingLimit;
for (; n; --n) { for (; n; --n) {

View File

@ -140,7 +140,7 @@ class MsgPackSerializer {
writeByte(value ? 0xC3 : 0xC2); writeByte(value ? 0xC3 : 0xC2);
} }
void acceptUndefined() { void acceptNull() {
writeByte(0xC0); writeByte(0xC0);
} }

View File

@ -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&);
};
}
}

View File

@ -33,28 +33,26 @@ class StaticJsonDocument {
return _root.as<T>(); return _root.as<T>();
} }
// JsonObject& to<JsonObject>() // JsonObject to<JsonObject>()
template <typename T> template <typename T>
typename Internals::enable_if<Internals::is_same<T, JsonObject>::value, typename Internals::enable_if<Internals::is_same<T, JsonObject>::value,
JsonObject&>::type JsonObject>::type
to() { to() {
clear(); clear();
JsonObject* object = new (&_buffer) JsonObject(&_buffer); JsonObject object(&_buffer);
if (!object) return JsonObject::invalid();
_root = object; _root = object;
return *object; return object;
} }
// JsonArray& to<JsonArray>() // JsonArray to<JsonArray>()
template <typename T> template <typename T>
typename Internals::enable_if<Internals::is_same<T, JsonArray>::value, typename Internals::enable_if<Internals::is_same<T, JsonArray>::value,
JsonArray&>::type JsonArray>::type
to() { to() {
clear(); clear();
JsonArray* array = new (&_buffer) JsonArray(&_buffer); JsonArray array(&_buffer);
if (!array) return JsonArray::invalid();
_root = array; _root = array;
return *array; return array;
} }
// JsonVariant to<JsonVariant>() // JsonVariant to<JsonVariant>()

View File

@ -20,7 +20,7 @@ TEST_CASE("Gbathree") {
"\"measlights\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15," "\"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]],\"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]}"); "[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") { SECTION("Success") {
REQUIRE(error == DeserializationError::Ok); REQUIRE(error == DeserializationError::Ok);
@ -81,8 +81,8 @@ TEST_CASE("Gbathree") {
SECTION("Pulses") { SECTION("Pulses") {
// "pulses":[50,50,50] // "pulses":[50,50,50]
JsonArray& array = root["pulses"]; JsonArray array = root["pulses"];
REQUIRE(array.success()); REQUIRE(array.isNull() == false);
REQUIRE(3 == array.size()); REQUIRE(3 == array.size());
@ -94,8 +94,8 @@ TEST_CASE("Gbathree") {
SECTION("Act") { SECTION("Act") {
// "act":[2,1,2,2] // "act":[2,1,2,2]
JsonArray& array = root["act"]; JsonArray array = root["act"];
REQUIRE(array.success()); REQUIRE(array.isNull() == false);
REQUIRE(4 == array.size()); REQUIRE(4 == array.size());
REQUIRE(2 == array[0]); REQUIRE(2 == array[0]);
@ -107,12 +107,12 @@ TEST_CASE("Gbathree") {
SECTION("Detectors") { SECTION("Detectors") {
// "detectors":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]] // "detectors":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]]
JsonArray& array = root["detectors"]; JsonArray array = root["detectors"];
REQUIRE(array.success()); REQUIRE(array.isNull() == false);
REQUIRE(4 == array.size()); REQUIRE(4 == array.size());
for (size_t i = 0; i < 4; i++) { for (size_t i = 0; i < 4; i++) {
JsonArray& nestedArray = array[i]; JsonArray nestedArray = array[i];
REQUIRE(4 == nestedArray.size()); REQUIRE(4 == nestedArray.size());
for (size_t j = 0; j < 4; j++) { for (size_t j = 0; j < 4; j++) {
@ -124,8 +124,8 @@ TEST_CASE("Gbathree") {
SECTION("Alta") { SECTION("Alta") {
// alta:[2,2,2,2] // alta:[2,2,2,2]
JsonArray& array = root["alta"]; JsonArray array = root["alta"];
REQUIRE(array.success()); REQUIRE(array.isNull() == false);
REQUIRE(4 == array.size()); REQUIRE(4 == array.size());
@ -137,8 +137,8 @@ TEST_CASE("Gbathree") {
SECTION("Altb") { SECTION("Altb") {
// altb:[2,2,2,2] // altb:[2,2,2,2]
JsonArray& array = root["altb"]; JsonArray array = root["altb"];
REQUIRE(array.success()); REQUIRE(array.isNull() == false);
REQUIRE(4 == array.size()); REQUIRE(4 == array.size());
@ -150,12 +150,12 @@ TEST_CASE("Gbathree") {
SECTION("Measlights") { SECTION("Measlights") {
// "measlights":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]] // "measlights":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]]
JsonArray& array = root["measlights"]; JsonArray array = root["measlights"];
REQUIRE(array.success()); REQUIRE(array.isNull() == false);
REQUIRE(4 == array.size()); REQUIRE(4 == array.size());
for (size_t i = 0; i < 4; i++) { for (size_t i = 0; i < 4; i++) {
JsonArray& nestedArray = array[i]; JsonArray nestedArray = array[i];
REQUIRE(4 == nestedArray.size()); REQUIRE(4 == nestedArray.size());
@ -168,12 +168,12 @@ TEST_CASE("Gbathree") {
SECTION("Measlights2") { SECTION("Measlights2") {
// "measlights2":[[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]]
JsonArray& array = root["measlights2"]; JsonArray array = root["measlights2"];
REQUIRE(array.success()); REQUIRE(array.isNull() == false);
REQUIRE(4 == array.size()); REQUIRE(4 == array.size());
for (size_t i = 0; i < 4; i++) { for (size_t i = 0; i < 4; i++) {
JsonArray& nestedArray = array[i]; JsonArray nestedArray = array[i];
REQUIRE(4 == nestedArray.size()); REQUIRE(4 == nestedArray.size());
for (size_t j = 0; j < 4; j++) { for (size_t j = 0; j < 4; j++) {
@ -185,8 +185,8 @@ TEST_CASE("Gbathree") {
SECTION("Altc") { SECTION("Altc") {
// altc:[2,2,2,2] // altc:[2,2,2,2]
JsonArray& array = root["altc"]; JsonArray array = root["altc"];
REQUIRE(array.success()); REQUIRE(array.isNull() == false);
REQUIRE(4 == array.size()); REQUIRE(4 == array.size());
@ -198,8 +198,8 @@ TEST_CASE("Gbathree") {
SECTION("Altd") { SECTION("Altd") {
// altd:[2,2,2,2] // altd:[2,2,2,2]
JsonArray& array = root["altd"]; JsonArray array = root["altd"];
REQUIRE(array.success()); REQUIRE(array.isNull() == false);
REQUIRE(4 == array.size()); REQUIRE(4 == array.size());

View File

@ -7,7 +7,7 @@
TEST_CASE("JsonArray::add()") { TEST_CASE("JsonArray::add()") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& _array = doc.to<JsonArray>(); JsonArray _array = doc.to<JsonArray>();
SECTION("int") { SECTION("int") {
_array.add(123); _array.add(123);
@ -40,30 +40,30 @@ TEST_CASE("JsonArray::add()") {
SECTION("nested array") { SECTION("nested array") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>(); JsonArray arr = doc2.to<JsonArray>();
_array.add(arr); _array.add(arr);
REQUIRE(&arr == &_array[0].as<JsonArray&>()); REQUIRE(arr == _array[0].as<JsonArray>());
REQUIRE(_array[0].is<JsonArray&>()); REQUIRE(_array[0].is<JsonArray>());
REQUIRE_FALSE(_array[0].is<int>()); REQUIRE_FALSE(_array[0].is<int>());
} }
SECTION("nested object") { SECTION("nested object") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonObject& obj = doc2.to<JsonObject>(); JsonObject obj = doc2.to<JsonObject>();
_array.add(obj); _array.add(obj);
REQUIRE(&obj == &_array[0].as<JsonObject&>()); REQUIRE(obj == _array[0].as<JsonObject>());
REQUIRE(_array[0].is<JsonObject&>()); REQUIRE(_array[0].is<JsonObject>());
REQUIRE_FALSE(_array[0].is<int>()); REQUIRE_FALSE(_array[0].is<int>());
} }
SECTION("array subscript") { SECTION("array subscript") {
const char* str = "hello"; const char* str = "hello";
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>(); JsonArray arr = doc2.to<JsonArray>();
arr.add(str); arr.add(str);
_array.add(arr[0]); _array.add(arr[0]);
@ -74,7 +74,7 @@ TEST_CASE("JsonArray::add()") {
SECTION("object subscript") { SECTION("object subscript") {
const char* str = "hello"; const char* str = "hello";
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonObject& obj = doc2.to<JsonObject>(); JsonObject obj = doc2.to<JsonObject>();
obj["x"] = str; obj["x"] = str;
_array.add(obj["x"]); _array.add(obj["x"]);

View File

@ -7,10 +7,10 @@
TEST_CASE("JsonArray basics") { TEST_CASE("JsonArray basics") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
SECTION("SuccessIsTrue") { SECTION("isNull()") {
REQUIRE(array.success()); REQUIRE(array.isNull() == false);
} }
SECTION("InitialSizeIsZero") { SECTION("InitialSizeIsZero") {
@ -18,12 +18,12 @@ TEST_CASE("JsonArray basics") {
} }
SECTION("CreateNestedArray") { SECTION("CreateNestedArray") {
JsonArray& arr = array.createNestedArray(); JsonArray arr = array.createNestedArray();
REQUIRE(&arr == &array[0].as<JsonArray&>()); REQUIRE(arr == array[0].as<JsonArray>());
} }
SECTION("CreateNestedObject") { SECTION("CreateNestedObject") {
JsonObject& obj = array.createNestedObject(); JsonObject obj = array.createNestedObject();
REQUIRE(&obj == &array[0].as<JsonObject&>()); REQUIRE(obj == array[0].as<JsonObject>());
} }
} }

View File

@ -8,7 +8,7 @@
TEST_CASE("JsonArray::copyFrom()") { TEST_CASE("JsonArray::copyFrom()") {
SECTION("OneDimension") { SECTION("OneDimension") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
char json[32]; char json[32];
int source[] = {1, 2, 3}; int source[] = {1, 2, 3};
@ -22,7 +22,7 @@ TEST_CASE("JsonArray::copyFrom()") {
SECTION("OneDimension_JsonBufferTooSmall") { SECTION("OneDimension_JsonBufferTooSmall") {
const size_t SIZE = JSON_ARRAY_SIZE(2); const size_t SIZE = JSON_ARRAY_SIZE(2);
StaticJsonDocument<SIZE> doc; StaticJsonDocument<SIZE> doc;
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
char json[32]; char json[32];
int source[] = {1, 2, 3}; int source[] = {1, 2, 3};
@ -35,7 +35,7 @@ TEST_CASE("JsonArray::copyFrom()") {
SECTION("TwoDimensions") { SECTION("TwoDimensions") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
char json[32]; char json[32];
int source[][3] = {{1, 2, 3}, {4, 5, 6}}; int source[][3] = {{1, 2, 3}, {4, 5, 6}};
@ -50,7 +50,7 @@ TEST_CASE("JsonArray::copyFrom()") {
const size_t SIZE = const size_t SIZE =
JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2); JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
StaticJsonDocument<SIZE> doc; StaticJsonDocument<SIZE> doc;
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
char json[32]; char json[32];
int source[][3] = {{1, 2, 3}, {4, 5, 6}}; int source[][3] = {{1, 2, 3}, {4, 5, 6}};

View File

@ -12,7 +12,7 @@ TEST_CASE("JsonArray::copyTo()") {
char json[] = "[1,2,3]"; char json[] = "[1,2,3]";
DeserializationError err = deserializeJson(doc, json); DeserializationError err = deserializeJson(doc, json);
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
JsonArray& array = doc.as<JsonArray>(); JsonArray array = doc.as<JsonArray>();
int destination[4] = {0}; int destination[4] = {0};
size_t result = array.copyTo(destination); size_t result = array.copyTo(destination);
@ -28,7 +28,7 @@ TEST_CASE("JsonArray::copyTo()") {
char json[] = "[1,2,3]"; char json[] = "[1,2,3]";
DeserializationError err = deserializeJson(doc, json); DeserializationError err = deserializeJson(doc, json);
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
JsonArray& array = doc.as<JsonArray>(); JsonArray array = doc.as<JsonArray>();
int destination[2] = {0}; int destination[2] = {0};
size_t result = array.copyTo(destination); size_t result = array.copyTo(destination);
@ -43,7 +43,7 @@ TEST_CASE("JsonArray::copyTo()") {
DeserializationError err = deserializeJson(doc, json); DeserializationError err = deserializeJson(doc, json);
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
JsonArray& array = doc.as<JsonArray>(); JsonArray array = doc.as<JsonArray>();
int destination[3][2] = {{0}}; int destination[3][2] = {{0}};
array.copyTo(destination); array.copyTo(destination);

View File

@ -7,28 +7,29 @@
using namespace Catch::Matchers; using namespace Catch::Matchers;
TEST_CASE("JsonArray::invalid()") { TEST_CASE("Undefined JsonArray") {
JsonArray array;
SECTION("SubscriptFails") { SECTION("SubscriptFails") {
REQUIRE_FALSE(JsonArray::invalid()[0].success()); REQUIRE(array[0].isNull());
} }
SECTION("AddFails") { SECTION("AddFails") {
JsonArray& array = JsonArray::invalid();
array.add(1); array.add(1);
REQUIRE(0 == array.size()); REQUIRE(0 == array.size());
} }
SECTION("CreateNestedArrayFails") { SECTION("CreateNestedArrayFails") {
REQUIRE_FALSE(JsonArray::invalid().createNestedArray().success()); REQUIRE(array.createNestedArray().isNull());
} }
SECTION("CreateNestedObjectFails") { SECTION("CreateNestedObjectFails") {
REQUIRE_FALSE(JsonArray::invalid().createNestedObject().success()); REQUIRE(array.createNestedObject().isNull());
} }
SECTION("PrintToWritesBrackets") { SECTION("PrintToWritesBrackets") {
char buffer[32]; char buffer[32];
serializeJson(JsonArray::invalid(), buffer, sizeof(buffer)); serializeJson(array, buffer, sizeof(buffer));
REQUIRE_THAT(buffer, Equals("[]")); REQUIRE_THAT(buffer, Equals("null"));
} }
} }

View File

@ -8,7 +8,7 @@
template <typename TIterator> template <typename TIterator>
static void run_iterator_test() { static void run_iterator_test() {
StaticJsonDocument<JSON_ARRAY_SIZE(2)> doc; StaticJsonDocument<JSON_ARRAY_SIZE(2)> doc;
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
array.add(12); array.add(12);
array.add(34); array.add(34);

View File

@ -7,7 +7,7 @@
TEST_CASE("JsonArray::remove()") { TEST_CASE("JsonArray::remove()") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& _array = doc.to<JsonArray>(); JsonArray _array = doc.to<JsonArray>();
_array.add(1); _array.add(1);
_array.add(2); _array.add(2);
_array.add(3); _array.add(3);

View File

@ -9,7 +9,7 @@ using namespace Catch::Matchers;
TEST_CASE("JsonArray::set()") { TEST_CASE("JsonArray::set()") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& _array = doc.to<JsonArray>(); JsonArray _array = doc.to<JsonArray>();
_array.add(0); _array.add(0);
SECTION("int") { SECTION("int") {
@ -42,29 +42,29 @@ TEST_CASE("JsonArray::set()") {
SECTION("nested array") { SECTION("nested array") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>(); JsonArray arr = doc2.to<JsonArray>();
_array.set(0, arr); _array.set(0, arr);
REQUIRE(&arr == &_array[0].as<JsonArray&>()); REQUIRE(arr == _array[0].as<JsonArray>());
REQUIRE(_array[0].is<JsonArray&>()); REQUIRE(_array[0].is<JsonArray>());
REQUIRE_FALSE(_array[0].is<int>()); REQUIRE_FALSE(_array[0].is<int>());
} }
SECTION("nested object") { SECTION("nested object") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonObject& obj = doc2.to<JsonObject>(); JsonObject obj = doc2.to<JsonObject>();
_array.set(0, obj); _array.set(0, obj);
REQUIRE(&obj == &_array[0].as<JsonObject&>()); REQUIRE(obj == _array[0].as<JsonObject>());
REQUIRE(_array[0].is<JsonObject&>()); REQUIRE(_array[0].is<JsonObject>());
REQUIRE_FALSE(_array[0].is<int>()); REQUIRE_FALSE(_array[0].is<int>());
} }
SECTION("array subscript") { SECTION("array subscript") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>(); JsonArray arr = doc2.to<JsonArray>();
arr.add("hello"); arr.add("hello");
_array.set(0, arr[0]); _array.set(0, arr[0]);
@ -74,7 +74,7 @@ TEST_CASE("JsonArray::set()") {
SECTION("object subscript") { SECTION("object subscript") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonObject& obj = doc2.to<JsonObject>(); JsonObject obj = doc2.to<JsonObject>();
obj["x"] = "hello"; obj["x"] = "hello";
_array.set(0, obj["x"]); _array.set(0, obj["x"]);

View File

@ -7,7 +7,7 @@
TEST_CASE("JsonArray::size()") { TEST_CASE("JsonArray::size()") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& _array = doc.to<JsonArray>(); JsonArray _array = doc.to<JsonArray>();
SECTION("increases after add()") { SECTION("increases after add()") {
_array.add("hello"); _array.add("hello");

View File

@ -12,7 +12,7 @@ static void eraseString(std::string &str) {
TEST_CASE("std::string") { TEST_CASE("std::string") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray &array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
SECTION("add()") { SECTION("add()") {
std::string value("hello"); std::string value("hello");

View File

@ -8,7 +8,7 @@
TEST_CASE("JsonArray::operator[]") { TEST_CASE("JsonArray::operator[]") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& _array = doc.to<JsonArray>(); JsonArray _array = doc.to<JsonArray>();
_array.add(0); _array.add(0);
SECTION("int") { SECTION("int") {
@ -53,35 +53,33 @@ TEST_CASE("JsonArray::operator[]") {
SECTION("nested array") { SECTION("nested array") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>(); JsonArray arr = doc2.to<JsonArray>();
_array[0] = arr; _array[0] = arr;
REQUIRE(&arr == &_array[0].as<JsonArray&>()); REQUIRE(arr == _array[0].as<JsonArray>());
REQUIRE(&arr == &_array[0].as<JsonArray>()); // <- short hand REQUIRE(arr == _array[0].as<JsonArray>()); // <- short hand
REQUIRE(&arr == &_array[0].as<const JsonArray&>()); // REQUIRE(arr == _array[0].as<const JsonArray>());
REQUIRE(&arr == &_array[0].as<const JsonArray>()); // <- short hand // REQUIRE(arr == _array[0].as<const JsonArray>()); // <- short hand
REQUIRE(true == _array[0].is<JsonArray&>()); REQUIRE(true == _array[0].is<JsonArray>());
REQUIRE(false == _array[0].is<int>()); REQUIRE(false == _array[0].is<int>());
} }
SECTION("nested object") { SECTION("nested object") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonObject& obj = doc2.to<JsonObject>(); JsonObject obj = doc2.to<JsonObject>();
_array[0] = obj; _array[0] = obj;
REQUIRE(&obj == &_array[0].as<JsonObject&>()); REQUIRE(obj == _array[0].as<JsonObject>());
REQUIRE(&obj == &_array[0].as<JsonObject>()); // <- short hand REQUIRE(obj == _array[0].as<const JsonObject>()); // <- short hand
REQUIRE(&obj == &_array[0].as<const JsonObject&>()); REQUIRE(true == _array[0].is<JsonObject>());
REQUIRE(&obj == &_array[0].as<const JsonObject>()); // <- short hand
REQUIRE(true == _array[0].is<JsonObject&>());
REQUIRE(false == _array[0].is<int>()); REQUIRE(false == _array[0].is<int>());
} }
SECTION("array subscript") { SECTION("array subscript") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>(); JsonArray arr = doc2.to<JsonArray>();
const char* str = "hello"; const char* str = "hello";
arr.add(str); arr.add(str);
@ -94,7 +92,7 @@ TEST_CASE("JsonArray::operator[]") {
SECTION("object subscript") { SECTION("object subscript") {
const char* str = "hello"; const char* str = "hello";
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonObject& obj = doc2.to<JsonObject>(); JsonObject obj = doc2.to<JsonObject>();
obj["x"] = str; obj["x"] = str;

View File

@ -10,7 +10,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("An empty array") { SECTION("An empty array") {
DeserializationError err = deserializeJson(doc, "[]"); DeserializationError err = deserializeJson(doc, "[]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(0 == arr.size()); REQUIRE(0 == arr.size());
@ -19,7 +19,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Spaces") { SECTION("Spaces") {
SECTION("Before the opening bracket") { SECTION("Before the opening bracket") {
DeserializationError err = deserializeJson(doc, " []"); DeserializationError err = deserializeJson(doc, " []");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(0 == arr.size()); REQUIRE(0 == arr.size());
@ -27,7 +27,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Before first value") { SECTION("Before first value") {
DeserializationError err = deserializeJson(doc, "[ \t\r\n42]"); DeserializationError err = deserializeJson(doc, "[ \t\r\n42]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size()); REQUIRE(1 == arr.size());
@ -36,7 +36,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("After first value") { SECTION("After first value") {
DeserializationError err = deserializeJson(doc, "[42 \t\r\n]"); DeserializationError err = deserializeJson(doc, "[42 \t\r\n]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size()); REQUIRE(1 == arr.size());
@ -47,7 +47,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Values types") { SECTION("Values types") {
SECTION("On integer") { SECTION("On integer") {
DeserializationError err = deserializeJson(doc, "[42]"); DeserializationError err = deserializeJson(doc, "[42]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size()); REQUIRE(1 == arr.size());
@ -56,7 +56,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Two integers") { SECTION("Two integers") {
DeserializationError err = deserializeJson(doc, "[42,84]"); DeserializationError err = deserializeJson(doc, "[42,84]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size()); REQUIRE(2 == arr.size());
@ -66,7 +66,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Double") { SECTION("Double") {
DeserializationError err = deserializeJson(doc, "[4.2,1e2]"); DeserializationError err = deserializeJson(doc, "[4.2,1e2]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size()); REQUIRE(2 == arr.size());
@ -76,7 +76,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Unsigned long") { SECTION("Unsigned long") {
DeserializationError err = deserializeJson(doc, "[4294967295]"); DeserializationError err = deserializeJson(doc, "[4294967295]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size()); REQUIRE(1 == arr.size());
@ -85,7 +85,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Boolean") { SECTION("Boolean") {
DeserializationError err = deserializeJson(doc, "[true,false]"); DeserializationError err = deserializeJson(doc, "[true,false]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size()); REQUIRE(2 == arr.size());
@ -95,7 +95,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Null") { SECTION("Null") {
DeserializationError err = deserializeJson(doc, "[null,null]"); DeserializationError err = deserializeJson(doc, "[null,null]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size()); REQUIRE(2 == arr.size());
@ -108,7 +108,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Double quotes") { SECTION("Double quotes") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "[ \"hello\" , \"world\" ]"); deserializeJson(doc, "[ \"hello\" , \"world\" ]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size()); REQUIRE(2 == arr.size());
@ -118,7 +118,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Single quotes") { SECTION("Single quotes") {
DeserializationError err = deserializeJson(doc, "[ 'hello' , 'world' ]"); DeserializationError err = deserializeJson(doc, "[ 'hello' , 'world' ]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size()); REQUIRE(2 == arr.size());
@ -128,7 +128,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("No quotes") { SECTION("No quotes") {
DeserializationError err = deserializeJson(doc, "[ hello , world ]"); DeserializationError err = deserializeJson(doc, "[ hello , world ]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size()); REQUIRE(2 == arr.size());
@ -138,7 +138,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Double quotes (empty strings)") { SECTION("Double quotes (empty strings)") {
DeserializationError err = deserializeJson(doc, "[\"\",\"\"]"); DeserializationError err = deserializeJson(doc, "[\"\",\"\"]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size()); REQUIRE(2 == arr.size());
@ -148,7 +148,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Single quotes (empty strings)") { SECTION("Single quotes (empty strings)") {
DeserializationError err = deserializeJson(doc, "[\'\',\'\']"); DeserializationError err = deserializeJson(doc, "[\'\',\'\']");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size()); REQUIRE(2 == arr.size());
@ -179,7 +179,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Before opening bracket") { SECTION("Before opening bracket") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "/*COMMENT*/ [\"hello\"]"); deserializeJson(doc, "/*COMMENT*/ [\"hello\"]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size()); REQUIRE(1 == arr.size());
@ -189,7 +189,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("After opening bracket") { SECTION("After opening bracket") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "[/*COMMENT*/ \"hello\"]"); deserializeJson(doc, "[/*COMMENT*/ \"hello\"]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size()); REQUIRE(1 == arr.size());
@ -198,7 +198,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Before closing bracket") { SECTION("Before closing bracket") {
DeserializationError err = deserializeJson(doc, "[\"hello\"/*COMMENT*/]"); DeserializationError err = deserializeJson(doc, "[\"hello\"/*COMMENT*/]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size()); REQUIRE(1 == arr.size());
@ -207,7 +207,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("After closing bracket") { SECTION("After closing bracket") {
DeserializationError err = deserializeJson(doc, "[\"hello\"]/*COMMENT*/"); DeserializationError err = deserializeJson(doc, "[\"hello\"]/*COMMENT*/");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size()); REQUIRE(1 == arr.size());
@ -217,7 +217,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Before comma") { SECTION("Before comma") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "[\"hello\"/*COMMENT*/,\"world\"]"); deserializeJson(doc, "[\"hello\"/*COMMENT*/,\"world\"]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size()); REQUIRE(2 == arr.size());
@ -228,7 +228,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("After comma") { SECTION("After comma") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "[\"hello\",/*COMMENT*/ \"world\"]"); deserializeJson(doc, "[\"hello\",/*COMMENT*/ \"world\"]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size()); REQUIRE(2 == arr.size());
@ -256,7 +256,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Before opening bracket") { SECTION("Before opening bracket") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "//COMMENT\n\t[\"hello\"]"); deserializeJson(doc, "//COMMENT\n\t[\"hello\"]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size()); REQUIRE(1 == arr.size());
@ -265,7 +265,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("After opening bracket") { SECTION("After opening bracket") {
DeserializationError err = deserializeJson(doc, "[//COMMENT\n\"hello\"]"); DeserializationError err = deserializeJson(doc, "[//COMMENT\n\"hello\"]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size()); REQUIRE(1 == arr.size());
@ -275,7 +275,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Before closing bracket") { SECTION("Before closing bracket") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "[\"hello\"//COMMENT\r\n]"); deserializeJson(doc, "[\"hello\"//COMMENT\r\n]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size()); REQUIRE(1 == arr.size());
@ -284,7 +284,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("After closing bracket") { SECTION("After closing bracket") {
DeserializationError err = deserializeJson(doc, "[\"hello\"]//COMMENT\n"); DeserializationError err = deserializeJson(doc, "[\"hello\"]//COMMENT\n");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size()); REQUIRE(1 == arr.size());
@ -294,7 +294,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Before comma") { SECTION("Before comma") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "[\"hello\"//COMMENT\n,\"world\"]"); deserializeJson(doc, "[\"hello\"//COMMENT\n,\"world\"]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size()); REQUIRE(2 == arr.size());
@ -305,7 +305,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("After comma") { SECTION("After comma") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "[\"hello\",//COMMENT\n\"world\"]"); deserializeJson(doc, "[\"hello\",//COMMENT\n\"world\"]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size()); REQUIRE(2 == arr.size());
@ -372,17 +372,17 @@ TEST_CASE("deserialize JSON array") {
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] "; " [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
DeserializationError err = deserializeJson(doc, jsonString); DeserializationError err = deserializeJson(doc, jsonString);
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
JsonObject& object1 = arr[0]; JsonObject object1 = arr[0];
const JsonObject& object2 = arr[1]; const JsonObject object2 = arr[1];
JsonObject& object3 = arr[2]; JsonObject object3 = arr[2];
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(true == object1.success()); REQUIRE(object1.isNull() == false);
REQUIRE(true == object2.success()); REQUIRE(object2.isNull() == false);
REQUIRE(false == object3.success()); REQUIRE(object3.isNull() == true);
REQUIRE(2 == object1.size()); REQUIRE(2 == object1.size());
REQUIRE(2 == object2.size()); REQUIRE(2 == object2.size());
@ -399,7 +399,7 @@ TEST_CASE("deserialize JSON array") {
SECTION("Should clear the JsonArray") { SECTION("Should clear the JsonArray") {
deserializeJson(doc, "[1,2,3,4]"); deserializeJson(doc, "[1,2,3,4]");
deserializeJson(doc, "[]"); deserializeJson(doc, "[]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(arr.size() == 0); REQUIRE(arr.size() == 0);
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0)); REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));

View File

@ -68,7 +68,7 @@ TEST_CASE("deserialize JSON array with a StaticJsonDocument") {
deserializeJson(doc, input); deserializeJson(doc, input);
deserializeJson(doc, "[]"); deserializeJson(doc, "[]");
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(arr.size() == 0); REQUIRE(arr.size() == 0);
REQUIRE(doc.memoryUsage() == JSON_ARRAY_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]"; char input[] = "[1,2]";
DeserializationError err = deserializeJson(doc, input); DeserializationError err = deserializeJson(doc, input);
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonArray>()); REQUIRE(doc.is<JsonArray>());

View File

@ -10,7 +10,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("An empty object") { SECTION("An empty object") {
DeserializationError err = deserializeJson(doc, "{}"); DeserializationError err = deserializeJson(doc, "{}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -20,7 +20,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Quotes") { SECTION("Quotes") {
SECTION("Double quotes") { SECTION("Double quotes") {
DeserializationError err = deserializeJson(doc, "{\"key\":\"value\"}"); DeserializationError err = deserializeJson(doc, "{\"key\":\"value\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -30,7 +30,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Single quotes") { SECTION("Single quotes") {
DeserializationError err = deserializeJson(doc, "{'key':'value'}"); DeserializationError err = deserializeJson(doc, "{'key':'value'}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -40,7 +40,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("No quotes") { SECTION("No quotes") {
DeserializationError err = deserializeJson(doc, "{key:value}"); DeserializationError err = deserializeJson(doc, "{key:value}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -50,7 +50,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("No quotes, allow underscore in key") { SECTION("No quotes, allow underscore in key") {
DeserializationError err = deserializeJson(doc, "{_k_e_y_:42}"); DeserializationError err = deserializeJson(doc, "{_k_e_y_:42}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -62,7 +62,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Spaces") { SECTION("Spaces") {
SECTION("Before the key") { SECTION("Before the key") {
DeserializationError err = deserializeJson(doc, "{ \"key\":\"value\"}"); DeserializationError err = deserializeJson(doc, "{ \"key\":\"value\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -72,7 +72,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("After the key") { SECTION("After the key") {
DeserializationError err = deserializeJson(doc, "{\"key\" :\"value\"}"); DeserializationError err = deserializeJson(doc, "{\"key\" :\"value\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -82,7 +82,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Before the value") { SECTION("Before the value") {
DeserializationError err = deserializeJson(doc, "{\"key\": \"value\"}"); DeserializationError err = deserializeJson(doc, "{\"key\": \"value\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -92,7 +92,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("After the value") { SECTION("After the value") {
DeserializationError err = deserializeJson(doc, "{\"key\":\"value\" }"); DeserializationError err = deserializeJson(doc, "{\"key\":\"value\" }");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -103,7 +103,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Before the colon") { SECTION("Before the colon") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}"); deserializeJson(doc, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -115,7 +115,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("After the colon") { SECTION("After the colon") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}"); deserializeJson(doc, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -129,7 +129,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("String") { SECTION("String") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"key1\":\"value1\",\"key2\":\"value2\"}"); deserializeJson(doc, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -141,7 +141,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Integer") { SECTION("Integer") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"key1\":42,\"key2\":-42}"); deserializeJson(doc, "{\"key1\":42,\"key2\":-42}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -153,7 +153,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Double") { SECTION("Double") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"key1\":12.345,\"key2\":-7E89}"); deserializeJson(doc, "{\"key1\":12.345,\"key2\":-7E89}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -165,7 +165,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Booleans") { SECTION("Booleans") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"key1\":true,\"key2\":false}"); deserializeJson(doc, "{\"key1\":true,\"key2\":false}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -177,7 +177,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Null") { SECTION("Null") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"key1\":null,\"key2\":null}"); deserializeJson(doc, "{\"key1\":null,\"key2\":null}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -190,17 +190,17 @@ TEST_CASE("deserialize JSON object") {
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } "; char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
DeserializationError err = deserializeJson(doc, jsonString); DeserializationError err = deserializeJson(doc, jsonString);
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
JsonArray& array1 = obj["ab"]; JsonArray array1 = obj["ab"];
const JsonArray& array2 = obj["cd"]; const JsonArray array2 = obj["cd"];
JsonArray& array3 = obj["ef"]; JsonArray array3 = obj["ef"];
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(true == array1.success()); REQUIRE(array1.isNull() == false);
REQUIRE(true == array2.success()); REQUIRE(array2.isNull() == false);
REQUIRE(false == array3.success()); REQUIRE(array3.isNull() == true);
REQUIRE(2 == array1.size()); REQUIRE(2 == array1.size());
REQUIRE(2 == array2.size()); REQUIRE(2 == array2.size());
@ -278,7 +278,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Before opening brace") { SECTION("Before opening brace") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "/*COMMENT*/ {\"hello\":\"world\"}"); deserializeJson(doc, "/*COMMENT*/ {\"hello\":\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -287,7 +287,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("After opening brace") { SECTION("After opening brace") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{/*COMMENT*/\"hello\":\"world\"}"); deserializeJson(doc, "{/*COMMENT*/\"hello\":\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -296,7 +296,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Before colon") { SECTION("Before colon") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"hello\"/*COMMENT*/:\"world\"}"); deserializeJson(doc, "{\"hello\"/*COMMENT*/:\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -305,7 +305,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("After colon") { SECTION("After colon") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"hello\":/*COMMENT*/\"world\"}"); deserializeJson(doc, "{\"hello\":/*COMMENT*/\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -314,7 +314,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Before closing brace") { SECTION("Before closing brace") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"hello\":\"world\"/*COMMENT*/}"); deserializeJson(doc, "{\"hello\":\"world\"/*COMMENT*/}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -323,7 +323,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("After closing brace") { SECTION("After closing brace") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"hello\":\"world\"}/*COMMENT*/"); deserializeJson(doc, "{\"hello\":\"world\"}/*COMMENT*/");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -332,7 +332,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Before comma") { SECTION("Before comma") {
DeserializationError err = deserializeJson( DeserializationError err = deserializeJson(
doc, "{\"hello\":\"world\"/*COMMENT*/,\"answer\":42}"); doc, "{\"hello\":\"world\"/*COMMENT*/,\"answer\":42}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -342,7 +342,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("After comma") { SECTION("After comma") {
DeserializationError err = deserializeJson( DeserializationError err = deserializeJson(
doc, "{\"hello\":\"world\",/*COMMENT*/\"answer\":42}"); doc, "{\"hello\":\"world\",/*COMMENT*/\"answer\":42}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -354,7 +354,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Before opening brace") { SECTION("Before opening brace") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "//COMMENT\n {\"hello\":\"world\"}"); deserializeJson(doc, "//COMMENT\n {\"hello\":\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -363,7 +363,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("After opening brace") { SECTION("After opening brace") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{//COMMENT\n\"hello\":\"world\"}"); deserializeJson(doc, "{//COMMENT\n\"hello\":\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -372,7 +372,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Before colon") { SECTION("Before colon") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"hello\"//COMMENT\n:\"world\"}"); deserializeJson(doc, "{\"hello\"//COMMENT\n:\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -381,7 +381,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("After colon") { SECTION("After colon") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"hello\"://COMMENT\n\"world\"}"); deserializeJson(doc, "{\"hello\"://COMMENT\n\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -390,7 +390,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Before closing brace") { SECTION("Before closing brace") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"hello\":\"world\"//COMMENT\n}"); deserializeJson(doc, "{\"hello\":\"world\"//COMMENT\n}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -399,7 +399,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("After closing brace") { SECTION("After closing brace") {
DeserializationError err = DeserializationError err =
deserializeJson(doc, "{\"hello\":\"world\"}//COMMENT\n"); deserializeJson(doc, "{\"hello\":\"world\"}//COMMENT\n");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -408,7 +408,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Before comma") { SECTION("Before comma") {
DeserializationError err = deserializeJson( DeserializationError err = deserializeJson(
doc, "{\"hello\":\"world\"//COMMENT\n,\"answer\":42}"); doc, "{\"hello\":\"world\"//COMMENT\n,\"answer\":42}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -418,7 +418,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("After comma") { SECTION("After comma") {
DeserializationError err = deserializeJson( DeserializationError err = deserializeJson(
doc, "{\"hello\":\"world\",//COMMENT\n\"answer\":42}"); doc, "{\"hello\":\"world\",//COMMENT\n\"answer\":42}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -459,7 +459,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("After closing brace") { SECTION("After closing brace") {
DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"}/"); DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"}/");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(obj["hello"] == "world"); REQUIRE(obj["hello"] == "world");
@ -483,7 +483,7 @@ TEST_CASE("deserialize JSON object") {
SECTION("Should clear the JsonObject") { SECTION("Should clear the JsonObject") {
deserializeJson(doc, "{\"hello\":\"world\"}"); deserializeJson(doc, "{\"hello\":\"world\"}");
deserializeJson(doc, "{}"); deserializeJson(doc, "{}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 0); REQUIRE(obj.size() == 0);

View File

@ -13,7 +13,7 @@ TEST_CASE("deserializeJson(std::istream&)") {
std::istringstream json(" [ 42 /* comment */ ] "); std::istringstream json(" [ 42 /* comment */ ] ");
DeserializationError err = deserializeJson(doc, json); DeserializationError err = deserializeJson(doc, json);
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == arr.size()); REQUIRE(1 == arr.size());
@ -24,7 +24,7 @@ TEST_CASE("deserializeJson(std::istream&)") {
std::istringstream json(" { hello : world // comment\n }"); std::istringstream json(" { hello : world // comment\n }");
DeserializationError err = deserializeJson(doc, json); DeserializationError err = deserializeJson(doc, json);
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == obj.size()); REQUIRE(1 == obj.size());

View File

@ -28,7 +28,7 @@ TEST_CASE("deserializeJson(const std::string&)") {
DeserializationError err = deserializeJson(doc, input); DeserializationError err = deserializeJson(doc, input);
input[2] = 'X'; // alter the string tomake sure we made a copy 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(err == DeserializationError::Ok);
REQUIRE(std::string("hello") == array[0]); REQUIRE(std::string("hello") == array[0]);
} }

View File

@ -7,9 +7,9 @@
TEST_CASE("JsonObject basics") { TEST_CASE("JsonObject basics") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
SECTION("SuccessIsTrue") { SECTION("isNull()") {
REQUIRE(obj.success()); REQUIRE(obj.isNull() == false);
} }
} }

View File

@ -7,7 +7,7 @@
TEST_CASE("JsonObject::containsKey()") { TEST_CASE("JsonObject::containsKey()") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
SECTION("ContainsKeyReturnsFalseForNonExistingKey") { SECTION("ContainsKeyReturnsFalseForNonExistingKey") {
obj.set("hello", 42); obj.set("hello", 42);

View File

@ -9,7 +9,7 @@ using namespace Catch::Matchers;
TEST_CASE("JsonObject::get()") { TEST_CASE("JsonObject::get()") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
SECTION("GetConstCharPointer_GivenStringLiteral") { SECTION("GetConstCharPointer_GivenStringLiteral") {
obj.set("hello", "world"); obj.set("hello", "world");

View File

@ -8,10 +8,10 @@
using namespace Catch::Matchers; using namespace Catch::Matchers;
TEST_CASE("JsonObject::invalid()") { TEST_CASE("JsonObject::invalid()") {
JsonObject& obj = JsonObject::invalid(); JsonObject obj;
SECTION("SubscriptFails") { SECTION("SubscriptFails") {
REQUIRE_FALSE(obj["key"].success()); REQUIRE(obj["key"].isNull());
} }
SECTION("AddFails") { SECTION("AddFails") {
@ -20,16 +20,16 @@ TEST_CASE("JsonObject::invalid()") {
} }
SECTION("CreateNestedArrayFails") { SECTION("CreateNestedArrayFails") {
REQUIRE_FALSE(obj.createNestedArray("hello").success()); REQUIRE(obj.createNestedArray("hello").isNull());
} }
SECTION("CreateNestedObjectFails") { SECTION("CreateNestedObjectFails") {
REQUIRE_FALSE(obj.createNestedObject("world").success()); REQUIRE(obj.createNestedObject("world").isNull());
} }
SECTION("PrintToWritesBraces") { SECTION("serialize to 'null'") {
char buffer[32]; char buffer[32];
serializeJson(obj, buffer, sizeof(buffer)); serializeJson(obj, buffer, sizeof(buffer));
REQUIRE_THAT(buffer, Equals("{}")); REQUIRE_THAT(buffer, Equals("null"));
} }
} }

View File

@ -9,7 +9,7 @@ using namespace Catch::Matchers;
TEST_CASE("JsonObject::begin()/end()") { TEST_CASE("JsonObject::begin()/end()") {
StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc; StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj["ab"] = 12; obj["ab"] = 12;
obj["cd"] = 34; obj["cd"] = 34;
@ -35,7 +35,7 @@ TEST_CASE("JsonObject::begin()/end()") {
} }
SECTION("ConstIterator") { SECTION("ConstIterator") {
const JsonObject& const_object = obj; const JsonObject const_object = obj;
JsonObject::const_iterator it = const_object.begin(); JsonObject::const_iterator it = const_object.begin();
REQUIRE(const_object.end() != it); REQUIRE(const_object.end() != it);

View File

@ -8,7 +8,7 @@
TEST_CASE("JsonObject::remove()") { TEST_CASE("JsonObject::remove()") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
SECTION("SizeDecreased_WhenValuesAreRemoved") { SECTION("SizeDecreased_WhenValuesAreRemoved") {
obj["hello"] = 1; obj["hello"] = 1;

View File

@ -8,7 +8,7 @@
TEST_CASE("JsonObject::set()") { TEST_CASE("JsonObject::set()") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
SECTION("int") { SECTION("int") {
obj.set("hello", 123); obj.set("hello", 123);
@ -44,29 +44,29 @@ TEST_CASE("JsonObject::set()") {
SECTION("nested array") { SECTION("nested array") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>(); JsonArray arr = doc2.to<JsonArray>();
obj.set("hello", arr); obj.set("hello", arr);
REQUIRE(&arr == &obj["hello"].as<JsonArray>()); REQUIRE(arr == obj["hello"].as<JsonArray>());
REQUIRE(obj["hello"].is<JsonArray&>()); REQUIRE(obj["hello"].is<JsonArray>());
REQUIRE_FALSE(obj["hello"].is<JsonObject&>()); REQUIRE_FALSE(obj["hello"].is<JsonObject>());
} }
SECTION("nested object") { SECTION("nested object") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonObject& obj2 = doc2.to<JsonObject>(); JsonObject obj2 = doc2.to<JsonObject>();
obj.set("hello", obj2); obj.set("hello", obj2);
REQUIRE(&obj2 == &obj["hello"].as<JsonObject>()); REQUIRE(obj2 == obj["hello"].as<JsonObject>());
REQUIRE(obj["hello"].is<JsonObject&>()); REQUIRE(obj["hello"].is<JsonObject>());
REQUIRE_FALSE(obj["hello"].is<JsonArray&>()); REQUIRE_FALSE(obj["hello"].is<JsonArray>());
} }
SECTION("array subscript") { SECTION("array subscript") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>(); JsonArray arr = doc2.to<JsonArray>();
arr.add(42); arr.add(42);
obj.set("a", arr[0]); obj.set("a", arr[0]);
@ -76,7 +76,7 @@ TEST_CASE("JsonObject::set()") {
SECTION("object subscript") { SECTION("object subscript") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonObject& obj2 = doc2.to<JsonObject>(); JsonObject obj2 = doc2.to<JsonObject>();
obj2.set("x", 42); obj2.set("x", 42);
obj.set("a", obj2["x"]); obj.set("a", obj2["x"]);
@ -86,14 +86,14 @@ TEST_CASE("JsonObject::set()") {
SECTION("returns true when allocation succeeds") { SECTION("returns true when allocation succeeds") {
StaticJsonDocument<JSON_OBJECT_SIZE(1) + 15> doc2; 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"))); REQUIRE(true == obj2.set(std::string("hello"), std::string("world")));
} }
SECTION("returns false when allocation fails") { SECTION("returns false when allocation fails") {
StaticJsonDocument<JSON_OBJECT_SIZE(1) + 10> doc2; 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"))); REQUIRE(false == obj2.set(std::string("hello"), std::string("world")));
} }

View File

@ -8,7 +8,7 @@
TEST_CASE("JsonObject::size()") { TEST_CASE("JsonObject::size()") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
SECTION("initial size is zero") { SECTION("initial size is zero") {
REQUIRE(0 == obj.size()); REQUIRE(0 == obj.size());

View File

@ -17,7 +17,7 @@ TEST_CASE("std::string") {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
deserializeJson(doc, json); deserializeJson(doc, json);
JsonObject &obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(std::string("value") == obj[std::string("key")]); REQUIRE(std::string("value") == obj[std::string("key")]);
} }
@ -26,13 +26,13 @@ TEST_CASE("std::string") {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
deserializeJson(doc, json); deserializeJson(doc, json);
JsonObject &obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(std::string("value") == obj[std::string("key")]); REQUIRE(std::string("value") == obj[std::string("key")]);
} }
SECTION("set(key)") { SECTION("set(key)") {
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
std::string key("hello"); std::string key("hello");
obj.set(key, "world"); obj.set(key, "world");
eraseString(key); eraseString(key);
@ -40,7 +40,7 @@ TEST_CASE("std::string") {
} }
SECTION("set(value)") { SECTION("set(value)") {
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
std::string value("world"); std::string value("world");
obj.set("hello", value); obj.set("hello", value);
eraseString(value); eraseString(value);
@ -48,7 +48,7 @@ TEST_CASE("std::string") {
} }
SECTION("set(key,value)") { SECTION("set(key,value)") {
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
std::string key("hello"); std::string key("hello");
std::string value("world"); std::string value("world");
obj.set(key, value); obj.set(key, value);
@ -58,9 +58,9 @@ TEST_CASE("std::string") {
} }
SECTION("set(JsonArraySubscript)") { SECTION("set(JsonArraySubscript)") {
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonArray &arr = doc2.to<JsonArray>(); JsonArray arr = doc2.to<JsonArray>();
arr.add("world"); arr.add("world");
obj.set(std::string("hello"), arr[0]); obj.set(std::string("hello"), arr[0]);
@ -69,9 +69,9 @@ TEST_CASE("std::string") {
} }
SECTION("set(JsonObjectSubscript)") { SECTION("set(JsonObjectSubscript)") {
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonObject &obj2 = doc2.to<JsonObject>(); JsonObject obj2 = doc2.to<JsonObject>();
obj2.set("x", "world"); obj2.set("x", "world");
obj.set(std::string("hello"), obj2["x"]); obj.set(std::string("hello"), obj2["x"]);
@ -82,7 +82,7 @@ TEST_CASE("std::string") {
SECTION("get<T>()") { SECTION("get<T>()") {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
deserializeJson(doc, json); deserializeJson(doc, json);
JsonObject &obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(std::string("value") == obj.get<const char *>(std::string("key"))); REQUIRE(std::string("value") == obj.get<const char *>(std::string("key")));
} }
@ -90,13 +90,13 @@ TEST_CASE("std::string") {
SECTION("is<T>()") { SECTION("is<T>()") {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
deserializeJson(doc, json); deserializeJson(doc, json);
JsonObject &obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(true == obj.is<const char *>(std::string("key"))); REQUIRE(true == obj.is<const char *>(std::string("key")));
} }
SECTION("createNestedObject()") { SECTION("createNestedObject()") {
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
std::string key = "key"; std::string key = "key";
char json[64]; char json[64];
obj.createNestedObject(key); obj.createNestedObject(key);
@ -106,7 +106,7 @@ TEST_CASE("std::string") {
} }
SECTION("createNestedArray()") { SECTION("createNestedArray()") {
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
std::string key = "key"; std::string key = "key";
char json[64]; char json[64];
obj.createNestedArray(key); obj.createNestedArray(key);
@ -118,12 +118,12 @@ TEST_CASE("std::string") {
SECTION("containsKey()") { SECTION("containsKey()") {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
deserializeJson(doc, json); deserializeJson(doc, json);
JsonObject &obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(true == obj.containsKey(std::string("key"))); REQUIRE(true == obj.containsKey(std::string("key")));
} }
SECTION("remove()") { SECTION("remove()") {
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj["key"] = "value"; obj["key"] = "value";
obj.remove(std::string("key")); obj.remove(std::string("key"));
@ -133,7 +133,7 @@ TEST_CASE("std::string") {
SECTION("operator[], set key") { SECTION("operator[], set key") {
std::string key("hello"); std::string key("hello");
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj[key] = "world"; obj[key] = "world";
eraseString(key); eraseString(key);
REQUIRE(std::string("world") == obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
@ -141,7 +141,7 @@ TEST_CASE("std::string") {
SECTION("operator[], set value") { SECTION("operator[], set value") {
std::string value("world"); std::string value("world");
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj["hello"] = value; obj["hello"] = value;
eraseString(value); eraseString(value);
REQUIRE(std::string("world") == obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
@ -149,7 +149,7 @@ TEST_CASE("std::string") {
SECTION("memoryUsage() increases when adding a new key") { SECTION("memoryUsage() increases when adding a new key") {
std::string key1("hello"), key2("world"); std::string key1("hello"), key2("world");
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj[key1] = 1; obj[key1] = 1;
size_t sizeBefore = doc.memoryUsage(); size_t sizeBefore = doc.memoryUsage();
@ -161,7 +161,7 @@ TEST_CASE("std::string") {
SECTION("memoryUsage() remains when adding the same key") { SECTION("memoryUsage() remains when adding the same key") {
std::string key("hello"); std::string key("hello");
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj[key] = 1; obj[key] = 1;
size_t sizeBefore = doc.memoryUsage(); size_t sizeBefore = doc.memoryUsage();

View File

@ -7,7 +7,7 @@
TEST_CASE("JsonObject::operator[]") { TEST_CASE("JsonObject::operator[]") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
SECTION("int") { SECTION("int") {
obj["hello"] = 123; obj["hello"] = 123;
@ -53,41 +53,37 @@ TEST_CASE("JsonObject::operator[]") {
SECTION("array") { SECTION("array") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>(); JsonArray arr = doc2.to<JsonArray>();
obj["hello"] = arr; obj["hello"] = arr;
REQUIRE(&arr == &obj["hello"].as<JsonArray&>()); REQUIRE(arr == obj["hello"].as<JsonArray>());
REQUIRE(&arr == &obj["hello"].as<JsonArray>()); // <- short hand REQUIRE(arr == obj["hello"].as<JsonArray>()); // <- short hand
REQUIRE(&arr == &obj["hello"].as<const JsonArray&>()); // REQUIRE(arr == obj["hello"].as<const JsonArray>());
REQUIRE(&arr == &obj["hello"].as<const JsonArray>()); // <- short hand // 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<JsonArray>()); REQUIRE(true == obj["hello"].is<JsonArray>());
REQUIRE(true == obj["hello"].is<const 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") { SECTION("object") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonObject& obj2 = doc2.to<JsonObject>(); JsonObject obj2 = doc2.to<JsonObject>();
obj["hello"] = obj2; obj["hello"] = obj2;
REQUIRE(&obj2 == &obj["hello"].as<JsonObject&>()); 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&>());
REQUIRE(&obj2 == &obj["hello"].as<const JsonObject>()); // <- short hand
REQUIRE(true == obj["hello"].is<JsonObject&>());
REQUIRE(true == obj["hello"].is<JsonObject>()); REQUIRE(true == obj["hello"].is<JsonObject>());
REQUIRE(true == obj["hello"].is<const 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") { SECTION("array subscript") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>(); JsonArray arr = doc2.to<JsonArray>();
arr.add(42); arr.add(42);
obj["a"] = arr[0]; obj["a"] = arr[0];
@ -97,7 +93,7 @@ TEST_CASE("JsonObject::operator[]") {
SECTION("object subscript") { SECTION("object subscript") {
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonObject& obj2 = doc2.to<JsonObject>(); JsonObject obj2 = doc2.to<JsonObject>();
obj2.set("x", 42); obj2.set("x", 42);
obj["a"] = obj2["x"]; obj["a"] = obj2["x"];

View File

@ -5,7 +5,7 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <catch.hpp> #include <catch.hpp>
static void check(JsonArray &array, std::string expected) { static void check(JsonArray array, std::string expected) {
std::string actual; std::string actual;
size_t actualLen = serializeJson(array, actual); size_t actualLen = serializeJson(array, actual);
REQUIRE(expected == actual); REQUIRE(expected == actual);
@ -16,7 +16,7 @@ static void check(JsonArray &array, std::string expected) {
TEST_CASE("serializeJson(JsonArray)") { TEST_CASE("serializeJson(JsonArray)") {
StaticJsonDocument<JSON_ARRAY_SIZE(2)> doc; StaticJsonDocument<JSON_ARRAY_SIZE(2)> doc;
JsonArray &array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
SECTION("Empty") { SECTION("Empty") {
check(array, "[]"); check(array, "[]");

View File

@ -5,7 +5,7 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <catch.hpp> #include <catch.hpp>
static void check(JsonArray& array, std::string expected) { static void check(JsonArray array, std::string expected) {
std::string actual; std::string actual;
size_t actualLen = serializeJsonPretty(array, actual); size_t actualLen = serializeJsonPretty(array, actual);
size_t measuredLen = measureJsonPretty(array); size_t measuredLen = measureJsonPretty(array);
@ -16,7 +16,7 @@ static void check(JsonArray& array, std::string expected) {
TEST_CASE("serializeJsonPretty(JsonArray)") { TEST_CASE("serializeJsonPretty(JsonArray)") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
SECTION("Empty") { SECTION("Empty") {
check(array, "[]"); check(array, "[]");
@ -54,11 +54,11 @@ TEST_CASE("serializeJsonPretty(JsonArray)") {
} }
SECTION("NestedArrays") { SECTION("NestedArrays") {
JsonArray& nested1 = array.createNestedArray(); JsonArray nested1 = array.createNestedArray();
nested1.add(1); nested1.add(1);
nested1.add(2); nested1.add(2);
JsonObject& nested2 = array.createNestedObject(); JsonObject nested2 = array.createNestedObject();
nested2["key"] = 3; nested2["key"] = 3;
check(array, check(array,

View File

@ -6,7 +6,7 @@
#include <catch.hpp> #include <catch.hpp>
#include <string> #include <string>
void check(const JsonObject &obj, const std::string &expected) { void check(const JsonObject obj, const std::string &expected) {
char actual[256]; char actual[256];
size_t actualLen = serializeJson(obj, actual); size_t actualLen = serializeJson(obj, actual);
size_t measuredLen = measureJson(obj); size_t measuredLen = measureJson(obj);
@ -18,7 +18,7 @@ void check(const JsonObject &obj, const std::string &expected) {
TEST_CASE("serializeJson(JsonObject)") { TEST_CASE("serializeJson(JsonObject)") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
SECTION("EmptyObject") { SECTION("EmptyObject") {
check(obj, "{}"); check(obj, "{}");

View File

@ -6,7 +6,7 @@
#include <catch.hpp> #include <catch.hpp>
#include <string> #include <string>
void check(const JsonObject &obj, const std::string expected) { void check(const JsonObject obj, const std::string expected) {
char json[256]; char json[256];
size_t actualLen = serializeJsonPretty(obj, json); size_t actualLen = serializeJsonPretty(obj, json);
@ -19,7 +19,7 @@ void check(const JsonObject &obj, const std::string expected) {
TEST_CASE("serializeJsonPretty(JsonObject)") { TEST_CASE("serializeJsonPretty(JsonObject)") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
SECTION("EmptyObject") { SECTION("EmptyObject") {
check(obj, "{}"); check(obj, "{}");
@ -57,10 +57,10 @@ TEST_CASE("serializeJsonPretty(JsonObject)") {
} }
SECTION("NestedContainers") { SECTION("NestedContainers") {
JsonObject &nested1 = obj.createNestedObject("key1"); JsonObject nested1 = obj.createNestedObject("key1");
nested1["a"] = 1; nested1["a"] = 1;
JsonArray &nested2 = obj.createNestedArray("key2"); JsonArray nested2 = obj.createNestedArray("key2");
nested2.add(2); nested2.add(2);
check(obj, check(obj,

View File

@ -14,11 +14,11 @@ void check(JsonVariant variant, const std::string &expected) {
} }
TEST_CASE("serializeJson(JsonVariant)") { TEST_CASE("serializeJson(JsonVariant)") {
SECTION("Empty") { SECTION("Undefined") {
check(JsonVariant(), ""); check(JsonVariant(), "null");
} }
SECTION("Null") { SECTION("Null string") {
check(static_cast<char *>(0), "null"); check(static_cast<char *>(0), "null");
} }

View File

@ -27,7 +27,7 @@ TEST_CASE("operator<<(std::ostream)") {
} }
SECTION("JsonObject") { SECTION("JsonObject") {
JsonObject& object = doc.to<JsonObject>(); JsonObject object = doc.to<JsonObject>();
object["key"] = "value"; object["key"] = "value";
os << object; os << object;
@ -36,7 +36,7 @@ TEST_CASE("operator<<(std::ostream)") {
} }
SECTION("JsonObjectSubscript") { SECTION("JsonObjectSubscript") {
JsonObject& object = doc.to<JsonObject>(); JsonObject object = doc.to<JsonObject>();
object["key"] = "value"; object["key"] = "value";
os << object["key"]; os << object["key"];
@ -45,7 +45,7 @@ TEST_CASE("operator<<(std::ostream)") {
} }
SECTION("JsonArray") { SECTION("JsonArray") {
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
array.add("value"); array.add("value");
os << array; os << array;
@ -54,7 +54,7 @@ TEST_CASE("operator<<(std::ostream)") {
} }
SECTION("JsonArraySubscript") { SECTION("JsonArraySubscript") {
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
array.add("value"); array.add("value");
os << array[0]; os << array[0];

View File

@ -7,7 +7,7 @@
TEST_CASE("serialize JsonArray to std::string") { TEST_CASE("serialize JsonArray to std::string") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray &array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
array.add(4); array.add(4);
array.add(2); array.add(2);
@ -28,7 +28,7 @@ TEST_CASE("serialize JsonArray to std::string") {
TEST_CASE("serialize JsonObject to std::string") { TEST_CASE("serialize JsonObject to std::string") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject &obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj["key"] = "value"; obj["key"] = "value";
SECTION("object") { SECTION("object") {

View File

@ -7,10 +7,10 @@ add_executable(JsonVariantTests
compare.cpp compare.cpp
copy.cpp copy.cpp
is.cpp is.cpp
isnull.cpp
or.cpp or.cpp
set_get.cpp set_get.cpp
subscript.cpp subscript.cpp
success.cpp
undefined.cpp undefined.cpp
) )

View File

@ -193,7 +193,7 @@ TEST_CASE("JsonVariant::as()") {
SECTION("ObjectAsString") { SECTION("ObjectAsString") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj["key"] = "value"; obj["key"] = "value";
@ -203,7 +203,7 @@ TEST_CASE("JsonVariant::as()") {
SECTION("ArrayAsString") { SECTION("ArrayAsString") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& arr = doc.to<JsonArray>(); JsonArray arr = doc.to<JsonArray>();
arr.add(4); arr.add(4);
arr.add(2); arr.add(2);
@ -213,19 +213,19 @@ TEST_CASE("JsonVariant::as()") {
SECTION("ArrayAsJsonArray") { SECTION("ArrayAsJsonArray") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& arr = doc.to<JsonArray>(); JsonArray arr = doc.to<JsonArray>();
JsonVariant variant = arr; JsonVariant variant = arr;
REQUIRE(&arr == &variant.as<JsonArray&>()); REQUIRE(arr == variant.as<JsonArray>());
REQUIRE(&arr == &variant.as<JsonArray>()); // <- shorthand REQUIRE(arr == variant.as<JsonArray>()); // <- shorthand
} }
SECTION("ObjectAsJsonObject") { SECTION("ObjectAsJsonObject") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
JsonVariant variant = obj; JsonVariant variant = obj;
REQUIRE(&obj == &variant.as<JsonObject&>()); REQUIRE(obj == variant.as<JsonObject>());
REQUIRE(&obj == &variant.as<JsonObject>()); // <- shorthand REQUIRE(obj == variant.as<JsonObject>()); // <- shorthand
} }
} }

View File

@ -213,9 +213,9 @@ TEST_CASE("JsonVariant comparisons") {
SECTION("ArrayInVariant") { SECTION("ArrayInVariant") {
DynamicJsonDocument doc1; DynamicJsonDocument doc1;
JsonArray& array1 = doc1.to<JsonArray>(); JsonArray array1 = doc1.to<JsonArray>();
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonArray& array2 = doc2.to<JsonArray>(); JsonArray array2 = doc2.to<JsonArray>();
JsonVariant variant1 = array1; JsonVariant variant1 = array1;
JsonVariant variant2 = array1; JsonVariant variant2 = array1;
@ -230,9 +230,9 @@ TEST_CASE("JsonVariant comparisons") {
SECTION("ObjectInVariant") { SECTION("ObjectInVariant") {
DynamicJsonDocument doc1; DynamicJsonDocument doc1;
JsonObject& obj1 = doc1.to<JsonObject>(); JsonObject obj1 = doc1.to<JsonObject>();
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonObject& obj2 = doc2.to<JsonObject>(); JsonObject obj2 = doc2.to<JsonObject>();
JsonVariant variant1 = obj1; JsonVariant variant1 = obj1;
JsonVariant variant2 = obj1; JsonVariant variant2 = obj1;
@ -247,10 +247,10 @@ TEST_CASE("JsonVariant comparisons") {
SECTION("VariantsOfDifferentTypes") { SECTION("VariantsOfDifferentTypes") {
DynamicJsonDocument doc1; DynamicJsonDocument doc1;
JsonObject& obj = doc1.to<JsonObject>(); JsonObject obj = doc1.to<JsonObject>();
DynamicJsonDocument doc2; DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>(); JsonArray arr = doc2.to<JsonArray>();
JsonVariant variants[] = { JsonVariant variants[] = {
true, 42, 666.667, "hello", arr, obj, true, 42, 666.667, "hello", arr, obj,
}; };

View File

@ -43,7 +43,7 @@ TEST_CASE("JsonVariant copy") {
SECTION("ObjectsAreCopiedByReference") { SECTION("ObjectsAreCopiedByReference") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& object = doc.to<JsonObject>(); JsonObject object = doc.to<JsonObject>();
_variant1 = object; _variant1 = object;
@ -54,7 +54,7 @@ TEST_CASE("JsonVariant copy") {
SECTION("ArraysAreCopiedByReference") { SECTION("ArraysAreCopiedByReference") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
_variant1 = array; _variant1 = array;

View File

@ -7,9 +7,9 @@
void checkIsArray(JsonVariant var) { void checkIsArray(JsonVariant var) {
REQUIRE(var.is<JsonArray>()); 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(var.is<const JsonArray&>());
REQUIRE_FALSE(var.is<bool>()); REQUIRE_FALSE(var.is<bool>());
REQUIRE_FALSE(var.is<double>()); REQUIRE_FALSE(var.is<double>());
@ -71,7 +71,7 @@ void checkIsString(JsonVariant var) {
TEST_CASE("JsonVariant::is()") { TEST_CASE("JsonVariant::is()") {
SECTION("JsonArray") { SECTION("JsonArray") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
checkIsArray(array); checkIsArray(array);
} }

View File

@ -5,40 +5,40 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <catch.hpp> #include <catch.hpp>
TEST_CASE("JsonVariant::success()") { TEST_CASE("JsonVariant::isNull()") {
SECTION("ReturnsFalse_WhenUndefined") { SECTION("ReturnsFalse_WhenUndefined") {
JsonVariant variant; JsonVariant variant;
REQUIRE(false == variant.success()); REQUIRE(variant.isNull() == true);
} }
SECTION("ReturnsTrue_WhenInteger") { SECTION("ReturnsTrue_WhenInteger") {
JsonVariant variant = 0; JsonVariant variant = 0;
REQUIRE(true == variant.success()); REQUIRE(variant.isNull() == false);
} }
SECTION("ReturnsTrue_WhenEmptyArray") { SECTION("ReturnsTrue_WhenEmptyArray") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
JsonVariant variant = array; JsonVariant variant = array;
REQUIRE(true == variant.success()); REQUIRE(variant.isNull() == false);
} }
SECTION("ReturnsTrue_WhenEmptyObject") { SECTION("ReturnsTrue_WhenEmptyObject") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
JsonVariant variant = obj; JsonVariant variant = obj;
REQUIRE(true == variant.success()); REQUIRE(variant.isNull() == false);
} }
SECTION("ReturnsFalse_WhenInvalidArray") { SECTION("ReturnsFalse_WhenInvalidArray") {
JsonVariant variant = JsonArray::invalid(); JsonVariant variant = JsonArray();
REQUIRE(false == variant.success()); REQUIRE(variant.isNull() == true);
} }
SECTION("ReturnsFalse_WhenInvalidObject") { SECTION("ReturnsFalse_WhenInvalidObject") {
JsonVariant variant = JsonObject::invalid(); JsonVariant variant = JsonObject();
REQUIRE(false == variant.success()); REQUIRE(variant.isNull() == true);
} }
} }

View File

@ -123,8 +123,8 @@ TEST_CASE("JsonVariant set()/get()") {
SECTION("CanStoreObject") { SECTION("CanStoreObject") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject &object = doc.to<JsonObject>(); JsonObject object = doc.to<JsonObject>();
checkReference<JsonObject>(object); checkValue<JsonObject>(object);
} }
} }

View File

@ -8,7 +8,7 @@
TEST_CASE("JsonVariant::operator[]") { TEST_CASE("JsonVariant::operator[]") {
SECTION("Array") { SECTION("Array") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
array.add("element at index 0"); array.add("element at index 0");
array.add("element at index 1"); 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 1") == var[1]);
REQUIRE(std::string("element at index 0") == REQUIRE(std::string("element at index 0") ==
var[static_cast<unsigned char>(0)]); // issue #381 var[static_cast<unsigned char>(0)]); // issue #381
REQUIRE_FALSE(var[666].success()); REQUIRE(var[666].isNull());
REQUIRE_FALSE(var[3].success()); REQUIRE(var[3].isNull());
REQUIRE_FALSE(var["0"].success()); REQUIRE(var["0"].isNull());
} }
SECTION("Object") { SECTION("Object") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& object = doc.to<JsonObject>(); JsonObject object = doc.to<JsonObject>();
object["a"] = "element at key \"a\""; object["a"] = "element at key \"a\"";
object["b"] = "element at key \"b\""; object["b"] = "element at key \"b\"";
@ -35,27 +35,27 @@ TEST_CASE("JsonVariant::operator[]") {
REQUIRE(2 == var.size()); REQUIRE(2 == var.size());
REQUIRE(std::string("element at key \"a\"") == var["a"]); REQUIRE(std::string("element at key \"a\"") == var["a"]);
REQUIRE(std::string("element at key \"b\"") == var["b"]); REQUIRE(std::string("element at key \"b\"") == var["b"]);
REQUIRE_FALSE(var["c"].success()); REQUIRE(var["c"].isNull());
REQUIRE_FALSE(var[0].success()); REQUIRE(var[0].isNull());
} }
SECTION("Undefined") { SECTION("Undefined") {
JsonVariant var = JsonVariant(); JsonVariant var = JsonVariant();
REQUIRE(0 == var.size()); REQUIRE(0 == var.size());
REQUIRE_FALSE(var["0"].success()); REQUIRE(var["0"].isNull());
REQUIRE_FALSE(var[0].success()); REQUIRE(var[0].isNull());
} }
SECTION("String") { SECTION("String") {
JsonVariant var = "hello world"; JsonVariant var = "hello world";
REQUIRE(0 == var.size()); REQUIRE(0 == var.size());
REQUIRE_FALSE(var["0"].success()); REQUIRE(var["0"].isNull());
REQUIRE_FALSE(var[0].success()); REQUIRE(var[0].isNull());
} }
SECTION("ObjectSetValue") { SECTION("ObjectSetValue") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
JsonVariant var = obj; JsonVariant var = obj;
var["hello"] = "world"; var["hello"] = "world";
REQUIRE(1 == var.size()); REQUIRE(1 == var.size());
@ -64,7 +64,7 @@ TEST_CASE("JsonVariant::operator[]") {
SECTION("ArraySetValue") { SECTION("ArraySetValue") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& arr = doc.to<JsonArray>(); JsonArray arr = doc.to<JsonArray>();
arr.add("hello"); arr.add("hello");
JsonVariant var = arr; JsonVariant var = arr;
var[0] = "world"; var[0] = "world";

View File

@ -29,26 +29,26 @@ TEST_CASE("JsonVariant undefined") {
} }
SECTION("AsArrayReturnInvalid") { SECTION("AsArrayReturnInvalid") {
REQUIRE(JsonArray::invalid() == variant.as<JsonArray&>()); REQUIRE(JsonArray() == variant.as<JsonArray>());
} }
SECTION("AsConstArrayReturnInvalid") { SECTION("AsConstArrayReturnInvalid") {
REQUIRE(JsonArray::invalid() == variant.as<const JsonArray&>()); REQUIRE(JsonArray() == variant.as<const JsonArray>());
} }
SECTION("AsObjectReturnInvalid") { SECTION("AsObjectReturnInvalid") {
REQUIRE(JsonObject::invalid() == variant.as<JsonObject&>()); REQUIRE(JsonObject() == variant.as<JsonObject>());
} }
SECTION("AsConstObjectReturnInvalid") { SECTION("AsConstObjectReturnInvalid") {
REQUIRE(JsonObject::invalid() == variant.as<const JsonObject&>()); REQUIRE(JsonObject() == variant.as<const JsonObject>());
} }
SECTION("AsArrayWrapperReturnInvalid") { SECTION("AsArrayWrapperReturnInvalid") {
REQUIRE(JsonArray::invalid() == variant.as<JsonArray>()); REQUIRE(JsonArray() == variant.as<JsonArray>());
} }
SECTION("AsObjectWrapperReturnInvalid") { SECTION("AsObjectWrapperReturnInvalid") {
REQUIRE(JsonObject::invalid() == variant.as<JsonObject>()); REQUIRE(JsonObject() == variant.as<JsonObject>());
} }
} }

View File

@ -101,7 +101,7 @@ TEST_CASE("unsigned char[]") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj[key] = "world"; obj[key] = "world";
REQUIRE(std::string("world") == obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
@ -113,7 +113,7 @@ TEST_CASE("unsigned char[]") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
deserializeJson(doc, "{\"hello\":\"world\"}"); deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(std::string("world") == obj[key]); REQUIRE(std::string("world") == obj[key]);
} }
#endif #endif
@ -123,7 +123,7 @@ TEST_CASE("unsigned char[]") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
deserializeJson(doc, "{\"hello\":\"world\"}"); deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(std::string("world") == obj.get<char*>(key)); REQUIRE(std::string("world") == obj.get<char*>(key));
} }
@ -131,7 +131,7 @@ TEST_CASE("unsigned char[]") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj.set(key, "world"); obj.set(key, "world");
REQUIRE(std::string("world") == obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
@ -141,7 +141,7 @@ TEST_CASE("unsigned char[]") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj.set("hello", value); obj.set("hello", value);
REQUIRE(std::string("world") == obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
@ -151,7 +151,7 @@ TEST_CASE("unsigned char[]") {
unsigned char key[] = "world"; unsigned char key[] = "world";
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj.set(key, key); obj.set(key, key);
REQUIRE(std::string("world") == obj["world"]); REQUIRE(std::string("world") == obj["world"]);
@ -162,7 +162,7 @@ TEST_CASE("unsigned char[]") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
deserializeJson(doc, "{\"hello\":\"world\"}"); deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(true == obj.containsKey(key)); REQUIRE(true == obj.containsKey(key));
} }
@ -171,7 +171,7 @@ TEST_CASE("unsigned char[]") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
deserializeJson(doc, "{\"hello\":\"world\"}"); deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
obj.remove(key); obj.remove(key);
REQUIRE(0 == obj.size()); REQUIRE(0 == obj.size());
@ -182,7 +182,7 @@ TEST_CASE("unsigned char[]") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
deserializeJson(doc, "{\"hello\":42}"); deserializeJson(doc, "{\"hello\":42}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(true == obj.is<int>(key)); REQUIRE(true == obj.is<int>(key));
} }
@ -191,7 +191,7 @@ TEST_CASE("unsigned char[]") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj.createNestedArray(key); obj.createNestedArray(key);
} }
@ -199,7 +199,7 @@ TEST_CASE("unsigned char[]") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj.createNestedObject(key); obj.createNestedObject(key);
} }
} }
@ -209,7 +209,7 @@ TEST_CASE("unsigned char[]") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj["hello"] = value; obj["hello"] = value;
REQUIRE(std::string("world") == obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
@ -219,7 +219,7 @@ TEST_CASE("unsigned char[]") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj["hello"].set(value); obj["hello"].set(value);
REQUIRE(std::string("world") == obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
@ -231,7 +231,7 @@ TEST_CASE("unsigned char[]") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& arr = doc.to<JsonArray>(); JsonArray arr = doc.to<JsonArray>();
arr.add(value); arr.add(value);
REQUIRE(std::string("world") == arr[0]); REQUIRE(std::string("world") == arr[0]);
@ -241,7 +241,7 @@ TEST_CASE("unsigned char[]") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& arr = doc.to<JsonArray>(); JsonArray arr = doc.to<JsonArray>();
arr.add("hello"); arr.add("hello");
arr.set(0, value); arr.set(0, value);
@ -254,7 +254,7 @@ TEST_CASE("unsigned char[]") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& arr = doc.to<JsonArray>(); JsonArray arr = doc.to<JsonArray>();
arr.add("hello"); arr.add("hello");
arr[0].set(value); arr[0].set(value);
@ -265,7 +265,7 @@ TEST_CASE("unsigned char[]") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& arr = doc.to<JsonArray>(); JsonArray arr = doc.to<JsonArray>();
arr.add("hello"); arr.add("hello");
arr[0] = value; arr[0] = value;

View File

@ -126,7 +126,7 @@ TEST_CASE("Variable Length Array") {
strcpy(vla, "hello"); strcpy(vla, "hello");
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj[vla] = "world"; obj[vla] = "world";
REQUIRE(std::string("world") == obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
@ -142,7 +142,7 @@ TEST_CASE("Variable Length Array") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
deserializeJson(doc, "{\"hello\":\"world\"}"); deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(std::string("world") == obj[vla]); REQUIRE(std::string("world") == obj[vla]);
} }
#endif #endif
@ -155,7 +155,7 @@ TEST_CASE("Variable Length Array") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
deserializeJson(doc, "{\"hello\":\"world\"}"); deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(std::string("world") == obj.get<char*>(vla)); REQUIRE(std::string("world") == obj.get<char*>(vla));
} }
@ -165,7 +165,7 @@ TEST_CASE("Variable Length Array") {
strcpy(vla, "hello"); strcpy(vla, "hello");
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj.set(vla, "world"); obj.set(vla, "world");
REQUIRE(std::string("world") == obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
@ -177,7 +177,7 @@ TEST_CASE("Variable Length Array") {
strcpy(vla, "world"); strcpy(vla, "world");
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj.set("hello", vla); obj.set("hello", vla);
REQUIRE(std::string("world") == obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
@ -189,7 +189,7 @@ TEST_CASE("Variable Length Array") {
strcpy(vla, "world"); strcpy(vla, "world");
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj.set(vla, vla); obj.set(vla, vla);
REQUIRE(std::string("world") == obj["world"]); REQUIRE(std::string("world") == obj["world"]);
@ -203,7 +203,7 @@ TEST_CASE("Variable Length Array") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
deserializeJson(doc, "{\"hello\":\"world\"}"); deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(true == obj.containsKey(vla)); REQUIRE(true == obj.containsKey(vla));
} }
@ -214,7 +214,7 @@ TEST_CASE("Variable Length Array") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
deserializeJson(doc, "{\"hello\":\"world\"}"); deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
obj.remove(vla); obj.remove(vla);
REQUIRE(0 == obj.size()); REQUIRE(0 == obj.size());
@ -227,7 +227,7 @@ TEST_CASE("Variable Length Array") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
deserializeJson(doc, "{\"hello\":42}"); deserializeJson(doc, "{\"hello\":42}");
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(true == obj.is<int>(vla)); REQUIRE(true == obj.is<int>(vla));
} }
@ -238,7 +238,7 @@ TEST_CASE("Variable Length Array") {
strcpy(vla, "hello"); strcpy(vla, "hello");
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj.createNestedArray(vla); obj.createNestedArray(vla);
} }
@ -248,7 +248,7 @@ TEST_CASE("Variable Length Array") {
strcpy(vla, "hello"); strcpy(vla, "hello");
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj.createNestedObject(vla); obj.createNestedObject(vla);
} }
} }
@ -260,7 +260,7 @@ TEST_CASE("Variable Length Array") {
strcpy(vla, "world"); strcpy(vla, "world");
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj["hello"] = vla; obj["hello"] = vla;
REQUIRE(std::string("world") == obj["hello"].as<char*>()); REQUIRE(std::string("world") == obj["hello"].as<char*>());
@ -272,7 +272,7 @@ TEST_CASE("Variable Length Array") {
strcpy(vla, "world"); strcpy(vla, "world");
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>(); JsonObject obj = doc.to<JsonObject>();
obj["hello"].set(vla); obj["hello"].set(vla);
REQUIRE(std::string("world") == obj["hello"].as<char*>()); REQUIRE(std::string("world") == obj["hello"].as<char*>());
@ -286,7 +286,7 @@ TEST_CASE("Variable Length Array") {
strcpy(vla, "world"); strcpy(vla, "world");
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& arr = doc.to<JsonArray>(); JsonArray arr = doc.to<JsonArray>();
arr.add(vla); arr.add(vla);
REQUIRE(std::string("world") == arr[0]); REQUIRE(std::string("world") == arr[0]);
@ -298,7 +298,7 @@ TEST_CASE("Variable Length Array") {
strcpy(vla, "world"); strcpy(vla, "world");
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& arr = doc.to<JsonArray>(); JsonArray arr = doc.to<JsonArray>();
arr.add("hello"); arr.add("hello");
arr.set(0, vla); arr.set(0, vla);
@ -313,7 +313,7 @@ TEST_CASE("Variable Length Array") {
strcpy(vla, "world"); strcpy(vla, "world");
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& arr = doc.to<JsonArray>(); JsonArray arr = doc.to<JsonArray>();
arr.add("hello"); arr.add("hello");
arr[0].set(vla); arr[0].set(vla);
@ -326,7 +326,7 @@ TEST_CASE("Variable Length Array") {
strcpy(vla, "world"); strcpy(vla, "world");
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& arr = doc.to<JsonArray>(); JsonArray arr = doc.to<JsonArray>();
arr.add("hello"); arr.add("hello");
arr[0] = vla; arr[0] = vla;

View File

@ -13,7 +13,7 @@ TEST_CASE("deserialize MsgPack array") {
const char* input = "\x90"; const char* input = "\x90";
DeserializationError error = deserializeMsgPack(doc, input); DeserializationError error = deserializeMsgPack(doc, input);
JsonArray& array = doc.as<JsonArray>(); JsonArray array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok); REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 0); REQUIRE(array.size() == 0);
@ -23,7 +23,7 @@ TEST_CASE("deserialize MsgPack array") {
const char* input = "\x92\x01\x02"; const char* input = "\x92\x01\x02";
DeserializationError error = deserializeMsgPack(doc, input); DeserializationError error = deserializeMsgPack(doc, input);
JsonArray& array = doc.as<JsonArray>(); JsonArray array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok); REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 2); REQUIRE(array.size() == 2);
@ -37,7 +37,7 @@ TEST_CASE("deserialize MsgPack array") {
const char* input = "\xDC\x00\x00"; const char* input = "\xDC\x00\x00";
DeserializationError error = deserializeMsgPack(doc, input); DeserializationError error = deserializeMsgPack(doc, input);
JsonArray& array = doc.as<JsonArray>(); JsonArray array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok); REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 0); REQUIRE(array.size() == 0);
@ -47,7 +47,7 @@ TEST_CASE("deserialize MsgPack array") {
const char* input = "\xDC\x00\x02\xA5hello\xA5world"; const char* input = "\xDC\x00\x02\xA5hello\xA5world";
DeserializationError error = deserializeMsgPack(doc, input); DeserializationError error = deserializeMsgPack(doc, input);
JsonArray& array = doc.as<JsonArray>(); JsonArray array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok); REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 2); REQUIRE(array.size() == 2);
@ -61,7 +61,7 @@ TEST_CASE("deserialize MsgPack array") {
const char* input = "\xDD\x00\x00\x00\x00"; const char* input = "\xDD\x00\x00\x00\x00";
DeserializationError error = deserializeMsgPack(doc, input); DeserializationError error = deserializeMsgPack(doc, input);
JsonArray& array = doc.as<JsonArray>(); JsonArray array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok); REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 0); 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"; "\xDD\x00\x00\x00\x02\xCA\x00\x00\x00\x00\xCA\x40\x48\xF5\xC3";
DeserializationError error = deserializeMsgPack(doc, input); DeserializationError error = deserializeMsgPack(doc, input);
JsonArray& array = doc.as<JsonArray>(); JsonArray array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok); REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 2); REQUIRE(array.size() == 2);

View File

@ -13,7 +13,7 @@ TEST_CASE("deserialize MsgPack object") {
const char* input = "\x80"; const char* input = "\x80";
DeserializationError error = deserializeMsgPack(doc, input); DeserializationError error = deserializeMsgPack(doc, input);
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok); REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -24,7 +24,7 @@ TEST_CASE("deserialize MsgPack object") {
const char* input = "\x82\xA3one\x01\xA3two\x02"; const char* input = "\x82\xA3one\x01\xA3two\x02";
DeserializationError error = deserializeMsgPack(doc, input); DeserializationError error = deserializeMsgPack(doc, input);
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok); REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -39,7 +39,7 @@ TEST_CASE("deserialize MsgPack object") {
const char* input = "\xDE\x00\x00"; const char* input = "\xDE\x00\x00";
DeserializationError error = deserializeMsgPack(doc, input); DeserializationError error = deserializeMsgPack(doc, input);
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok); REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -50,7 +50,7 @@ TEST_CASE("deserialize MsgPack object") {
const char* input = "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world"; const char* input = "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world";
DeserializationError error = deserializeMsgPack(doc, input); DeserializationError error = deserializeMsgPack(doc, input);
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok); REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -65,7 +65,7 @@ TEST_CASE("deserialize MsgPack object") {
const char* input = "\xDF\x00\x00\x00\x00"; const char* input = "\xDF\x00\x00\x00\x00";
DeserializationError error = deserializeMsgPack(doc, input); DeserializationError error = deserializeMsgPack(doc, input);
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok); REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());
@ -78,7 +78,7 @@ TEST_CASE("deserialize MsgPack object") {
"\xF5\xC3"; "\xF5\xC3";
DeserializationError error = deserializeMsgPack(doc, input); DeserializationError error = deserializeMsgPack(doc, input);
JsonObject& obj = doc.as<JsonObject>(); JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok); REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>()); REQUIRE(doc.is<JsonObject>());

View File

@ -14,7 +14,7 @@ TEST_CASE("deserializeMsgPack(std::istream&)") {
DeserializationError err = deserializeMsgPack(doc, input); DeserializationError err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(arr[0] == 0); REQUIRE(arr[0] == 0);
REQUIRE(arr[1] == 2); REQUIRE(arr[1] == 2);
} }

View File

@ -29,7 +29,7 @@ TEST_CASE("deserializeMsgPack(const std::string&)") {
DeserializationError err = deserializeMsgPack(doc, input); DeserializationError err = deserializeMsgPack(doc, input);
input[2] = 'X'; // alter the string tomake sure we made a copy 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(err == DeserializationError::Ok);
REQUIRE(std::string("hello") == array[0]); REQUIRE(std::string("hello") == array[0]);
} }
@ -39,7 +39,7 @@ TEST_CASE("deserializeMsgPack(const std::string&)") {
deserializeMsgPack(doc, std::string("\x92\x00\x02", 3)); deserializeMsgPack(doc, std::string("\x92\x00\x02", 3));
REQUIRE(err == DeserializationError::Ok); REQUIRE(err == DeserializationError::Ok);
JsonArray& arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
REQUIRE(arr[0] == 0); REQUIRE(arr[0] == 0);
REQUIRE(arr[1] == 2); REQUIRE(arr[1] == 2);
} }

View File

@ -7,7 +7,7 @@
TEST_CASE("serialize MsgPack to various destination types") { TEST_CASE("serialize MsgPack to various destination types") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject &object = doc.to<JsonObject>(); JsonObject object = doc.to<JsonObject>();
object["hello"] = "world"; object["hello"] = "world";
const char *expected_result = "\x81\xA5hello\xA5world"; const char *expected_result = "\x81\xA5hello\xA5world";
const size_t expected_length = 13; const size_t expected_length = 13;

View File

@ -7,7 +7,7 @@
TEST_CASE("measureMsgPack()") { TEST_CASE("measureMsgPack()") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject &object = doc.to<JsonObject>(); JsonObject object = doc.to<JsonObject>();
object["hello"] = "world"; object["hello"] = "world";
REQUIRE(measureMsgPack(doc) == 13); REQUIRE(measureMsgPack(doc) == 13);

View File

@ -5,7 +5,7 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <catch.hpp> #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) { size_t expected_len) {
std::string expected(expected_data, expected_data + expected_len); std::string expected(expected_data, expected_data + expected_len);
std::string actual; std::string actual;
@ -16,19 +16,19 @@ static void check(const JsonArray& array, const char* expected_data,
} }
template <size_t N> 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; const size_t expected_len = N - 1;
check(array, expected_data, expected_len); check(array, expected_data, expected_len);
} }
// TODO: this function is used by the commented test // 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()); // check(array, expected.data(), expected.length());
// } // }
TEST_CASE("serialize MsgPack array") { TEST_CASE("serialize MsgPack array") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonArray& array = doc.to<JsonArray>(); JsonArray array = doc.to<JsonArray>();
SECTION("empty") { SECTION("empty") {
check(array, "\x90"); check(array, "\x90");

View File

@ -6,7 +6,7 @@
#include <stdio.h> #include <stdio.h>
#include <catch.hpp> #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) { size_t expected_len) {
std::string expected(expected_data, expected_data + expected_len); std::string expected(expected_data, expected_data + expected_len);
std::string actual; std::string actual;
@ -17,19 +17,19 @@ static void check(const JsonObject& object, const char* expected_data,
} }
template <size_t N> 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; const size_t expected_len = N - 1;
check(object, expected_data, expected_len); check(object, expected_data, expected_len);
} }
// TODO: used by the commented test // 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()); // check(object, expected.data(), expected.length());
//} //}
TEST_CASE("serialize MsgPack object") { TEST_CASE("serialize MsgPack object") {
DynamicJsonDocument doc; DynamicJsonDocument doc;
JsonObject& object = doc.to<JsonObject>(); JsonObject object = doc.to<JsonObject>();
SECTION("empty") { SECTION("empty") {
check(object, "\x80"); check(object, "\x80");