diff --git a/CHANGELOG.md b/CHANGELOG.md index 50c30377..b720ecff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,36 @@ ArduinoJson: change log ======================= +HEAD +---- + +* Return `JsonArray` and `JsonObject` by value instead of reference (issue #309) +* Replaced `success()` with `isNull()` + +> ### BREAKING CHANGES +> +> Old code: +> +> ```c++ +> JsonObject& obj = doc.to(); +> JsonArray& arr = obj.createNestedArray("key"); +> if (!arr.success()) { +> Serial.println("No enough memory"); +> return; +> } +> ``` +> +> New code: +> +> ```c++ +> JsonObject obj = doc.to(); +> JsonArray arr = obj.createNestedArray("key"); +> if (arr.isNull()) { +> Serial.println("No enough memory"); +> return; +> } +> ``` + v6.0.1-beta ----------- diff --git a/README.md b/README.md index 725ef8d8..64f31c09 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302 DynamicJsonDocument doc; deserializeJson(doc, json); -JsonObject& root = doc.as(); +JsonObjectRef root = doc.as(); const char* sensor = root["sensor"]; long time = root["time"]; double latitude = root["data"][0]; @@ -79,11 +79,11 @@ Here is a program that generates a JSON document with ArduinoJson: ```c++ DynamicJsonDocument doc; -JsonObject& root = doc.to(); +JsonObject root = doc.to(); root["sensor"] = "gps"; root["time"] = 1351824120; -JsonArray& data = root.createNestedArray("data"); +JsonArray data = root.createNestedArray("data"); data.add(48.756080); data.add(2.302038); diff --git a/examples/JsonConfigFile/JsonConfigFile.ino b/examples/JsonConfigFile/JsonConfigFile.ino index 2d5956ce..5778527f 100644 --- a/examples/JsonConfigFile/JsonConfigFile.ino +++ b/examples/JsonConfigFile/JsonConfigFile.ino @@ -40,7 +40,7 @@ void loadConfiguration(const char *filename, Config &config) { Serial.println(F("Failed to read file, using default configuration")); // Get the root object in the document - JsonObject &root = doc.as(); + JsonObject root = doc.as(); // Copy values from the JsonObject to the Config config.port = root["port"] | 2731; @@ -70,7 +70,7 @@ void saveConfiguration(const char *filename, const Config &config) { StaticJsonDocument<256> doc; // Make our document contain an object - JsonObject &root = doc.to(); + JsonObject root = doc.to(); // Set the values in the object root["hostname"] = config.hostname; @@ -132,15 +132,4 @@ void loop() { // not used in this example } -// See also -// -------- -// -// The website arduinojson.org contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any -// serialization or deserialization problem. -// Please check it out at: https://arduinojson.org/ -// -// The book "Mastering ArduinoJson" contains a case study of a project that has -// a complex configuration with nested members. -// Contrary to this example, the project in the book uses the SPIFFS filesystem. -// Please check it out at: https://arduinojson.org/book/ +// Visit https://arduinojson.org/v6/example/config/ for more. diff --git a/examples/JsonGeneratorExample/JsonGeneratorExample.ino b/examples/JsonGeneratorExample/JsonGeneratorExample.ino index d5f0cc70..81f12375 100644 --- a/examples/JsonGeneratorExample/JsonGeneratorExample.ino +++ b/examples/JsonGeneratorExample/JsonGeneratorExample.ino @@ -24,7 +24,7 @@ void setup() { // DynamicJsonDocument doc(200); // Make our document be an object - JsonObject& root = doc.to(); + JsonObject root = doc.to(); // Add values in the object // @@ -35,7 +35,7 @@ void setup() { // Add an array. // - JsonArray& data = root.createNestedArray("data"); + JsonArray data = root.createNestedArray("data"); data.add(48.756080); data.add(2.302038); @@ -61,15 +61,4 @@ void loop() { // not used in this example } -// See also -// -------- -// -// The website arduinojson.org contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any -// serialization problem. -// Please check it out at: https://arduinojson.org/ -// -// The book "Mastering ArduinoJson" contains a tutorial on serialization. -// It begins with a simple example, like the one above, and then adds more -// features like serializing directly to a file or an HTTP request. -// Please check it out at: https://arduinojson.org/book/ +// Visit https://arduinojson.org/v6/example/generator/ for more. diff --git a/examples/JsonHttpClient/JsonHttpClient.ino b/examples/JsonHttpClient/JsonHttpClient.ino index 73f6b15e..5bf0f2c1 100644 --- a/examples/JsonHttpClient/JsonHttpClient.ino +++ b/examples/JsonHttpClient/JsonHttpClient.ino @@ -84,7 +84,7 @@ void setup() { } // Extract values - JsonObject& root = doc.as(); + JsonObject root = doc.as(); Serial.println(F("Response:")); Serial.println(root["sensor"].as()); Serial.println(root["time"].as()); @@ -99,16 +99,4 @@ void loop() { // not used in this example } -// See also -// -------- -// -// The website arduinojson.org contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any -// serialization problem. -// Please check it out at: https://arduinojson.org/ -// -// The book "Mastering ArduinoJson" contains a tutorial on deserialization -// showing how to parse the response from Yahoo Weather. In the last chapter, -// it shows how to parse the huge documents from OpenWeatherMap -// and Weather Underground. -// Please check it out at: https://arduinojson.org/book/ +// Visit https://arduinojson.org/v6/example/http-client/ for more. diff --git a/examples/JsonParserExample/JsonParserExample.ino b/examples/JsonParserExample/JsonParserExample.ino index d2226be6..3e4e7ac8 100644 --- a/examples/JsonParserExample/JsonParserExample.ino +++ b/examples/JsonParserExample/JsonParserExample.ino @@ -42,7 +42,7 @@ void setup() { } // Get the root object in the document - JsonObject& root = doc.as(); + JsonObject root = doc.as(); // Fetch values. // @@ -64,15 +64,4 @@ void loop() { // not used in this example } -// See also -// -------- -// -// The website arduinojson.org contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any -// deserialization problem. -// Please check it out at: https://arduinojson.org/ -// -// The book "Mastering ArduinoJson" contains a tutorial on deserialization. -// It begins with a simple example, like the one above, and then adds more -// features like deserializing directly from a file or an HTTP request. -// Please check it out at: https://arduinojson.org/book/ +// Visit https://arduinojson.org/v6/example/parser/ for more. diff --git a/examples/JsonServer/JsonServer.ino b/examples/JsonServer/JsonServer.ino index 92dcc790..d1350904 100644 --- a/examples/JsonServer/JsonServer.ino +++ b/examples/JsonServer/JsonServer.ino @@ -56,10 +56,10 @@ void loop() { StaticJsonDocument<500> doc; // Make our document represent an object - JsonObject& root = doc.to(); + JsonObject root = doc.to(); // Create the "analog" array - JsonArray& analogValues = root.createNestedArray("analog"); + JsonArray analogValues = root.createNestedArray("analog"); for (int pin = 0; pin < 6; pin++) { // Read the analog input int value = analogRead(pin); @@ -69,7 +69,7 @@ void loop() { } // Create the "digital" array - JsonArray& digitalValues = root.createNestedArray("digital"); + JsonArray digitalValues = root.createNestedArray("digital"); for (int pin = 0; pin < 14; pin++) { // Read the digital input int value = digitalRead(pin); @@ -95,15 +95,4 @@ void loop() { client.stop(); } -// See also -// -------- -// -// The website arduinojson.org contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any -// serialization problem. -// Please check it out at: https://arduinojson.org/ -// -// The book "Mastering ArduinoJson" contains a tutorial on serialization. -// It begins with a simple example, then adds more features like serializing -// directly to a file or an HTTP client. -// Please check it out at: https://arduinojson.org/book/ +// Visit https://arduinojson.org/v6/example/http-server/ for more. diff --git a/examples/JsonUdpBeacon/JsonUdpBeacon.ino b/examples/JsonUdpBeacon/JsonUdpBeacon.ino index 1af13d9a..850a3a2f 100644 --- a/examples/JsonUdpBeacon/JsonUdpBeacon.ino +++ b/examples/JsonUdpBeacon/JsonUdpBeacon.ino @@ -48,10 +48,10 @@ void loop() { StaticJsonDocument<500> doc; // Make our document represent an object - JsonObject& root = doc.to(); + JsonObject root = doc.to(); // Create the "analog" array - JsonArray& analogValues = root.createNestedArray("analog"); + JsonArray analogValues = root.createNestedArray("analog"); for (int pin = 0; pin < 6; pin++) { // Read the analog input int value = analogRead(pin); @@ -61,7 +61,7 @@ void loop() { } // Create the "digital" array - JsonArray& digitalValues = root.createNestedArray("digital"); + JsonArray digitalValues = root.createNestedArray("digital"); for (int pin = 0; pin < 14; pin++) { // Read the digital input int value = digitalRead(pin); @@ -87,15 +87,4 @@ void loop() { delay(10000); } -// See also -// -------- -// -// The website arduinojson.org contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any -// serialization problem. -// Please check it out at: https://arduinojson.org/ -// -// The book "Mastering ArduinoJson" contains a tutorial on serialization. -// It begins with a simple example, then adds more features like serializing -// directly to a file or any stream. -// Please check it out at: https://arduinojson.org/book/ +// Visit https://arduinojson.org/v6/example/udp-beacon/ for more. diff --git a/examples/MsgPackParser/MsgPackParser.ino b/examples/MsgPackParser/MsgPackParser.ino index 0ee420aa..e1bdc918 100644 --- a/examples/MsgPackParser/MsgPackParser.ino +++ b/examples/MsgPackParser/MsgPackParser.ino @@ -55,7 +55,7 @@ void setup() { } // Get the root object in the document - JsonObject& root = doc.as(); + JsonObject root = doc.as(); // Fetch values. // @@ -77,15 +77,4 @@ void loop() { // not used in this example } -// See also -// -------- -// -// The website arduinojson.org contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any -// deserialization problem. -// Please check it out at: https://arduinojson.org/ -// -// The book "Mastering ArduinoJson" contains a tutorial on deserialization. -// It begins with a simple example, like the one above, and then adds more -// features like deserializing directly from a file or an HTTP request. -// Please check it out at: https://arduinojson.org/book/ +// Visit https://arduinojson.org/v6/example/msgpack-parser/ for more. diff --git a/examples/ProgmemExample/ProgmemExample.ino b/examples/ProgmemExample/ProgmemExample.ino index 3302fc2d..45a51c76 100644 --- a/examples/ProgmemExample/ProgmemExample.ino +++ b/examples/ProgmemExample/ProgmemExample.ino @@ -21,7 +21,7 @@ void setup() { // JsonBuffer. deserializeJson(doc, F("{\"sensor\":\"gps\",\"time\":1351824120," "\"data\":[48.756080,2.302038]}")); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); // You can use a Flash String to get an element of a JsonObject // No duplication is done. @@ -56,15 +56,4 @@ void loop() { // not used in this example } -// See also -// -------- -// -// The website arduinojson.org contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any memory -// problem. -// Please check it out at: https://arduinojson.org/ -// -// The book "Mastering ArduinoJson" contains a quick C++ course that explains -// how your microcontroller stores strings in memory. It also tells why you -// should not abuse Flash strings with ArduinoJson. -// Please check it out at: https://arduinojson.org/book/ +// Visit https://arduinojson.org/v6/example/progmem/ for more. diff --git a/examples/StringExample/StringExample.ino b/examples/StringExample/StringExample.ino index d888e620..967b9146 100644 --- a/examples/StringExample/StringExample.ino +++ b/examples/StringExample/StringExample.ino @@ -18,7 +18,7 @@ void setup() { String input = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; deserializeJson(doc, input); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); // You can use a String to get an element of a JsonObject // No duplication is done. @@ -62,14 +62,4 @@ void loop() { // not used in this example } -// See also -// -------- -// -// The website arduinojson.org contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any problem. -// Please check it out at: https://arduinojson.org/ -// -// The book "Mastering ArduinoJson" contains a quick C++ course that explains -// how your microcontroller stores strings in memory. On several occasions, it -// shows how you can avoid String in your program. -// Please check it out at: https://arduinojson.org/book/ +// Visit https://arduinojson.org/v6/example/string/ for more. diff --git a/src/ArduinoJson.hpp b/src/ArduinoJson.hpp index cf7e2023..949e6c30 100644 --- a/src/ArduinoJson.hpp +++ b/src/ArduinoJson.hpp @@ -7,13 +7,20 @@ #include "ArduinoJson/version.hpp" #include "ArduinoJson/DynamicJsonDocument.hpp" +#include "ArduinoJson/StaticJsonDocument.hpp" + +#include "ArduinoJson/JsonObjectImpl.hpp" + +#include "ArduinoJson/JsonArray.hpp" +#include "ArduinoJson/JsonObject.hpp" + +#include "ArduinoJson/JsonArrayImpl.hpp" +#include "ArduinoJson/JsonArraySubscript.hpp" +#include "ArduinoJson/JsonObjectSubscript.hpp" +#include "ArduinoJson/JsonVariantImpl.hpp" + #include "ArduinoJson/Json/JsonDeserializer.hpp" #include "ArduinoJson/Json/JsonSerializer.hpp" #include "ArduinoJson/Json/PrettyJsonSerializer.hpp" #include "ArduinoJson/MsgPack/MsgPackDeserializer.hpp" #include "ArduinoJson/MsgPack/MsgPackSerializer.hpp" -#include "ArduinoJson/StaticJsonDocument.hpp" - -#include "ArduinoJson/JsonArrayImpl.hpp" -#include "ArduinoJson/JsonObjectImpl.hpp" -#include "ArduinoJson/JsonVariantImpl.hpp" diff --git a/src/ArduinoJson/Data/JsonVariantAs.hpp b/src/ArduinoJson/Data/JsonVariantAs.hpp index 8f202c5e..a3a08479 100644 --- a/src/ArduinoJson/Data/JsonVariantAs.hpp +++ b/src/ArduinoJson/Data/JsonVariantAs.hpp @@ -19,24 +19,5 @@ struct JsonVariantAs { typedef const char* type; }; -template <> -struct JsonVariantAs { - typedef JsonArray& type; -}; - -template <> -struct JsonVariantAs { - typedef const JsonArray& type; -}; - -template <> -struct JsonVariantAs { - typedef JsonObject& type; -}; - -template <> -struct JsonVariantAs { - typedef const JsonObject& type; -}; -} -} +} // namespace Internals +} // namespace ArduinoJson diff --git a/src/ArduinoJson/Data/JsonVariantContent.hpp b/src/ArduinoJson/Data/JsonVariantContent.hpp index c525a606..5b8403c7 100644 --- a/src/ArduinoJson/Data/JsonVariantContent.hpp +++ b/src/ArduinoJson/Data/JsonVariantContent.hpp @@ -8,20 +8,19 @@ #include "JsonInteger.hpp" namespace ArduinoJson { - -// Forward declarations -class JsonArray; -class JsonObject; - namespace Internals { +// Forward declarations +struct JsonArrayData; +struct JsonObjectData; + // A union that defines the actual content of a JsonVariant. // The enum JsonVariantType determines which member is in use. union JsonVariantContent { - JsonFloat asFloat; // used for double and float - JsonUInt asInteger; // used for bool, char, short, int and longs - const char* asString; // asString can be null - JsonArray* asArray; // asArray cannot be null - JsonObject* asObject; // asObject cannot be null + JsonFloat asFloat; // used for double and float + JsonUInt asInteger; // used for bool, char, short, int and longs + const char* asString; // asString can be null + JsonArrayData* asArray; // asArray cannot be null + JsonObjectData* asObject; // asObject cannot be null }; -} -} +} // namespace Internals +} // namespace ArduinoJson diff --git a/src/ArduinoJson/Data/JsonVariantType.hpp b/src/ArduinoJson/Data/JsonVariantType.hpp index 21f890e5..838460a3 100644 --- a/src/ArduinoJson/Data/JsonVariantType.hpp +++ b/src/ArduinoJson/Data/JsonVariantType.hpp @@ -5,8 +5,6 @@ #pragma once namespace ArduinoJson { -class JsonArray; -class JsonObject; namespace Internals { @@ -19,9 +17,9 @@ enum JsonVariantType { JSON_BOOLEAN, // JsonVariant stores a bool JSON_POSITIVE_INTEGER, // JsonVariant stores an JsonUInt JSON_NEGATIVE_INTEGER, // JsonVariant stores an JsonUInt that must be negated - JSON_ARRAY, // JsonVariant stores a pointer to a JsonArray - JSON_OBJECT, // JsonVariant stores a pointer to a JsonObject + JSON_ARRAY, // JsonVariant stores a pointer to a JsonArrayData + JSON_OBJECT, // JsonVariant stores a pointer to a JsonObjectData JSON_FLOAT // JsonVariant stores a JsonFloat }; -} -} +} // namespace Internals +} // namespace ArduinoJson diff --git a/src/ArduinoJson/Data/List.hpp b/src/ArduinoJson/Data/List.hpp index 185a6223..0982c012 100644 --- a/src/ArduinoJson/Data/List.hpp +++ b/src/ArduinoJson/Data/List.hpp @@ -13,7 +13,7 @@ namespace Internals { // A singly linked list of T. // The linked list is composed of ListNode. -// It is derived by JsonArray and JsonObject +// It is derived by JsonArrayData and JsonObjectData template class List { public: @@ -22,23 +22,10 @@ class List { typedef ListIterator iterator; typedef ListConstIterator const_iterator; - // Creates an empty List attached to a JsonBuffer. - // The JsonBuffer allows to allocate new nodes. - // When buffer is NULL, the List is not able to grow and success() returns - // false. This is used to identify bad memory allocations and parsing - // failures. explicit List(JsonBuffer *buf) : _buffer(buf), _firstNode(NULL) {} - // Returns true if the object is valid - // Would return false in the following situation: - // - the memory allocation failed (StaticJsonBuffer was too small) - // - the JSON parsing failed - bool success() const { - return _buffer != NULL; - } - // Returns the numbers of elements in the list. - // For a JsonObject, it would return the number of key-value pairs + // For a JsonObjectData, it would return the number of key-value pairs size_t size() const { size_t nodeCount = 0; for (node_type *node = _firstNode; node; node = node->next) nodeCount++; @@ -87,14 +74,13 @@ class List { JsonBuffer &buffer() const { return *_buffer; } + JsonBuffer *_buffer; // TODO!! protected: void clear() { _firstNode = 0; } - JsonBuffer *_buffer; - private: node_type *_firstNode; }; diff --git a/src/ArduinoJson/Data/ReferenceType.hpp b/src/ArduinoJson/Data/ReferenceType.hpp deleted file mode 100644 index 1e491172..00000000 --- a/src/ArduinoJson/Data/ReferenceType.hpp +++ /dev/null @@ -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; - } -}; -} -} diff --git a/src/ArduinoJson/DynamicJsonDocument.hpp b/src/ArduinoJson/DynamicJsonDocument.hpp index cc68fd99..4f5378e7 100644 --- a/src/ArduinoJson/DynamicJsonDocument.hpp +++ b/src/ArduinoJson/DynamicJsonDocument.hpp @@ -32,28 +32,26 @@ class DynamicJsonDocument { return _root.as(); } - // JsonObject& to() + // JsonObject to() template typename Internals::enable_if::value, - JsonObject&>::type + JsonObject>::type to() { clear(); - JsonObject* object = new (&_buffer) JsonObject(&_buffer); - if (!object) return JsonObject::invalid(); + JsonObject object(&_buffer); _root = object; - return *object; + return object; } - // JsonArray& to() + // JsonArray to() template typename Internals::enable_if::value, - JsonArray&>::type + JsonArray>::type to() { clear(); - JsonArray* array = new (&_buffer) JsonArray(&_buffer); - if (!array) return JsonArray::invalid(); + JsonArray array(&_buffer); _root = array; - return *array; + return array; } // JsonVariant& to() diff --git a/src/ArduinoJson/Json/JsonDeserializer.hpp b/src/ArduinoJson/Json/JsonDeserializer.hpp index 7f6f72dd..c1314b09 100644 --- a/src/ArduinoJson/Json/JsonDeserializer.hpp +++ b/src/ArduinoJson/Json/JsonDeserializer.hpp @@ -66,8 +66,8 @@ class JsonDeserializer { DeserializationError parseArray(JsonVariant &variant) { if (_nestingLimit == 0) return DeserializationError::TooDeep; - JsonArray *array = new (_buffer) JsonArray(_buffer); - if (!array) return DeserializationError::NoMemory; + JsonArray array(_buffer); + if (array.isNull()) return DeserializationError::NoMemory; variant = array; // Check opening braket @@ -88,7 +88,7 @@ class JsonDeserializer { err = parse(value); _nestingLimit++; if (err) return err; - if (!array->add(value)) return DeserializationError::NoMemory; + if (!array.add(value)) return DeserializationError::NoMemory; // 2 - Skip spaces err = skipSpacesAndComments(); @@ -103,8 +103,8 @@ class JsonDeserializer { DeserializationError parseObject(JsonVariant &variant) { if (_nestingLimit == 0) return DeserializationError::TooDeep; - JsonObject *object = new (_buffer) JsonObject(_buffer); - if (!object) return DeserializationError::NoMemory; + JsonObject object(_buffer); + if (object.isNull()) return DeserializationError::NoMemory; variant = object; // Check opening brace @@ -126,9 +126,7 @@ class JsonDeserializer { // Skip spaces err = skipSpacesAndComments(); - if (err) return err; - - // Colon + if (err) return err; // Colon if (!eat(':')) return DeserializationError::InvalidInput; // Parse value @@ -137,7 +135,7 @@ class JsonDeserializer { err = parse(value); _nestingLimit++; if (err) return err; - if (!object->set(key, value)) return DeserializationError::NoMemory; + if (!object.set(key, value)) return DeserializationError::NoMemory; // Skip spaces err = skipSpacesAndComments(); diff --git a/src/ArduinoJson/Json/JsonSerializer.hpp b/src/ArduinoJson/Json/JsonSerializer.hpp index bd9dca30..3f0989cf 100644 --- a/src/ArduinoJson/Json/JsonSerializer.hpp +++ b/src/ArduinoJson/Json/JsonSerializer.hpp @@ -75,7 +75,9 @@ class JsonSerializer { _writer.writeBoolean(value); } - void acceptUndefined() {} + void acceptNull() { + _writer.writeRaw("null"); + } size_t bytesWritten() const { return _writer.bytesWritten(); diff --git a/src/ArduinoJson/Json/JsonWriter.hpp b/src/ArduinoJson/Json/JsonWriter.hpp index 93d7694c..31071eea 100644 --- a/src/ArduinoJson/Json/JsonWriter.hpp +++ b/src/ArduinoJson/Json/JsonWriter.hpp @@ -13,13 +13,6 @@ namespace ArduinoJson { namespace Internals { -// Writes the JSON tokens to a Print implementation -// This class is used by: -// - JsonArray::writeTo() -// - JsonObject::writeTo() -// - JsonVariant::writeTo() -// Its derived by PrettyJsonWriter that overrides some members to add -// indentation. template class JsonWriter { public: diff --git a/src/ArduinoJson/JsonArray.hpp b/src/ArduinoJson/JsonArray.hpp index c1316607..b223960b 100644 --- a/src/ArduinoJson/JsonArray.hpp +++ b/src/ArduinoJson/JsonArray.hpp @@ -4,91 +4,126 @@ #pragma once -#include "Data/List.hpp" -#include "Data/ReferenceType.hpp" -#include "Data/ValueSaver.hpp" -#include "JsonVariant.hpp" -#include "Memory/JsonBufferAllocated.hpp" -#include "Polyfills/type_traits.hpp" -#include "Strings/StringTraits.hpp" - -// Returns the size (in bytes) of an array with n elements. -// Can be very handy to determine the size of a StaticJsonBuffer. -#define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \ - (sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(JsonArray::node_type)) +#include "./JsonArrayData.hpp" namespace ArduinoJson { -// Forward declarations class JsonObject; -class JsonBuffer; + namespace Internals { class JsonArraySubscript; } -class JsonArray : public Internals::ReferenceType, - public Internals::NonCopyable, - public Internals::List, - public Internals::JsonBufferAllocated { +class JsonArray { + friend class JsonVariant; + public: - explicit JsonArray(Internals::JsonBuffer *buf) throw() - : Internals::List(buf) {} + typedef Internals::JsonArrayData::iterator iterator; + typedef Internals::JsonArrayData::const_iterator const_iterator; - // Gets the value at the specified index - const Internals::JsonArraySubscript operator[](size_t index) const; - - // Gets or sets the value at specified index - Internals::JsonArraySubscript operator[](size_t index); + JsonArray() : _data(0) {} + JsonArray(Internals::JsonArrayData* arr) : _data(arr) {} + JsonArray(Internals::JsonBuffer* buf) + : _data(new (buf) Internals::JsonArrayData(buf)) {} // Adds the specified value at the end of the array. // // bool add(TValue); // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, - // std::string, String, JsonArray, JsonObject + // std::string, String, JsonArrayData, JsonObject template - bool add(const T &value) { - return add_impl(value); + bool add(const T& value) { + return add_impl(value); } // // bool add(TValue); // TValue = char*, const char*, const FlashStringHelper* template - bool add(T *value) { - return add_impl(value); - } - // - // bool add(TValue value, uint8_t decimals); - // TValue = float, double - template - DEPRECATED("Second argument is not supported anymore") - bool add(T value, uint8_t) { - return add_impl(JsonVariant(value)); + bool add(T* value) { + return add_impl(value); } - // Sets the value at specified index. - // - // bool add(size_t index, const TValue&); - // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, - // std::string, String, JsonArray, JsonObject - template - bool set(size_t index, const T &value) { - return set_impl(index, value); + iterator begin() { + if (!_data) return iterator(); + return _data->begin(); } - // - // bool add(size_t index, TValue); - // TValue = char*, const char*, const FlashStringHelper* - template - bool set(size_t index, T *value) { - return set_impl(index, value); + + const_iterator begin() const { + if (!_data) return const_iterator(); + return _data->begin(); } - // - // bool set(size_t index, TValue value, uint8_t decimals); - // TValue = float, double + + iterator end() { + return iterator(); + } + + const_iterator end() const { + return const_iterator(); + } + + // Imports a 1D array + template + bool copyFrom(T (&array)[N]) { + return copyFrom(array, N); + } + + // Imports a 1D array template - typename Internals::enable_if::value, - bool>::type - set(size_t index, T value, uint8_t decimals) { - return set_impl(index, JsonVariant(value, decimals)); + bool copyFrom(T* array, size_t len) { + bool ok = true; + for (size_t i = 0; i < len; i++) { + ok &= add(array[i]); + } + return ok; + } + + // Imports a 2D array + template + bool copyFrom(T (&array)[N1][N2]) { + bool ok = true; + for (size_t i = 0; i < N1; i++) { + JsonArray nestedArray = createNestedArray(); + for (size_t j = 0; j < N2; j++) { + ok &= nestedArray.add(array[i][j]); + } + } + return ok; + } + + // Exports a 1D array + template + size_t copyTo(T (&array)[N]) const { + return copyTo(array, N); + } + + // Exports a 1D array + template + size_t copyTo(T* array, size_t len) const { + size_t i = 0; + for (const_iterator it = begin(); it != end() && i < len; ++it) + array[i++] = *it; + return i; + } + + // Exports a 2D array + template + void copyTo(T (&array)[N1][N2]) const { + if (!_data) return; + size_t i = 0; + for (const_iterator it = begin(); it != end() && i < N1; ++it) { + it->as().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. @@ -105,82 +140,51 @@ class JsonArray : public Internals::ReferenceType, return it != end() ? it->is() : 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 position. + void remove(iterator it) { + if (!_data) return; + _data->remove(it); + } // Removes element at specified index. void remove(size_t index) { remove(begin() += index); } - using Internals::List::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 - template - bool copyFrom(T (&array)[N]) { - return copyFrom(array, N); - } - - // Imports a 1D array + // 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 - bool copyFrom(T *array, size_t len) { - bool ok = true; - for (size_t i = 0; i < len; i++) { - ok &= add(array[i]); - } - return ok; + bool set(size_t index, const T& value) { + if (!_data) return false; + return set_impl(index, value); } - - // Imports a 2D array - template - bool copyFrom(T (&array)[N1][N2]) { - bool ok = true; - for (size_t i = 0; i < N1; i++) { - JsonArray &nestedArray = createNestedArray(); - for (size_t j = 0; j < N2; j++) { - ok &= nestedArray.add(array[i][j]); - } - } - return ok; - } - - // Exports a 1D array - template - size_t copyTo(T (&array)[N]) const { - return copyTo(array, N); - } - - // Exports a 1D array + // + // bool add(size_t index, TValue); + // TValue = char*, const char*, const FlashStringHelper* template - size_t copyTo(T *array, size_t len) const { - size_t i = 0; - for (const_iterator it = begin(); it != end() && i < len; ++it) - array[i++] = *it; - return i; + bool set(size_t index, T* value) { + if (!_data) return false; + return set_impl(index, value); } - // Exports a 2D array - template - void copyTo(T (&array)[N1][N2]) const { - size_t i = 0; - for (const_iterator it = begin(); it != end() && i < N1; ++it) { - it->as().copyTo(array[i++]); - } + size_t size() const { + if (!_data) return 0; + return _data->size(); + } + + bool isNull() const { + return _data == 0; } template - void visit(Visitor &visitor) const { - return visitor.acceptArray(*this); + void visit(Visitor& visitor) const { + if (_data) + return visitor.acceptArray(*this); + else + visitor.acceptNull(); } private: @@ -188,23 +192,17 @@ class JsonArray : public Internals::ReferenceType, bool set_impl(size_t index, TValueRef value) { iterator it = begin() += index; if (it == end()) return false; - return Internals::ValueSaver::save(_buffer, *it, value); + return Internals::ValueSaver::save(_data->_buffer, *it, value); } template bool add_impl(TValueRef value) { - iterator it = Internals::List::add(); + if (!_data) return false; + iterator it = _data->add(); if (it == end()) return false; - return Internals::ValueSaver::save(_buffer, *it, value); + return Internals::ValueSaver::save(_data->_buffer, *it, value); } -}; -namespace Internals { -template <> -struct JsonVariantDefault { - static JsonArray &get() { - return JsonArray::invalid(); - } + Internals::JsonArrayData* _data; }; -} // namespace Internals } // namespace ArduinoJson diff --git a/src/ArduinoJson/JsonArrayData.hpp b/src/ArduinoJson/JsonArrayData.hpp new file mode 100644 index 00000000..152a7008 --- /dev/null +++ b/src/ArduinoJson/JsonArrayData.hpp @@ -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, JsonBufferAllocated { + explicit JsonArrayData(JsonBuffer *buf) throw() : List(buf) {} +}; +} // namespace Internals +} // namespace ArduinoJson diff --git a/src/ArduinoJson/JsonArrayImpl.hpp b/src/ArduinoJson/JsonArrayImpl.hpp index d84a8476..7f8f9018 100644 --- a/src/ArduinoJson/JsonArrayImpl.hpp +++ b/src/ArduinoJson/JsonArrayImpl.hpp @@ -5,24 +5,21 @@ #pragma once #include "JsonArray.hpp" -#include "JsonArraySubscript.hpp" #include "JsonObject.hpp" namespace ArduinoJson { -inline JsonArray &JsonArray::createNestedArray() { - JsonArray *array = new (_buffer) JsonArray(_buffer); - if (!array) return JsonArray::invalid(); - - add(array); - return *array; +inline JsonArray JsonArray::createNestedArray() { + if (!_data) return JsonArray(); + JsonArray array(_data->_buffer); + if (!array.isNull()) add(array); + return array; } -inline JsonObject &JsonArray::createNestedObject() { - JsonObject *object = new (_buffer) JsonObject(_buffer); - if (!object) return JsonObject::invalid(); - - add(object); - return *object; +inline JsonObject JsonArray::createNestedObject() { + if (!_data) return JsonObject(); + JsonObject object(_data->_buffer); + if (!object.isNull()) add(object); + return object; } } // namespace ArduinoJson diff --git a/src/ArduinoJson/JsonArraySubscript.hpp b/src/ArduinoJson/JsonArraySubscript.hpp index 67ded3d3..66e1eb47 100644 --- a/src/ArduinoJson/JsonArraySubscript.hpp +++ b/src/ArduinoJson/JsonArraySubscript.hpp @@ -16,7 +16,7 @@ namespace ArduinoJson { namespace Internals { class JsonArraySubscript : public JsonVariantBase { public: - FORCE_INLINE JsonArraySubscript(JsonArray& array, size_t index) + FORCE_INLINE JsonArraySubscript(JsonArray array, size_t index) : _array(array), _index(index) {} FORCE_INLINE JsonArraySubscript& operator=(const JsonArraySubscript& src) { @@ -43,8 +43,8 @@ class JsonArraySubscript : public JsonVariantBase { return *this; } - FORCE_INLINE bool success() const { - return _index < _array.size(); + FORCE_INLINE bool isNull() const { + return _index >= _array.size(); } template @@ -80,7 +80,7 @@ class JsonArraySubscript : public JsonVariantBase { } private: - JsonArray& _array; + JsonArray _array; const size_t _index; }; @@ -103,7 +103,7 @@ inline Internals::JsonArraySubscript JsonArray::operator[](size_t index) { inline const Internals::JsonArraySubscript JsonArray::operator[]( size_t index) const { - return Internals::JsonArraySubscript(*const_cast(this), index); + return Internals::JsonArraySubscript(*this, index); } } // namespace ArduinoJson diff --git a/src/ArduinoJson/JsonObject.hpp b/src/ArduinoJson/JsonObject.hpp index 0bb050c7..17f7ac25 100644 --- a/src/ArduinoJson/JsonObject.hpp +++ b/src/ArduinoJson/JsonObject.hpp @@ -4,110 +4,82 @@ #pragma once -#include "Data/List.hpp" -#include "Data/ReferenceType.hpp" -#include "Data/ValueSaver.hpp" -#include "JsonPair.hpp" -#include "Memory/JsonBufferAllocated.hpp" -#include "Polyfills/type_traits.hpp" -#include "Strings/StringTraits.hpp" - -// Returns the size (in bytes) of an object with n elements. -// Can be very handy to determine the size of a StaticJsonBuffer. -#define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \ - (sizeof(JsonObject) + (NUMBER_OF_ELEMENTS) * sizeof(JsonObject::node_type)) +#include "./JsonObjectData.hpp" namespace ArduinoJson { -// Forward declarations -class JsonArray; -class JsonBuffer; -namespace Internals { -template -class JsonObjectSubscript; -} +class JsonObject { + friend class JsonVariant; -class JsonObject : public Internals::ReferenceType, - public Internals::NonCopyable, - public Internals::List, - public Internals::JsonBufferAllocated { public: - // Create an empty JsonArray attached to the specified JsonBuffer. - // You should not use this constructor directly. - explicit JsonObject(Internals::JsonBuffer* buf) throw() - : Internals::List(buf) {} + typedef Internals::JsonObjectData::iterator iterator; + typedef Internals::JsonObjectData::const_iterator const_iterator; - // Gets or sets the value associated with the specified key. - // - // JsonObjectSubscript operator[](TKey) - // TKey = const std::string&, const String& - template - Internals::JsonObjectSubscript operator[]( - const TString& key) { - return Internals::JsonObjectSubscript(*this, key); - } - // - // JsonObjectSubscript operator[](TKey) - // TKey = char*, const char*, char[], const char[N], const FlashStringHelper* - template - Internals::JsonObjectSubscript operator[](TString* key) { - return Internals::JsonObjectSubscript(*this, key); + JsonObject() : _data(0) {} + JsonObject(Internals::JsonObjectData* object) : _data(object) {} + JsonObject(Internals::JsonBuffer* buf) + : _data(new (buf) Internals::JsonObjectData(buf)) {} + + iterator begin() { + if (!_data) return iterator(); + return _data->begin(); } - // Gets the value associated with the specified key. - // - // const JsonObjectSubscript operator[](TKey) const; - // TKey = const std::string&, const String& - template - const Internals::JsonObjectSubscript operator[]( - const TString& key) const { - return Internals::JsonObjectSubscript( - *const_cast(this), key); - } - // - // const JsonObjectSubscript operator[](TKey) const; - // TKey = const char*, const char[N], const FlashStringHelper* - template - const Internals::JsonObjectSubscript operator[]( - TString* key) const { - return Internals::JsonObjectSubscript( - *const_cast(this), key); + const_iterator begin() const { + if (!_data) return const_iterator(); + return _data->begin(); } - // Sets the specified key with the specified value. + // Tells weither the specified key is present and associated with a value. // - // bool set(TKey, TValue); + // bool containsKey(TKey); // TKey = const std::string&, const String& - // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, - // std::string, String, JsonArray, JsonObject - template - bool set(const TString& key, const TValue& value) { - return set_impl(key, value); + template + bool containsKey(const TString& key) const { + return containsKey_impl(key); } // - // bool set(TKey, TValue); + // bool containsKey(TKey); + // TKey = char*, const char*, char[], const char[], const FlashStringHelper* + template + bool containsKey(TString* key) const { + return containsKey_impl(key); + } + + iterator end() { + return iterator(); + } + + const_iterator end() const { + return const_iterator(); + } + + // Creates and adds a JsonArray. + // + // JsonArray createNestedArray(TKey); // TKey = const std::string&, const String& - // TValue = char*, const char*, const FlashStringHelper* - template - bool set(const TString& key, TValue* value) { - return set_impl(key, value); + template + JsonArray createNestedArray(const TString& key); + // JsonArray createNestedArray(TKey); + // TKey = char*, const char*, char[], const char[], const FlashStringHelper* + template + JsonArray createNestedArray(TString* key); + + // Creates and adds a JsonObject. + // + // JsonObject createNestedObject(TKey); + // TKey = const std::string&, const String& + template + JsonObject createNestedObject(const TString& key) { + if (!_data) return JsonObject(); + return createNestedObject_impl(key); } // - // bool set(TKey, const TValue&); - // TKey = char*, const char*, const FlashStringHelper* - // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, - // std::string, String, JsonArray, JsonObject - template - bool set(TString* key, const TValue& value) { - return set_impl(key, value); - } - // - // bool set(TKey, TValue); - // TKey = char*, const char*, const FlashStringHelper* - // TValue = char*, const char*, const FlashStringHelper* - template - bool set(TString* key, TValue* value) { - return set_impl(key, value); + // JsonObject createNestedObject(TKey); + // TKey = char*, const char*, char[], const char[], const FlashStringHelper* + template + JsonObject createNestedObject(TString* key) { + return createNestedObject_impl(key); } // Gets the value associated with the specified key. @@ -152,51 +124,48 @@ class JsonObject : public Internals::ReferenceType, return is_impl(key); } - // Creates and adds a JsonArray. + // Gets or sets the value associated with the specified key. // - // JsonArray& createNestedArray(TKey); + // JsonObjectSubscript operator[](TKey) // TKey = const std::string&, const String& template - JsonArray& createNestedArray(const TString& key) { - return createNestedArray_impl(key); + Internals::JsonObjectSubscript operator[]( + const TString& key) { + return Internals::JsonObjectSubscript(*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 - JsonArray& createNestedArray(TString* key) { - return createNestedArray_impl(key); + Internals::JsonObjectSubscript operator[](TString* key) { + return Internals::JsonObjectSubscript(*this, key); } - // Creates and adds a JsonObject. + // Gets the value associated with the specified key. // - // JsonObject& createNestedObject(TKey); + // const JsonObjectSubscript operator[](TKey) const; // TKey = const std::string&, const String& template - JsonObject& createNestedObject(const TString& key) { - return createNestedObject_impl(key); + const Internals::JsonObjectSubscript operator[]( + const TString& key) const { + return Internals::JsonObjectSubscript(*this, key); } // - // JsonObject& createNestedObject(TKey); - // TKey = char*, const char*, char[], const char[], const FlashStringHelper* + // const JsonObjectSubscript operator[](TKey) const; + // TKey = const char*, const char[N], const FlashStringHelper* template - JsonObject& createNestedObject(TString* key) { - return createNestedObject_impl(key); + const Internals::JsonObjectSubscript operator[]( + TString* key) const { + return Internals::JsonObjectSubscript(*this, key); } - // Tells weither the specified key is present and associated with a value. - // - // bool containsKey(TKey); - // TKey = const std::string&, const String& - template - bool containsKey(const TString& key) const { - return findKey(key) != end(); + bool operator==(const JsonObject& rhs) const { + return _data == rhs._data; } - // - // bool containsKey(TKey); - // TKey = char*, const char*, char[], const char[], const FlashStringHelper* - template - bool containsKey(TString* key) const { - return findKey(key) != end(); + + void remove(iterator it) { + if (!_data) return; + _data->remove(it); } // Removes the specified key and the associated value. @@ -205,33 +174,81 @@ class JsonObject : public Internals::ReferenceType, // TKey = const std::string&, const String& template void remove(const TString& key) { - remove(findKey(key)); + remove_impl(key); } // // void remove(TKey); // TKey = char*, const char*, char[], const char[], const FlashStringHelper* template void remove(TString* key) { - remove(findKey(key)); + remove_impl(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 + bool set(const TString& key, const TValue& value) { + return set_impl(key, value); } // - // void remove(iterator) - using Internals::List::remove; + // bool set(TKey, TValue); + // TKey = const std::string&, const String& + // TValue = char*, const char*, const FlashStringHelper* + template + bool set(const TString& key, TValue* value) { + return set_impl(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 + bool set(TString* key, const TValue& value) { + return set_impl(key, value); + } + // + // bool set(TKey, TValue); + // TKey = char*, const char*, const FlashStringHelper* + // TValue = char*, const char*, const FlashStringHelper* + template + bool set(TString* key, TValue* value) { + return set_impl(key, value); + } - // Returns a reference an invalid JsonObject. - // This object is meant to replace a NULL pointer. - // This is used when memory allocation or JSON parsing fail. - static JsonObject& invalid() { - static JsonObject instance(NULL); - return instance; + size_t size() const { + if (!_data) return 0; + return _data->size(); + } + + bool isNull() const { + return _data == 0; } template void visit(Visitor& visitor) const { - return visitor.acceptObject(*this); + if (_data) + visitor.acceptObject(*this); + else + return visitor.acceptNull(); } private: + template + bool containsKey_impl(TStringRef key) const { + return findKey(key) != end(); + } + + template + JsonArray createNestedArray_impl(TStringRef key); + + template + JsonObject createNestedObject_impl(TStringRef key); + // Returns the list node that matches the specified key. template iterator findKey(TStringRef key) { @@ -254,26 +271,6 @@ class JsonObject : public Internals::ReferenceType, : Internals::JsonVariantDefault::get(); } - template - bool set_impl(TStringRef key, TValueRef value) { - // ignore null key - if (Internals::StringTraits::is_null(key)) return false; - - // search a matching key - iterator it = findKey(key); - if (it == end()) { - // add the key - it = Internals::List::add(); - if (it == end()) return false; - bool key_ok = - Internals::ValueSaver::save(_buffer, it->key, key); - if (!key_ok) return false; - } - - // save the value - return Internals::ValueSaver::save(_buffer, it->value, value); - } - template bool is_impl(TStringRef key) const { const_iterator it = findKey(key); @@ -281,18 +278,34 @@ class JsonObject : public Internals::ReferenceType, } template - JsonArray& createNestedArray_impl(TStringRef key); - - template - JsonObject& createNestedObject_impl(TStringRef key); -}; - -namespace Internals { -template <> -struct JsonVariantDefault { - static JsonObject& get() { - return JsonObject::invalid(); + void remove_impl(TStringRef key) { + if (!_data) return; + _data->remove(findKey(key)); } + + template + bool set_impl(TStringRef key, TValueRef value) { + if (!_data) return false; + + // ignore null key + if (Internals::StringTraits::is_null(key)) return false; + + // search a matching key + iterator it = findKey(key); + if (it == end()) { + // add the key + it = _data->add(); + if (it == end()) return false; + bool key_ok = + Internals::ValueSaver::save(_data->_buffer, it->key, key); + if (!key_ok) return false; + } + + // save the value + return Internals::ValueSaver::save(_data->_buffer, it->value, + value); + } + + Internals::JsonObjectData* _data; }; -} // namespace Internals } // namespace ArduinoJson diff --git a/src/ArduinoJson/JsonObjectData.hpp b/src/ArduinoJson/JsonObjectData.hpp new file mode 100644 index 00000000..5a9cf0a0 --- /dev/null +++ b/src/ArduinoJson/JsonObjectData.hpp @@ -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, JsonBufferAllocated { + explicit JsonObjectData(JsonBuffer* buf) throw() : List(buf) {} +}; +} // namespace Internals +} // namespace ArduinoJson diff --git a/src/ArduinoJson/JsonObjectImpl.hpp b/src/ArduinoJson/JsonObjectImpl.hpp index eabed38a..ebc64173 100644 --- a/src/ArduinoJson/JsonObjectImpl.hpp +++ b/src/ArduinoJson/JsonObjectImpl.hpp @@ -6,23 +6,32 @@ #include "JsonArray.hpp" #include "JsonObject.hpp" -#include "JsonObjectSubscript.hpp" namespace ArduinoJson { -template -inline JsonArray &JsonObject::createNestedArray_impl(TStringRef key) { - JsonArray *array = new (_buffer) JsonArray(_buffer); - if (!array) return JsonArray::invalid(); - set(key, array); - return *array; +template +inline JsonArray JsonObject::createNestedArray(const TString& key) { + return createNestedArray_impl(key); +} + +template +inline JsonArray JsonObject::createNestedArray(TString* key) { + return createNestedArray_impl(key); } template -inline JsonObject &JsonObject::createNestedObject_impl(TStringRef key) { - JsonObject *object = new (_buffer) JsonObject(_buffer); - if (!object) return JsonObject::invalid(); - set(key, object); - return *object; +inline JsonArray JsonObject::createNestedArray_impl(TStringRef key) { + if (!_data) return JsonArray(); + JsonArray array(_data->_buffer); + if (!array.isNull()) set(key, array); + return array; +} + +template +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 diff --git a/src/ArduinoJson/JsonObjectSubscript.hpp b/src/ArduinoJson/JsonObjectSubscript.hpp index 87062309..9f758647 100644 --- a/src/ArduinoJson/JsonObjectSubscript.hpp +++ b/src/ArduinoJson/JsonObjectSubscript.hpp @@ -22,10 +22,10 @@ class JsonObjectSubscript typedef JsonObjectSubscript this_type; public: - FORCE_INLINE JsonObjectSubscript(JsonObject& object, TStringRef key) + FORCE_INLINE JsonObjectSubscript(JsonObject object, TStringRef key) : _object(object), _key(key) {} - FORCE_INLINE this_type& operator=(const this_type& src) { + FORCE_INLINE this_type &operator=(const this_type &src) { _object.set(_key, src); return *this; } @@ -36,8 +36,8 @@ class JsonObjectSubscript // TValue = bool, char, long, int, short, float, double, // std::string, String, JsonArray, JsonObject template - FORCE_INLINE typename enable_if::value, this_type&>::type - operator=(const TValue& src) { + FORCE_INLINE typename enable_if::value, this_type &>::type + operator=(const TValue &src) { _object.set(_key, src); return *this; } @@ -45,13 +45,13 @@ class JsonObjectSubscript // operator=(TValue); // TValue = char*, const char*, const FlashStringHelper* template - FORCE_INLINE this_type& operator=(TValue* src) { + FORCE_INLINE this_type &operator=(TValue *src) { _object.set(_key, src); return *this; } - FORCE_INLINE bool success() const { - return _object.containsKey(_key); + FORCE_INLINE bool isNull() const { + return !_object.containsKey(_key); } template @@ -71,26 +71,59 @@ class JsonObjectSubscript // std::string, String, JsonArray, JsonObject template FORCE_INLINE typename enable_if::value, bool>::type set( - const TValue& value) { + const TValue &value) { return _object.set(_key, value); } // // bool set(TValue); // TValue = char*, const char, const FlashStringHelper* template - FORCE_INLINE bool set(const TValue* value) { + FORCE_INLINE bool set(const TValue *value) { return _object.set(_key, value); } template - void visit(Visitor& visitor) const { + void visit(Visitor &visitor) const { return _object.get(_key).visit(visitor); } private: - JsonObject& _object; + JsonObject _object; TStringRef _key; }; + +template +template +inline typename enable_if::has_equals, + const JsonObjectSubscript >::type + JsonVariantSubscripts::operator[](const TString &key) const { + return impl()->template as()[key]; +} + +template +template +inline typename enable_if::has_equals, + JsonObjectSubscript >::type + JsonVariantSubscripts::operator[](const TString &key) { + return impl()->template as()[key]; +} + +template +template +inline typename enable_if::has_equals, + JsonObjectSubscript >::type + JsonVariantSubscripts::operator[](const TString *key) { + return impl()->template as()[key]; +} + +template +template +inline typename enable_if::has_equals, + const JsonObjectSubscript >::type + JsonVariantSubscripts::operator[](const TString *key) const { + return impl()->template as()[key]; +} + } // namespace Internals } // namespace ArduinoJson diff --git a/src/ArduinoJson/JsonPair.hpp b/src/ArduinoJson/JsonPair.hpp index 41724304..1748f877 100644 --- a/src/ArduinoJson/JsonPair.hpp +++ b/src/ArduinoJson/JsonPair.hpp @@ -8,9 +8,9 @@ namespace ArduinoJson { -// A key value pair for JsonObject. +// A key value pair for JsonObjectData. struct JsonPair { const char* key; JsonVariant value; }; -} +} // namespace ArduinoJson diff --git a/src/ArduinoJson/JsonVariant.hpp b/src/ArduinoJson/JsonVariant.hpp index 521fce02..274e960c 100644 --- a/src/ArduinoJson/JsonVariant.hpp +++ b/src/ArduinoJson/JsonVariant.hpp @@ -104,25 +104,8 @@ class JsonVariant : public Internals::JsonVariantBase { _content.asString = value; } - // Create a JsonVariant containing a reference to an array. - // CAUTION: we are lying about constness, because the array can be modified if - // the variant is converted back to a JsonArray& - JsonVariant(const JsonArray &array); - - // Create a JsonVariant containing a reference to an object. - // CAUTION: we are lying about constness, because the object can be modified - // if the variant is converted back to a JsonObject& - JsonVariant(const JsonObject &object); - - JsonVariant(JsonArray *array) { - _content.asArray = array; - _type = Internals::JSON_ARRAY; - } - - JsonVariant(JsonObject *object) { - _content.asObject = object; - _type = Internals::JSON_OBJECT; - } + JsonVariant(JsonArray array); + JsonVariant(JsonObject object); // Get the variant as the specified type. // @@ -179,48 +162,23 @@ class JsonVariant : public Internals::JsonVariantBase { return s; } // - // JsonArray& as const; - // JsonArray& as const; + // JsonArray as() const; + // const JsonArray as() const; template typename Internals::enable_if< - Internals::is_same::type, + Internals::is_same::type, JsonArray>::value, - JsonArray &>::type - as() const { - return variantAsArray(); - } + JsonArray>::type + as() const; // - // const JsonArray& as const; + // JsonObject as() const; + // const JsonObject as() const; template typename Internals::enable_if< - Internals::is_same::type, - const JsonArray>::value, - const JsonArray &>::type - as() const { - return variantAsArray(); - } - // - // JsonObject& as const; - // JsonObject& as const; - template - typename Internals::enable_if< - Internals::is_same::type, + Internals::is_same::type, JsonObject>::value, - JsonObject &>::type - as() const { - return variantAsObject(); - } - // - // JsonObject& as const; - // JsonObject& as const; - template - typename Internals::enable_if< - Internals::is_same::type, - const JsonObject>::value, - const JsonObject &>::type - as() const { - return variantAsObject(); - } + T>::type + as() const; // // JsonVariant as const; template @@ -275,36 +233,30 @@ class JsonVariant : public Internals::JsonVariantBase { } // // bool is const; - // bool is const; - // bool is const; + // bool is const; template typename Internals::enable_if< - Internals::is_same< - typename Internals::remove_const< - typename Internals::remove_reference::type>::type, - JsonArray>::value, + Internals::is_same::type, + JsonArray>::value, bool>::type is() const { return variantIsArray(); } // // bool is const; - // bool is const; - // bool is const; + // bool is const; template typename Internals::enable_if< - Internals::is_same< - typename Internals::remove_const< - typename Internals::remove_reference::type>::type, - JsonObject>::value, + Internals::is_same::type, + JsonObject>::value, bool>::type is() const { return variantIsObject(); } // Returns true if the variant has a value - bool success() const { - return _type != Internals::JSON_UNDEFINED; + bool isNull() const { + return _type == Internals::JSON_UNDEFINED; } template @@ -315,10 +267,10 @@ class JsonVariant : public Internals::JsonVariantBase { return visitor.acceptFloat(_content.asFloat); case JSON_ARRAY: - return visitor.acceptArray(*_content.asArray); + return visitor.acceptArray(_content.asArray); case JSON_OBJECT: - return visitor.acceptObject(*_content.asObject); + return visitor.acceptObject(_content.asObject); case JSON_STRING: return visitor.acceptString(_content.asString); @@ -336,13 +288,13 @@ class JsonVariant : public Internals::JsonVariantBase { return visitor.acceptBoolean(_content.asInteger != 0); default: // JSON_UNDEFINED - return visitor.acceptUndefined(); + return visitor.acceptNull(); } } private: - JsonArray &variantAsArray() const; - JsonObject &variantAsObject() const; + JsonArray variantAsArray() const; + JsonObject variantAsObject() const; const char *variantAsString() const; template T variantAsFloat() const; diff --git a/src/ArduinoJson/JsonVariantCasts.hpp b/src/ArduinoJson/JsonVariantCasts.hpp index 3f4ee018..ae3c1c14 100644 --- a/src/ArduinoJson/JsonVariantCasts.hpp +++ b/src/ArduinoJson/JsonVariantCasts.hpp @@ -13,21 +13,6 @@ namespace Internals { template class JsonVariantCasts { public: - // Gets the variant as an array. - // Returns a reference to the JsonArray or JsonArray::invalid() if the - // variant - // is not an array. - FORCE_INLINE operator JsonArray &() const { - return impl()->template as(); - } - - // 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(); - } - template FORCE_INLINE operator T() const { return impl()->template as(); diff --git a/src/ArduinoJson/JsonVariantComparisons.hpp b/src/ArduinoJson/JsonVariantComparisons.hpp index 79d0c219..b08ff0c2 100644 --- a/src/ArduinoJson/JsonVariantComparisons.hpp +++ b/src/ArduinoJson/JsonVariantComparisons.hpp @@ -9,6 +9,9 @@ #include "Strings/StringTraits.hpp" namespace ArduinoJson { +class JsonArray; +class JsonObject; + namespace Internals { template diff --git a/src/ArduinoJson/JsonVariantImpl.hpp b/src/ArduinoJson/JsonVariantImpl.hpp index d7ffec08..b3c3d834 100644 --- a/src/ArduinoJson/JsonVariantImpl.hpp +++ b/src/ArduinoJson/JsonVariantImpl.hpp @@ -5,8 +5,8 @@ #pragma once #include "Configuration.hpp" -#include "JsonArray.hpp" -#include "JsonObject.hpp" +#include "JsonArrayData.hpp" +#include "JsonObjectData.hpp" #include "JsonVariant.hpp" #include "Numbers/isFloat.hpp" #include "Numbers/isInteger.hpp" @@ -17,32 +17,50 @@ namespace ArduinoJson { -inline JsonVariant::JsonVariant(const JsonArray &array) { - if (array.success()) { +inline JsonVariant::JsonVariant(JsonArray array) { + if (!array.isNull()) { _type = Internals::JSON_ARRAY; - _content.asArray = const_cast(&array); + _content.asArray = array._data; } else { _type = Internals::JSON_UNDEFINED; } } -inline JsonVariant::JsonVariant(const JsonObject &object) { - if (object.success()) { +inline JsonVariant::JsonVariant(JsonObject object) { + if (!object.isNull()) { _type = Internals::JSON_OBJECT; - _content.asObject = const_cast(&object); + _content.asObject = object._data; } else { _type = Internals::JSON_UNDEFINED; } } -inline JsonArray &JsonVariant::variantAsArray() const { - if (_type == Internals::JSON_ARRAY) return *_content.asArray; - return JsonArray::invalid(); +template +inline typename Internals::enable_if< + Internals::is_same::type, + JsonArray>::value, + JsonArray>::type +JsonVariant::as() const { + return variantAsArray(); } -inline JsonObject &JsonVariant::variantAsObject() const { - if (_type == Internals::JSON_OBJECT) return *_content.asObject; - return JsonObject::invalid(); +template +inline typename Internals::enable_if< + Internals::is_same::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 diff --git a/src/ArduinoJson/JsonVariantSubscripts.hpp b/src/ArduinoJson/JsonVariantSubscripts.hpp index d75ec052..2227df9a 100644 --- a/src/ArduinoJson/JsonVariantSubscripts.hpp +++ b/src/ArduinoJson/JsonVariantSubscripts.hpp @@ -10,6 +10,8 @@ #include "Strings/StringTraits.hpp" namespace ArduinoJson { +class JsonArray; +class JsonObject; namespace Internals { // Forward declarations. @@ -30,14 +32,12 @@ class JsonVariantSubscripts { // Mimics an array. // Returns the element at specified index if the variant is an array. - // Returns JsonVariant::invalid() if the variant is not an array. FORCE_INLINE const JsonArraySubscript operator[](size_t index) const; FORCE_INLINE JsonArraySubscript operator[](size_t index); // Mimics an object. // Returns the value associated with the specified key if the variant is // an object. - // Return JsonVariant::invalid() if the variant is not an object. // // const JsonObjectSubscript operator[](TKey) const; // TKey = const std::string&, const String& @@ -45,27 +45,21 @@ class JsonVariantSubscripts { FORCE_INLINE typename enable_if::has_equals, const JsonObjectSubscript >::type - operator[](const TString &key) const { - return impl()->template as()[key]; - } + operator[](const TString &key) const; // // const JsonObjectSubscript operator[](TKey) const; // TKey = const std::string&, const String& template FORCE_INLINE typename enable_if::has_equals, JsonObjectSubscript >::type - operator[](const TString &key) { - return impl()->template as()[key]; - } + operator[](const TString &key); // // JsonObjectSubscript operator[](TKey); // TKey = const char*, const char[N], const FlashStringHelper* template FORCE_INLINE typename enable_if::has_equals, JsonObjectSubscript >::type - operator[](const TString *key) { - return impl()->template as()[key]; - } + operator[](const TString *key); // // JsonObjectSubscript operator[](TKey); // TKey = const char*, const char[N], const FlashStringHelper* @@ -73,9 +67,7 @@ class JsonVariantSubscripts { FORCE_INLINE typename enable_if::has_equals, const JsonObjectSubscript >::type - operator[](const TString *key) const { - return impl()->template as()[key]; - } + operator[](const TString *key) const; private: const TImpl *impl() const { diff --git a/src/ArduinoJson/Memory/JsonBuffer.hpp b/src/ArduinoJson/Memory/JsonBuffer.hpp index dedf2e38..dd2f3483 100644 --- a/src/ArduinoJson/Memory/JsonBuffer.hpp +++ b/src/ArduinoJson/Memory/JsonBuffer.hpp @@ -9,7 +9,6 @@ #include #include "../Configuration.hpp" -#include "../Polyfills/NonCopyable.hpp" #include "../Polyfills/attributes.hpp" namespace ArduinoJson { @@ -17,7 +16,7 @@ namespace Internals { // Handle the memory management (done in derived classes) and calls the parser. // This abstract class is implemented by StaticJsonBuffer which implements a // fixed memory allocation. -class JsonBuffer : NonCopyable { +class JsonBuffer { public: // Allocates n bytes in the JsonBuffer. // Return a pointer to the allocated memory or NULL if allocation fails. diff --git a/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp b/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp index f90520c0..c87c0bb1 100644 --- a/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp +++ b/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp @@ -240,13 +240,13 @@ class MsgPackDeserializer { } DeserializationError readArray(JsonVariant &variant, size_t n) { - JsonArray *array = new (_buffer) JsonArray(_buffer); - if (!array) return DeserializationError::NoMemory; + JsonArray array(_buffer); + if (array.isNull()) return DeserializationError::NoMemory; variant = array; - return readArray(*array, n); + return readArray(array, n); } - DeserializationError readArray(JsonArray &array, size_t n) { + DeserializationError readArray(JsonArray array, size_t n) { if (_nestingLimit == 0) return DeserializationError::TooDeep; --_nestingLimit; for (; n; --n) { @@ -267,13 +267,13 @@ class MsgPackDeserializer { } DeserializationError readObject(JsonVariant &variant, size_t n) { - JsonObject *object = new (_buffer) JsonObject(_buffer); - if (!object) return DeserializationError::NoMemory; + JsonObject object(_buffer); + if (object.isNull()) return DeserializationError::NoMemory; variant = object; - return readObject(*object, n); + return readObject(object, n); } - DeserializationError readObject(JsonObject &object, size_t n) { + DeserializationError readObject(JsonObject object, size_t n) { if (_nestingLimit == 0) return DeserializationError::TooDeep; --_nestingLimit; for (; n; --n) { diff --git a/src/ArduinoJson/MsgPack/MsgPackSerializer.hpp b/src/ArduinoJson/MsgPack/MsgPackSerializer.hpp index eab7c781..0981f662 100644 --- a/src/ArduinoJson/MsgPack/MsgPackSerializer.hpp +++ b/src/ArduinoJson/MsgPack/MsgPackSerializer.hpp @@ -140,7 +140,7 @@ class MsgPackSerializer { writeByte(value ? 0xC3 : 0xC2); } - void acceptUndefined() { + void acceptNull() { writeByte(0xC0); } diff --git a/src/ArduinoJson/Polyfills/NonCopyable.hpp b/src/ArduinoJson/Polyfills/NonCopyable.hpp deleted file mode 100644 index 73f3d8ed..00000000 --- a/src/ArduinoJson/Polyfills/NonCopyable.hpp +++ /dev/null @@ -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&); -}; -} -} diff --git a/src/ArduinoJson/StaticJsonDocument.hpp b/src/ArduinoJson/StaticJsonDocument.hpp index 2ae234da..cb080169 100644 --- a/src/ArduinoJson/StaticJsonDocument.hpp +++ b/src/ArduinoJson/StaticJsonDocument.hpp @@ -33,28 +33,26 @@ class StaticJsonDocument { return _root.as(); } - // JsonObject& to() + // JsonObject to() template typename Internals::enable_if::value, - JsonObject&>::type + JsonObject>::type to() { clear(); - JsonObject* object = new (&_buffer) JsonObject(&_buffer); - if (!object) return JsonObject::invalid(); + JsonObject object(&_buffer); _root = object; - return *object; + return object; } - // JsonArray& to() + // JsonArray to() template typename Internals::enable_if::value, - JsonArray&>::type + JsonArray>::type to() { clear(); - JsonArray* array = new (&_buffer) JsonArray(&_buffer); - if (!array) return JsonArray::invalid(); + JsonArray array(&_buffer); _root = array; - return *array; + return array; } // JsonVariant to() diff --git a/test/IntegrationTests/gbathree.cpp b/test/IntegrationTests/gbathree.cpp index 9372fc1d..c731724d 100644 --- a/test/IntegrationTests/gbathree.cpp +++ b/test/IntegrationTests/gbathree.cpp @@ -20,7 +20,7 @@ TEST_CASE("Gbathree") { "\"measlights\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15," "15,15]],\"measlights2\":[[15,15,15,15],[15,15,15,15],[15,15,15,15]," "[15,15,15,15]],\"altc\":[2,2,2,2],\"altd\":[2,2,2,2]}"); - JsonObject& root = doc.as(); + JsonObject root = doc.as(); SECTION("Success") { REQUIRE(error == DeserializationError::Ok); @@ -81,8 +81,8 @@ TEST_CASE("Gbathree") { SECTION("Pulses") { // "pulses":[50,50,50] - JsonArray& array = root["pulses"]; - REQUIRE(array.success()); + JsonArray array = root["pulses"]; + REQUIRE(array.isNull() == false); REQUIRE(3 == array.size()); @@ -94,8 +94,8 @@ TEST_CASE("Gbathree") { SECTION("Act") { // "act":[2,1,2,2] - JsonArray& array = root["act"]; - REQUIRE(array.success()); + JsonArray array = root["act"]; + REQUIRE(array.isNull() == false); REQUIRE(4 == array.size()); REQUIRE(2 == array[0]); @@ -107,12 +107,12 @@ TEST_CASE("Gbathree") { SECTION("Detectors") { // "detectors":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]] - JsonArray& array = root["detectors"]; - REQUIRE(array.success()); + JsonArray array = root["detectors"]; + REQUIRE(array.isNull() == false); REQUIRE(4 == array.size()); for (size_t i = 0; i < 4; i++) { - JsonArray& nestedArray = array[i]; + JsonArray nestedArray = array[i]; REQUIRE(4 == nestedArray.size()); for (size_t j = 0; j < 4; j++) { @@ -124,8 +124,8 @@ TEST_CASE("Gbathree") { SECTION("Alta") { // alta:[2,2,2,2] - JsonArray& array = root["alta"]; - REQUIRE(array.success()); + JsonArray array = root["alta"]; + REQUIRE(array.isNull() == false); REQUIRE(4 == array.size()); @@ -137,8 +137,8 @@ TEST_CASE("Gbathree") { SECTION("Altb") { // altb:[2,2,2,2] - JsonArray& array = root["altb"]; - REQUIRE(array.success()); + JsonArray array = root["altb"]; + REQUIRE(array.isNull() == false); REQUIRE(4 == array.size()); @@ -150,12 +150,12 @@ TEST_CASE("Gbathree") { SECTION("Measlights") { // "measlights":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]] - JsonArray& array = root["measlights"]; - REQUIRE(array.success()); + JsonArray array = root["measlights"]; + REQUIRE(array.isNull() == false); REQUIRE(4 == array.size()); for (size_t i = 0; i < 4; i++) { - JsonArray& nestedArray = array[i]; + JsonArray nestedArray = array[i]; REQUIRE(4 == nestedArray.size()); @@ -168,12 +168,12 @@ TEST_CASE("Gbathree") { SECTION("Measlights2") { // "measlights2":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]] - JsonArray& array = root["measlights2"]; - REQUIRE(array.success()); + JsonArray array = root["measlights2"]; + REQUIRE(array.isNull() == false); REQUIRE(4 == array.size()); for (size_t i = 0; i < 4; i++) { - JsonArray& nestedArray = array[i]; + JsonArray nestedArray = array[i]; REQUIRE(4 == nestedArray.size()); for (size_t j = 0; j < 4; j++) { @@ -185,8 +185,8 @@ TEST_CASE("Gbathree") { SECTION("Altc") { // altc:[2,2,2,2] - JsonArray& array = root["altc"]; - REQUIRE(array.success()); + JsonArray array = root["altc"]; + REQUIRE(array.isNull() == false); REQUIRE(4 == array.size()); @@ -198,8 +198,8 @@ TEST_CASE("Gbathree") { SECTION("Altd") { // altd:[2,2,2,2] - JsonArray& array = root["altd"]; - REQUIRE(array.success()); + JsonArray array = root["altd"]; + REQUIRE(array.isNull() == false); REQUIRE(4 == array.size()); diff --git a/test/JsonArray/add.cpp b/test/JsonArray/add.cpp index b2d6034d..c2540a33 100644 --- a/test/JsonArray/add.cpp +++ b/test/JsonArray/add.cpp @@ -7,7 +7,7 @@ TEST_CASE("JsonArray::add()") { DynamicJsonDocument doc; - JsonArray& _array = doc.to(); + JsonArray _array = doc.to(); SECTION("int") { _array.add(123); @@ -40,30 +40,30 @@ TEST_CASE("JsonArray::add()") { SECTION("nested array") { DynamicJsonDocument doc2; - JsonArray& arr = doc2.to(); + JsonArray arr = doc2.to(); _array.add(arr); - REQUIRE(&arr == &_array[0].as()); - REQUIRE(_array[0].is()); + REQUIRE(arr == _array[0].as()); + REQUIRE(_array[0].is()); REQUIRE_FALSE(_array[0].is()); } SECTION("nested object") { DynamicJsonDocument doc2; - JsonObject& obj = doc2.to(); + JsonObject obj = doc2.to(); _array.add(obj); - REQUIRE(&obj == &_array[0].as()); - REQUIRE(_array[0].is()); + REQUIRE(obj == _array[0].as()); + REQUIRE(_array[0].is()); REQUIRE_FALSE(_array[0].is()); } SECTION("array subscript") { const char* str = "hello"; DynamicJsonDocument doc2; - JsonArray& arr = doc2.to(); + JsonArray arr = doc2.to(); arr.add(str); _array.add(arr[0]); @@ -74,7 +74,7 @@ TEST_CASE("JsonArray::add()") { SECTION("object subscript") { const char* str = "hello"; DynamicJsonDocument doc2; - JsonObject& obj = doc2.to(); + JsonObject obj = doc2.to(); obj["x"] = str; _array.add(obj["x"]); diff --git a/test/JsonArray/basics.cpp b/test/JsonArray/basics.cpp index 9c0b727d..0a263ee3 100644 --- a/test/JsonArray/basics.cpp +++ b/test/JsonArray/basics.cpp @@ -7,10 +7,10 @@ TEST_CASE("JsonArray basics") { DynamicJsonDocument doc; - JsonArray& array = doc.to(); + JsonArray array = doc.to(); - SECTION("SuccessIsTrue") { - REQUIRE(array.success()); + SECTION("isNull()") { + REQUIRE(array.isNull() == false); } SECTION("InitialSizeIsZero") { @@ -18,12 +18,12 @@ TEST_CASE("JsonArray basics") { } SECTION("CreateNestedArray") { - JsonArray& arr = array.createNestedArray(); - REQUIRE(&arr == &array[0].as()); + JsonArray arr = array.createNestedArray(); + REQUIRE(arr == array[0].as()); } SECTION("CreateNestedObject") { - JsonObject& obj = array.createNestedObject(); - REQUIRE(&obj == &array[0].as()); + JsonObject obj = array.createNestedObject(); + REQUIRE(obj == array[0].as()); } } diff --git a/test/JsonArray/copyFrom.cpp b/test/JsonArray/copyFrom.cpp index 60ba95ea..0838c074 100644 --- a/test/JsonArray/copyFrom.cpp +++ b/test/JsonArray/copyFrom.cpp @@ -8,7 +8,7 @@ TEST_CASE("JsonArray::copyFrom()") { SECTION("OneDimension") { DynamicJsonDocument doc; - JsonArray& array = doc.to(); + JsonArray array = doc.to(); char json[32]; int source[] = {1, 2, 3}; @@ -22,7 +22,7 @@ TEST_CASE("JsonArray::copyFrom()") { SECTION("OneDimension_JsonBufferTooSmall") { const size_t SIZE = JSON_ARRAY_SIZE(2); StaticJsonDocument doc; - JsonArray& array = doc.to(); + JsonArray array = doc.to(); char json[32]; int source[] = {1, 2, 3}; @@ -35,7 +35,7 @@ TEST_CASE("JsonArray::copyFrom()") { SECTION("TwoDimensions") { DynamicJsonDocument doc; - JsonArray& array = doc.to(); + JsonArray array = doc.to(); char json[32]; int source[][3] = {{1, 2, 3}, {4, 5, 6}}; @@ -50,7 +50,7 @@ TEST_CASE("JsonArray::copyFrom()") { const size_t SIZE = JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2); StaticJsonDocument doc; - JsonArray& array = doc.to(); + JsonArray array = doc.to(); char json[32]; int source[][3] = {{1, 2, 3}, {4, 5, 6}}; diff --git a/test/JsonArray/copyTo.cpp b/test/JsonArray/copyTo.cpp index d9092708..48826550 100644 --- a/test/JsonArray/copyTo.cpp +++ b/test/JsonArray/copyTo.cpp @@ -12,7 +12,7 @@ TEST_CASE("JsonArray::copyTo()") { char json[] = "[1,2,3]"; DeserializationError err = deserializeJson(doc, json); REQUIRE(err == DeserializationError::Ok); - JsonArray& array = doc.as(); + JsonArray array = doc.as(); int destination[4] = {0}; size_t result = array.copyTo(destination); @@ -28,7 +28,7 @@ TEST_CASE("JsonArray::copyTo()") { char json[] = "[1,2,3]"; DeserializationError err = deserializeJson(doc, json); REQUIRE(err == DeserializationError::Ok); - JsonArray& array = doc.as(); + JsonArray array = doc.as(); int destination[2] = {0}; size_t result = array.copyTo(destination); @@ -43,7 +43,7 @@ TEST_CASE("JsonArray::copyTo()") { DeserializationError err = deserializeJson(doc, json); REQUIRE(err == DeserializationError::Ok); - JsonArray& array = doc.as(); + JsonArray array = doc.as(); int destination[3][2] = {{0}}; array.copyTo(destination); diff --git a/test/JsonArray/invalid.cpp b/test/JsonArray/invalid.cpp index f057c0a7..506feefd 100644 --- a/test/JsonArray/invalid.cpp +++ b/test/JsonArray/invalid.cpp @@ -7,28 +7,29 @@ using namespace Catch::Matchers; -TEST_CASE("JsonArray::invalid()") { +TEST_CASE("Undefined JsonArray") { + JsonArray array; + SECTION("SubscriptFails") { - REQUIRE_FALSE(JsonArray::invalid()[0].success()); + REQUIRE(array[0].isNull()); } SECTION("AddFails") { - JsonArray& array = JsonArray::invalid(); array.add(1); REQUIRE(0 == array.size()); } SECTION("CreateNestedArrayFails") { - REQUIRE_FALSE(JsonArray::invalid().createNestedArray().success()); + REQUIRE(array.createNestedArray().isNull()); } SECTION("CreateNestedObjectFails") { - REQUIRE_FALSE(JsonArray::invalid().createNestedObject().success()); + REQUIRE(array.createNestedObject().isNull()); } SECTION("PrintToWritesBrackets") { char buffer[32]; - serializeJson(JsonArray::invalid(), buffer, sizeof(buffer)); - REQUIRE_THAT(buffer, Equals("[]")); + serializeJson(array, buffer, sizeof(buffer)); + REQUIRE_THAT(buffer, Equals("null")); } } diff --git a/test/JsonArray/iterator.cpp b/test/JsonArray/iterator.cpp index 91efc243..db884ee4 100644 --- a/test/JsonArray/iterator.cpp +++ b/test/JsonArray/iterator.cpp @@ -8,7 +8,7 @@ template static void run_iterator_test() { StaticJsonDocument doc; - JsonArray& array = doc.to(); + JsonArray array = doc.to(); array.add(12); array.add(34); diff --git a/test/JsonArray/remove.cpp b/test/JsonArray/remove.cpp index 27f149ac..ac502cd4 100644 --- a/test/JsonArray/remove.cpp +++ b/test/JsonArray/remove.cpp @@ -7,7 +7,7 @@ TEST_CASE("JsonArray::remove()") { DynamicJsonDocument doc; - JsonArray& _array = doc.to(); + JsonArray _array = doc.to(); _array.add(1); _array.add(2); _array.add(3); diff --git a/test/JsonArray/set.cpp b/test/JsonArray/set.cpp index 498b2668..b434bf13 100644 --- a/test/JsonArray/set.cpp +++ b/test/JsonArray/set.cpp @@ -9,7 +9,7 @@ using namespace Catch::Matchers; TEST_CASE("JsonArray::set()") { DynamicJsonDocument doc; - JsonArray& _array = doc.to(); + JsonArray _array = doc.to(); _array.add(0); SECTION("int") { @@ -42,29 +42,29 @@ TEST_CASE("JsonArray::set()") { SECTION("nested array") { DynamicJsonDocument doc2; - JsonArray& arr = doc2.to(); + JsonArray arr = doc2.to(); _array.set(0, arr); - REQUIRE(&arr == &_array[0].as()); - REQUIRE(_array[0].is()); + REQUIRE(arr == _array[0].as()); + REQUIRE(_array[0].is()); REQUIRE_FALSE(_array[0].is()); } SECTION("nested object") { DynamicJsonDocument doc2; - JsonObject& obj = doc2.to(); + JsonObject obj = doc2.to(); _array.set(0, obj); - REQUIRE(&obj == &_array[0].as()); - REQUIRE(_array[0].is()); + REQUIRE(obj == _array[0].as()); + REQUIRE(_array[0].is()); REQUIRE_FALSE(_array[0].is()); } SECTION("array subscript") { DynamicJsonDocument doc2; - JsonArray& arr = doc2.to(); + JsonArray arr = doc2.to(); arr.add("hello"); _array.set(0, arr[0]); @@ -74,7 +74,7 @@ TEST_CASE("JsonArray::set()") { SECTION("object subscript") { DynamicJsonDocument doc2; - JsonObject& obj = doc2.to(); + JsonObject obj = doc2.to(); obj["x"] = "hello"; _array.set(0, obj["x"]); diff --git a/test/JsonArray/size.cpp b/test/JsonArray/size.cpp index 48edee07..36311c9e 100644 --- a/test/JsonArray/size.cpp +++ b/test/JsonArray/size.cpp @@ -7,7 +7,7 @@ TEST_CASE("JsonArray::size()") { DynamicJsonDocument doc; - JsonArray& _array = doc.to(); + JsonArray _array = doc.to(); SECTION("increases after add()") { _array.add("hello"); diff --git a/test/JsonArray/std_string.cpp b/test/JsonArray/std_string.cpp index d6be2973..ac355436 100644 --- a/test/JsonArray/std_string.cpp +++ b/test/JsonArray/std_string.cpp @@ -12,7 +12,7 @@ static void eraseString(std::string &str) { TEST_CASE("std::string") { DynamicJsonDocument doc; - JsonArray &array = doc.to(); + JsonArray array = doc.to(); SECTION("add()") { std::string value("hello"); diff --git a/test/JsonArray/subscript.cpp b/test/JsonArray/subscript.cpp index d763e4b1..35040d10 100644 --- a/test/JsonArray/subscript.cpp +++ b/test/JsonArray/subscript.cpp @@ -8,7 +8,7 @@ TEST_CASE("JsonArray::operator[]") { DynamicJsonDocument doc; - JsonArray& _array = doc.to(); + JsonArray _array = doc.to(); _array.add(0); SECTION("int") { @@ -53,35 +53,33 @@ TEST_CASE("JsonArray::operator[]") { SECTION("nested array") { DynamicJsonDocument doc2; - JsonArray& arr = doc2.to(); + JsonArray arr = doc2.to(); _array[0] = arr; - REQUIRE(&arr == &_array[0].as()); - REQUIRE(&arr == &_array[0].as()); // <- short hand - REQUIRE(&arr == &_array[0].as()); - REQUIRE(&arr == &_array[0].as()); // <- short hand - REQUIRE(true == _array[0].is()); + REQUIRE(arr == _array[0].as()); + REQUIRE(arr == _array[0].as()); // <- short hand + // REQUIRE(arr == _array[0].as()); + // REQUIRE(arr == _array[0].as()); // <- short hand + REQUIRE(true == _array[0].is()); REQUIRE(false == _array[0].is()); } SECTION("nested object") { DynamicJsonDocument doc2; - JsonObject& obj = doc2.to(); + JsonObject obj = doc2.to(); _array[0] = obj; - REQUIRE(&obj == &_array[0].as()); - REQUIRE(&obj == &_array[0].as()); // <- short hand - REQUIRE(&obj == &_array[0].as()); - REQUIRE(&obj == &_array[0].as()); // <- short hand - REQUIRE(true == _array[0].is()); + REQUIRE(obj == _array[0].as()); + REQUIRE(obj == _array[0].as()); // <- short hand + REQUIRE(true == _array[0].is()); REQUIRE(false == _array[0].is()); } SECTION("array subscript") { DynamicJsonDocument doc2; - JsonArray& arr = doc2.to(); + JsonArray arr = doc2.to(); const char* str = "hello"; arr.add(str); @@ -94,7 +92,7 @@ TEST_CASE("JsonArray::operator[]") { SECTION("object subscript") { const char* str = "hello"; DynamicJsonDocument doc2; - JsonObject& obj = doc2.to(); + JsonObject obj = doc2.to(); obj["x"] = str; diff --git a/test/JsonDeserializer/deserializeJsonArray.cpp b/test/JsonDeserializer/deserializeJsonArray.cpp index f1f7ff28..cec7f105 100644 --- a/test/JsonDeserializer/deserializeJsonArray.cpp +++ b/test/JsonDeserializer/deserializeJsonArray.cpp @@ -10,7 +10,7 @@ TEST_CASE("deserialize JSON array") { SECTION("An empty array") { DeserializationError err = deserializeJson(doc, "[]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(0 == arr.size()); @@ -19,7 +19,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Spaces") { SECTION("Before the opening bracket") { DeserializationError err = deserializeJson(doc, " []"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(0 == arr.size()); @@ -27,7 +27,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Before first value") { DeserializationError err = deserializeJson(doc, "[ \t\r\n42]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == arr.size()); @@ -36,7 +36,7 @@ TEST_CASE("deserialize JSON array") { SECTION("After first value") { DeserializationError err = deserializeJson(doc, "[42 \t\r\n]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == arr.size()); @@ -47,7 +47,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Values types") { SECTION("On integer") { DeserializationError err = deserializeJson(doc, "[42]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == arr.size()); @@ -56,7 +56,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Two integers") { DeserializationError err = deserializeJson(doc, "[42,84]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(2 == arr.size()); @@ -66,7 +66,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Double") { DeserializationError err = deserializeJson(doc, "[4.2,1e2]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(2 == arr.size()); @@ -76,7 +76,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Unsigned long") { DeserializationError err = deserializeJson(doc, "[4294967295]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == arr.size()); @@ -85,7 +85,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Boolean") { DeserializationError err = deserializeJson(doc, "[true,false]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(2 == arr.size()); @@ -95,7 +95,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Null") { DeserializationError err = deserializeJson(doc, "[null,null]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(2 == arr.size()); @@ -108,7 +108,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Double quotes") { DeserializationError err = deserializeJson(doc, "[ \"hello\" , \"world\" ]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(2 == arr.size()); @@ -118,7 +118,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Single quotes") { DeserializationError err = deserializeJson(doc, "[ 'hello' , 'world' ]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(2 == arr.size()); @@ -128,7 +128,7 @@ TEST_CASE("deserialize JSON array") { SECTION("No quotes") { DeserializationError err = deserializeJson(doc, "[ hello , world ]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(2 == arr.size()); @@ -138,7 +138,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Double quotes (empty strings)") { DeserializationError err = deserializeJson(doc, "[\"\",\"\"]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(2 == arr.size()); @@ -148,7 +148,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Single quotes (empty strings)") { DeserializationError err = deserializeJson(doc, "[\'\',\'\']"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(2 == arr.size()); @@ -179,7 +179,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Before opening bracket") { DeserializationError err = deserializeJson(doc, "/*COMMENT*/ [\"hello\"]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == arr.size()); @@ -189,7 +189,7 @@ TEST_CASE("deserialize JSON array") { SECTION("After opening bracket") { DeserializationError err = deserializeJson(doc, "[/*COMMENT*/ \"hello\"]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == arr.size()); @@ -198,7 +198,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Before closing bracket") { DeserializationError err = deserializeJson(doc, "[\"hello\"/*COMMENT*/]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == arr.size()); @@ -207,7 +207,7 @@ TEST_CASE("deserialize JSON array") { SECTION("After closing bracket") { DeserializationError err = deserializeJson(doc, "[\"hello\"]/*COMMENT*/"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == arr.size()); @@ -217,7 +217,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Before comma") { DeserializationError err = deserializeJson(doc, "[\"hello\"/*COMMENT*/,\"world\"]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(2 == arr.size()); @@ -228,7 +228,7 @@ TEST_CASE("deserialize JSON array") { SECTION("After comma") { DeserializationError err = deserializeJson(doc, "[\"hello\",/*COMMENT*/ \"world\"]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(2 == arr.size()); @@ -256,7 +256,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Before opening bracket") { DeserializationError err = deserializeJson(doc, "//COMMENT\n\t[\"hello\"]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == arr.size()); @@ -265,7 +265,7 @@ TEST_CASE("deserialize JSON array") { SECTION("After opening bracket") { DeserializationError err = deserializeJson(doc, "[//COMMENT\n\"hello\"]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == arr.size()); @@ -275,7 +275,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Before closing bracket") { DeserializationError err = deserializeJson(doc, "[\"hello\"//COMMENT\r\n]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == arr.size()); @@ -284,7 +284,7 @@ TEST_CASE("deserialize JSON array") { SECTION("After closing bracket") { DeserializationError err = deserializeJson(doc, "[\"hello\"]//COMMENT\n"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == arr.size()); @@ -294,7 +294,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Before comma") { DeserializationError err = deserializeJson(doc, "[\"hello\"//COMMENT\n,\"world\"]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(2 == arr.size()); @@ -305,7 +305,7 @@ TEST_CASE("deserialize JSON array") { SECTION("After comma") { DeserializationError err = deserializeJson(doc, "[\"hello\",//COMMENT\n\"world\"]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(2 == arr.size()); @@ -372,17 +372,17 @@ TEST_CASE("deserialize JSON array") { " [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] "; DeserializationError err = deserializeJson(doc, jsonString); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); - JsonObject& object1 = arr[0]; - const JsonObject& object2 = arr[1]; - JsonObject& object3 = arr[2]; + JsonObject object1 = arr[0]; + const JsonObject object2 = arr[1]; + JsonObject object3 = arr[2]; REQUIRE(err == DeserializationError::Ok); - REQUIRE(true == object1.success()); - REQUIRE(true == object2.success()); - REQUIRE(false == object3.success()); + REQUIRE(object1.isNull() == false); + REQUIRE(object2.isNull() == false); + REQUIRE(object3.isNull() == true); REQUIRE(2 == object1.size()); REQUIRE(2 == object2.size()); @@ -399,7 +399,7 @@ TEST_CASE("deserialize JSON array") { SECTION("Should clear the JsonArray") { deserializeJson(doc, "[1,2,3,4]"); deserializeJson(doc, "[]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(arr.size() == 0); REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0)); diff --git a/test/JsonDeserializer/deserializeJsonArrayStatic.cpp b/test/JsonDeserializer/deserializeJsonArrayStatic.cpp index 471ad72a..c4fa6bce 100644 --- a/test/JsonDeserializer/deserializeJsonArrayStatic.cpp +++ b/test/JsonDeserializer/deserializeJsonArrayStatic.cpp @@ -68,7 +68,7 @@ TEST_CASE("deserialize JSON array with a StaticJsonDocument") { deserializeJson(doc, input); deserializeJson(doc, "[]"); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(arr.size() == 0); REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0)); } @@ -78,7 +78,7 @@ TEST_CASE("deserialize JSON array with a StaticJsonDocument") { char input[] = "[1,2]"; DeserializationError err = deserializeJson(doc, input); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); diff --git a/test/JsonDeserializer/deserializeJsonObject.cpp b/test/JsonDeserializer/deserializeJsonObject.cpp index d62ce9b0..df6da728 100644 --- a/test/JsonDeserializer/deserializeJsonObject.cpp +++ b/test/JsonDeserializer/deserializeJsonObject.cpp @@ -10,7 +10,7 @@ TEST_CASE("deserialize JSON object") { SECTION("An empty object") { DeserializationError err = deserializeJson(doc, "{}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -20,7 +20,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Quotes") { SECTION("Double quotes") { DeserializationError err = deserializeJson(doc, "{\"key\":\"value\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -30,7 +30,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Single quotes") { DeserializationError err = deserializeJson(doc, "{'key':'value'}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -40,7 +40,7 @@ TEST_CASE("deserialize JSON object") { SECTION("No quotes") { DeserializationError err = deserializeJson(doc, "{key:value}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -50,7 +50,7 @@ TEST_CASE("deserialize JSON object") { SECTION("No quotes, allow underscore in key") { DeserializationError err = deserializeJson(doc, "{_k_e_y_:42}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -62,7 +62,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Spaces") { SECTION("Before the key") { DeserializationError err = deserializeJson(doc, "{ \"key\":\"value\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -72,7 +72,7 @@ TEST_CASE("deserialize JSON object") { SECTION("After the key") { DeserializationError err = deserializeJson(doc, "{\"key\" :\"value\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -82,7 +82,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Before the value") { DeserializationError err = deserializeJson(doc, "{\"key\": \"value\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -92,7 +92,7 @@ TEST_CASE("deserialize JSON object") { SECTION("After the value") { DeserializationError err = deserializeJson(doc, "{\"key\":\"value\" }"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -103,7 +103,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Before the colon") { DeserializationError err = deserializeJson(doc, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -115,7 +115,7 @@ TEST_CASE("deserialize JSON object") { SECTION("After the colon") { DeserializationError err = deserializeJson(doc, "{\"key1\":\"value1\" ,\"key2\":\"value2\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -129,7 +129,7 @@ TEST_CASE("deserialize JSON object") { SECTION("String") { DeserializationError err = deserializeJson(doc, "{\"key1\":\"value1\",\"key2\":\"value2\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -141,7 +141,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Integer") { DeserializationError err = deserializeJson(doc, "{\"key1\":42,\"key2\":-42}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -153,7 +153,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Double") { DeserializationError err = deserializeJson(doc, "{\"key1\":12.345,\"key2\":-7E89}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -165,7 +165,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Booleans") { DeserializationError err = deserializeJson(doc, "{\"key1\":true,\"key2\":false}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -177,7 +177,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Null") { DeserializationError err = deserializeJson(doc, "{\"key1\":null,\"key2\":null}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(doc.is()); @@ -190,17 +190,17 @@ TEST_CASE("deserialize JSON object") { char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } "; DeserializationError err = deserializeJson(doc, jsonString); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); - JsonArray& array1 = obj["ab"]; - const JsonArray& array2 = obj["cd"]; - JsonArray& array3 = obj["ef"]; + JsonArray array1 = obj["ab"]; + const JsonArray array2 = obj["cd"]; + JsonArray array3 = obj["ef"]; REQUIRE(err == DeserializationError::Ok); - REQUIRE(true == array1.success()); - REQUIRE(true == array2.success()); - REQUIRE(false == array3.success()); + REQUIRE(array1.isNull() == false); + REQUIRE(array2.isNull() == false); + REQUIRE(array3.isNull() == true); REQUIRE(2 == array1.size()); REQUIRE(2 == array2.size()); @@ -278,7 +278,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Before opening brace") { DeserializationError err = deserializeJson(doc, "/*COMMENT*/ {\"hello\":\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -287,7 +287,7 @@ TEST_CASE("deserialize JSON object") { SECTION("After opening brace") { DeserializationError err = deserializeJson(doc, "{/*COMMENT*/\"hello\":\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -296,7 +296,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Before colon") { DeserializationError err = deserializeJson(doc, "{\"hello\"/*COMMENT*/:\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -305,7 +305,7 @@ TEST_CASE("deserialize JSON object") { SECTION("After colon") { DeserializationError err = deserializeJson(doc, "{\"hello\":/*COMMENT*/\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -314,7 +314,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Before closing brace") { DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"/*COMMENT*/}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -323,7 +323,7 @@ TEST_CASE("deserialize JSON object") { SECTION("After closing brace") { DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"}/*COMMENT*/"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -332,7 +332,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Before comma") { DeserializationError err = deserializeJson( doc, "{\"hello\":\"world\"/*COMMENT*/,\"answer\":42}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -342,7 +342,7 @@ TEST_CASE("deserialize JSON object") { SECTION("After comma") { DeserializationError err = deserializeJson( doc, "{\"hello\":\"world\",/*COMMENT*/\"answer\":42}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -354,7 +354,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Before opening brace") { DeserializationError err = deserializeJson(doc, "//COMMENT\n {\"hello\":\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -363,7 +363,7 @@ TEST_CASE("deserialize JSON object") { SECTION("After opening brace") { DeserializationError err = deserializeJson(doc, "{//COMMENT\n\"hello\":\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -372,7 +372,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Before colon") { DeserializationError err = deserializeJson(doc, "{\"hello\"//COMMENT\n:\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -381,7 +381,7 @@ TEST_CASE("deserialize JSON object") { SECTION("After colon") { DeserializationError err = deserializeJson(doc, "{\"hello\"://COMMENT\n\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -390,7 +390,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Before closing brace") { DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"//COMMENT\n}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -399,7 +399,7 @@ TEST_CASE("deserialize JSON object") { SECTION("After closing brace") { DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"}//COMMENT\n"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -408,7 +408,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Before comma") { DeserializationError err = deserializeJson( doc, "{\"hello\":\"world\"//COMMENT\n,\"answer\":42}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -418,7 +418,7 @@ TEST_CASE("deserialize JSON object") { SECTION("After comma") { DeserializationError err = deserializeJson( doc, "{\"hello\":\"world\",//COMMENT\n\"answer\":42}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -459,7 +459,7 @@ TEST_CASE("deserialize JSON object") { SECTION("After closing brace") { DeserializationError err = deserializeJson(doc, "{\"hello\":\"world\"}/"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(obj["hello"] == "world"); @@ -483,7 +483,7 @@ TEST_CASE("deserialize JSON object") { SECTION("Should clear the JsonObject") { deserializeJson(doc, "{\"hello\":\"world\"}"); deserializeJson(doc, "{}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(doc.is()); REQUIRE(obj.size() == 0); diff --git a/test/JsonDeserializer/std_istream.cpp b/test/JsonDeserializer/std_istream.cpp index 3e894fe0..771885ce 100644 --- a/test/JsonDeserializer/std_istream.cpp +++ b/test/JsonDeserializer/std_istream.cpp @@ -13,7 +13,7 @@ TEST_CASE("deserializeJson(std::istream&)") { std::istringstream json(" [ 42 /* comment */ ] "); DeserializationError err = deserializeJson(doc, json); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == arr.size()); @@ -24,7 +24,7 @@ TEST_CASE("deserializeJson(std::istream&)") { std::istringstream json(" { hello : world // comment\n }"); DeserializationError err = deserializeJson(doc, json); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(1 == obj.size()); diff --git a/test/JsonDeserializer/std_string.cpp b/test/JsonDeserializer/std_string.cpp index bd5e383b..f15063b3 100644 --- a/test/JsonDeserializer/std_string.cpp +++ b/test/JsonDeserializer/std_string.cpp @@ -28,7 +28,7 @@ TEST_CASE("deserializeJson(const std::string&)") { DeserializationError err = deserializeJson(doc, input); input[2] = 'X'; // alter the string tomake sure we made a copy - JsonArray &array = doc.as(); + JsonArray array = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(std::string("hello") == array[0]); } diff --git a/test/JsonObject/basics.cpp b/test/JsonObject/basics.cpp index 6a9f4e6c..19305d85 100644 --- a/test/JsonObject/basics.cpp +++ b/test/JsonObject/basics.cpp @@ -7,9 +7,9 @@ TEST_CASE("JsonObject basics") { DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); - SECTION("SuccessIsTrue") { - REQUIRE(obj.success()); + SECTION("isNull()") { + REQUIRE(obj.isNull() == false); } } diff --git a/test/JsonObject/containsKey.cpp b/test/JsonObject/containsKey.cpp index 71e22ad4..d8cba0c0 100644 --- a/test/JsonObject/containsKey.cpp +++ b/test/JsonObject/containsKey.cpp @@ -7,7 +7,7 @@ TEST_CASE("JsonObject::containsKey()") { DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); SECTION("ContainsKeyReturnsFalseForNonExistingKey") { obj.set("hello", 42); diff --git a/test/JsonObject/get.cpp b/test/JsonObject/get.cpp index ec6ed4ea..11182dff 100644 --- a/test/JsonObject/get.cpp +++ b/test/JsonObject/get.cpp @@ -9,7 +9,7 @@ using namespace Catch::Matchers; TEST_CASE("JsonObject::get()") { DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); SECTION("GetConstCharPointer_GivenStringLiteral") { obj.set("hello", "world"); diff --git a/test/JsonObject/invalid.cpp b/test/JsonObject/invalid.cpp index 7dbfd868..b3b8490e 100644 --- a/test/JsonObject/invalid.cpp +++ b/test/JsonObject/invalid.cpp @@ -8,10 +8,10 @@ using namespace Catch::Matchers; TEST_CASE("JsonObject::invalid()") { - JsonObject& obj = JsonObject::invalid(); + JsonObject obj; SECTION("SubscriptFails") { - REQUIRE_FALSE(obj["key"].success()); + REQUIRE(obj["key"].isNull()); } SECTION("AddFails") { @@ -20,16 +20,16 @@ TEST_CASE("JsonObject::invalid()") { } SECTION("CreateNestedArrayFails") { - REQUIRE_FALSE(obj.createNestedArray("hello").success()); + REQUIRE(obj.createNestedArray("hello").isNull()); } SECTION("CreateNestedObjectFails") { - REQUIRE_FALSE(obj.createNestedObject("world").success()); + REQUIRE(obj.createNestedObject("world").isNull()); } - SECTION("PrintToWritesBraces") { + SECTION("serialize to 'null'") { char buffer[32]; serializeJson(obj, buffer, sizeof(buffer)); - REQUIRE_THAT(buffer, Equals("{}")); + REQUIRE_THAT(buffer, Equals("null")); } } diff --git a/test/JsonObject/iterator.cpp b/test/JsonObject/iterator.cpp index 306d1986..0ccf1b54 100644 --- a/test/JsonObject/iterator.cpp +++ b/test/JsonObject/iterator.cpp @@ -9,7 +9,7 @@ using namespace Catch::Matchers; TEST_CASE("JsonObject::begin()/end()") { StaticJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj["ab"] = 12; obj["cd"] = 34; @@ -35,7 +35,7 @@ TEST_CASE("JsonObject::begin()/end()") { } SECTION("ConstIterator") { - const JsonObject& const_object = obj; + const JsonObject const_object = obj; JsonObject::const_iterator it = const_object.begin(); REQUIRE(const_object.end() != it); diff --git a/test/JsonObject/remove.cpp b/test/JsonObject/remove.cpp index 395e6b50..0277385f 100644 --- a/test/JsonObject/remove.cpp +++ b/test/JsonObject/remove.cpp @@ -8,7 +8,7 @@ TEST_CASE("JsonObject::remove()") { DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); SECTION("SizeDecreased_WhenValuesAreRemoved") { obj["hello"] = 1; diff --git a/test/JsonObject/set.cpp b/test/JsonObject/set.cpp index 25406c9d..7427d554 100644 --- a/test/JsonObject/set.cpp +++ b/test/JsonObject/set.cpp @@ -8,7 +8,7 @@ TEST_CASE("JsonObject::set()") { DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); SECTION("int") { obj.set("hello", 123); @@ -44,29 +44,29 @@ TEST_CASE("JsonObject::set()") { SECTION("nested array") { DynamicJsonDocument doc2; - JsonArray& arr = doc2.to(); + JsonArray arr = doc2.to(); obj.set("hello", arr); - REQUIRE(&arr == &obj["hello"].as()); - REQUIRE(obj["hello"].is()); - REQUIRE_FALSE(obj["hello"].is()); + REQUIRE(arr == obj["hello"].as()); + REQUIRE(obj["hello"].is()); + REQUIRE_FALSE(obj["hello"].is()); } SECTION("nested object") { DynamicJsonDocument doc2; - JsonObject& obj2 = doc2.to(); + JsonObject obj2 = doc2.to(); obj.set("hello", obj2); - REQUIRE(&obj2 == &obj["hello"].as()); - REQUIRE(obj["hello"].is()); - REQUIRE_FALSE(obj["hello"].is()); + REQUIRE(obj2 == obj["hello"].as()); + REQUIRE(obj["hello"].is()); + REQUIRE_FALSE(obj["hello"].is()); } SECTION("array subscript") { DynamicJsonDocument doc2; - JsonArray& arr = doc2.to(); + JsonArray arr = doc2.to(); arr.add(42); obj.set("a", arr[0]); @@ -76,7 +76,7 @@ TEST_CASE("JsonObject::set()") { SECTION("object subscript") { DynamicJsonDocument doc2; - JsonObject& obj2 = doc2.to(); + JsonObject obj2 = doc2.to(); obj2.set("x", 42); obj.set("a", obj2["x"]); @@ -86,14 +86,14 @@ TEST_CASE("JsonObject::set()") { SECTION("returns true when allocation succeeds") { StaticJsonDocument doc2; - JsonObject& obj2 = doc2.to(); + JsonObject obj2 = doc2.to(); REQUIRE(true == obj2.set(std::string("hello"), std::string("world"))); } SECTION("returns false when allocation fails") { StaticJsonDocument doc2; - JsonObject& obj2 = doc2.to(); + JsonObject obj2 = doc2.to(); REQUIRE(false == obj2.set(std::string("hello"), std::string("world"))); } diff --git a/test/JsonObject/size.cpp b/test/JsonObject/size.cpp index 5f6f0f2a..299957cc 100644 --- a/test/JsonObject/size.cpp +++ b/test/JsonObject/size.cpp @@ -8,7 +8,7 @@ TEST_CASE("JsonObject::size()") { DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); SECTION("initial size is zero") { REQUIRE(0 == obj.size()); diff --git a/test/JsonObject/std_string.cpp b/test/JsonObject/std_string.cpp index accd7ae1..de106739 100644 --- a/test/JsonObject/std_string.cpp +++ b/test/JsonObject/std_string.cpp @@ -17,7 +17,7 @@ TEST_CASE("std::string") { char json[] = "{\"key\":\"value\"}"; deserializeJson(doc, json); - JsonObject &obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(std::string("value") == obj[std::string("key")]); } @@ -26,13 +26,13 @@ TEST_CASE("std::string") { char json[] = "{\"key\":\"value\"}"; deserializeJson(doc, json); - JsonObject &obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(std::string("value") == obj[std::string("key")]); } SECTION("set(key)") { - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); std::string key("hello"); obj.set(key, "world"); eraseString(key); @@ -40,7 +40,7 @@ TEST_CASE("std::string") { } SECTION("set(value)") { - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); std::string value("world"); obj.set("hello", value); eraseString(value); @@ -48,7 +48,7 @@ TEST_CASE("std::string") { } SECTION("set(key,value)") { - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); std::string key("hello"); std::string value("world"); obj.set(key, value); @@ -58,9 +58,9 @@ TEST_CASE("std::string") { } SECTION("set(JsonArraySubscript)") { - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); DynamicJsonDocument doc2; - JsonArray &arr = doc2.to(); + JsonArray arr = doc2.to(); arr.add("world"); obj.set(std::string("hello"), arr[0]); @@ -69,9 +69,9 @@ TEST_CASE("std::string") { } SECTION("set(JsonObjectSubscript)") { - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); DynamicJsonDocument doc2; - JsonObject &obj2 = doc2.to(); + JsonObject obj2 = doc2.to(); obj2.set("x", "world"); obj.set(std::string("hello"), obj2["x"]); @@ -82,7 +82,7 @@ TEST_CASE("std::string") { SECTION("get()") { char json[] = "{\"key\":\"value\"}"; deserializeJson(doc, json); - JsonObject &obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(std::string("value") == obj.get(std::string("key"))); } @@ -90,13 +90,13 @@ TEST_CASE("std::string") { SECTION("is()") { char json[] = "{\"key\":\"value\"}"; deserializeJson(doc, json); - JsonObject &obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(true == obj.is(std::string("key"))); } SECTION("createNestedObject()") { - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); std::string key = "key"; char json[64]; obj.createNestedObject(key); @@ -106,7 +106,7 @@ TEST_CASE("std::string") { } SECTION("createNestedArray()") { - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); std::string key = "key"; char json[64]; obj.createNestedArray(key); @@ -118,12 +118,12 @@ TEST_CASE("std::string") { SECTION("containsKey()") { char json[] = "{\"key\":\"value\"}"; deserializeJson(doc, json); - JsonObject &obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(true == obj.containsKey(std::string("key"))); } SECTION("remove()") { - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); obj["key"] = "value"; obj.remove(std::string("key")); @@ -133,7 +133,7 @@ TEST_CASE("std::string") { SECTION("operator[], set key") { std::string key("hello"); - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); obj[key] = "world"; eraseString(key); REQUIRE(std::string("world") == obj["hello"]); @@ -141,7 +141,7 @@ TEST_CASE("std::string") { SECTION("operator[], set value") { std::string value("world"); - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); obj["hello"] = value; eraseString(value); REQUIRE(std::string("world") == obj["hello"]); @@ -149,7 +149,7 @@ TEST_CASE("std::string") { SECTION("memoryUsage() increases when adding a new key") { std::string key1("hello"), key2("world"); - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); obj[key1] = 1; size_t sizeBefore = doc.memoryUsage(); @@ -161,7 +161,7 @@ TEST_CASE("std::string") { SECTION("memoryUsage() remains when adding the same key") { std::string key("hello"); - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); obj[key] = 1; size_t sizeBefore = doc.memoryUsage(); diff --git a/test/JsonObject/subscript.cpp b/test/JsonObject/subscript.cpp index 98747f09..82f5a17d 100644 --- a/test/JsonObject/subscript.cpp +++ b/test/JsonObject/subscript.cpp @@ -7,7 +7,7 @@ TEST_CASE("JsonObject::operator[]") { DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); SECTION("int") { obj["hello"] = 123; @@ -53,41 +53,37 @@ TEST_CASE("JsonObject::operator[]") { SECTION("array") { DynamicJsonDocument doc2; - JsonArray& arr = doc2.to(); + JsonArray arr = doc2.to(); obj["hello"] = arr; - REQUIRE(&arr == &obj["hello"].as()); - REQUIRE(&arr == &obj["hello"].as()); // <- short hand - REQUIRE(&arr == &obj["hello"].as()); - REQUIRE(&arr == &obj["hello"].as()); // <- short hand - REQUIRE(true == obj["hello"].is()); + REQUIRE(arr == obj["hello"].as()); + REQUIRE(arr == obj["hello"].as()); // <- short hand + // REQUIRE(arr == obj["hello"].as()); + // REQUIRE(arr == obj["hello"].as()); // <- short hand + REQUIRE(true == obj["hello"].is()); REQUIRE(true == obj["hello"].is()); - REQUIRE(true == obj["hello"].is()); REQUIRE(true == obj["hello"].is()); - REQUIRE(false == obj["hello"].is()); + REQUIRE(true == obj["hello"].is()); + REQUIRE(false == obj["hello"].is()); } SECTION("object") { DynamicJsonDocument doc2; - JsonObject& obj2 = doc2.to(); + JsonObject obj2 = doc2.to(); obj["hello"] = obj2; - REQUIRE(&obj2 == &obj["hello"].as()); - REQUIRE(&obj2 == &obj["hello"].as()); // <- short hand - REQUIRE(&obj2 == &obj["hello"].as()); - REQUIRE(&obj2 == &obj["hello"].as()); // <- short hand - REQUIRE(true == obj["hello"].is()); + REQUIRE(obj2 == obj["hello"].as()); + REQUIRE(obj2 == obj["hello"].as()); REQUIRE(true == obj["hello"].is()); - REQUIRE(true == obj["hello"].is()); REQUIRE(true == obj["hello"].is()); - REQUIRE(false == obj["hello"].is()); + REQUIRE(false == obj["hello"].is()); } SECTION("array subscript") { DynamicJsonDocument doc2; - JsonArray& arr = doc2.to(); + JsonArray arr = doc2.to(); arr.add(42); obj["a"] = arr[0]; @@ -97,7 +93,7 @@ TEST_CASE("JsonObject::operator[]") { SECTION("object subscript") { DynamicJsonDocument doc2; - JsonObject& obj2 = doc2.to(); + JsonObject obj2 = doc2.to(); obj2.set("x", 42); obj["a"] = obj2["x"]; diff --git a/test/JsonSerializer/JsonArray.cpp b/test/JsonSerializer/JsonArray.cpp index 1556b495..f0a10192 100644 --- a/test/JsonSerializer/JsonArray.cpp +++ b/test/JsonSerializer/JsonArray.cpp @@ -5,7 +5,7 @@ #include #include -static void check(JsonArray &array, std::string expected) { +static void check(JsonArray array, std::string expected) { std::string actual; size_t actualLen = serializeJson(array, actual); REQUIRE(expected == actual); @@ -16,7 +16,7 @@ static void check(JsonArray &array, std::string expected) { TEST_CASE("serializeJson(JsonArray)") { StaticJsonDocument doc; - JsonArray &array = doc.to(); + JsonArray array = doc.to(); SECTION("Empty") { check(array, "[]"); diff --git a/test/JsonSerializer/JsonArrayPretty.cpp b/test/JsonSerializer/JsonArrayPretty.cpp index fc3af2e4..d0c59487 100644 --- a/test/JsonSerializer/JsonArrayPretty.cpp +++ b/test/JsonSerializer/JsonArrayPretty.cpp @@ -5,7 +5,7 @@ #include #include -static void check(JsonArray& array, std::string expected) { +static void check(JsonArray array, std::string expected) { std::string actual; size_t actualLen = serializeJsonPretty(array, actual); size_t measuredLen = measureJsonPretty(array); @@ -16,7 +16,7 @@ static void check(JsonArray& array, std::string expected) { TEST_CASE("serializeJsonPretty(JsonArray)") { DynamicJsonDocument doc; - JsonArray& array = doc.to(); + JsonArray array = doc.to(); SECTION("Empty") { check(array, "[]"); @@ -54,11 +54,11 @@ TEST_CASE("serializeJsonPretty(JsonArray)") { } SECTION("NestedArrays") { - JsonArray& nested1 = array.createNestedArray(); + JsonArray nested1 = array.createNestedArray(); nested1.add(1); nested1.add(2); - JsonObject& nested2 = array.createNestedObject(); + JsonObject nested2 = array.createNestedObject(); nested2["key"] = 3; check(array, diff --git a/test/JsonSerializer/JsonObject.cpp b/test/JsonSerializer/JsonObject.cpp index 3674b1b8..91869099 100644 --- a/test/JsonSerializer/JsonObject.cpp +++ b/test/JsonSerializer/JsonObject.cpp @@ -6,7 +6,7 @@ #include #include -void check(const JsonObject &obj, const std::string &expected) { +void check(const JsonObject obj, const std::string &expected) { char actual[256]; size_t actualLen = serializeJson(obj, actual); size_t measuredLen = measureJson(obj); @@ -18,7 +18,7 @@ void check(const JsonObject &obj, const std::string &expected) { TEST_CASE("serializeJson(JsonObject)") { DynamicJsonDocument doc; - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); SECTION("EmptyObject") { check(obj, "{}"); diff --git a/test/JsonSerializer/JsonObjectPretty.cpp b/test/JsonSerializer/JsonObjectPretty.cpp index ec3ef14b..52975014 100644 --- a/test/JsonSerializer/JsonObjectPretty.cpp +++ b/test/JsonSerializer/JsonObjectPretty.cpp @@ -6,7 +6,7 @@ #include #include -void check(const JsonObject &obj, const std::string expected) { +void check(const JsonObject obj, const std::string expected) { char json[256]; size_t actualLen = serializeJsonPretty(obj, json); @@ -19,7 +19,7 @@ void check(const JsonObject &obj, const std::string expected) { TEST_CASE("serializeJsonPretty(JsonObject)") { DynamicJsonDocument doc; - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); SECTION("EmptyObject") { check(obj, "{}"); @@ -57,10 +57,10 @@ TEST_CASE("serializeJsonPretty(JsonObject)") { } SECTION("NestedContainers") { - JsonObject &nested1 = obj.createNestedObject("key1"); + JsonObject nested1 = obj.createNestedObject("key1"); nested1["a"] = 1; - JsonArray &nested2 = obj.createNestedArray("key2"); + JsonArray nested2 = obj.createNestedArray("key2"); nested2.add(2); check(obj, diff --git a/test/JsonSerializer/JsonVariant.cpp b/test/JsonSerializer/JsonVariant.cpp index 10c5ddf3..517eb38c 100644 --- a/test/JsonSerializer/JsonVariant.cpp +++ b/test/JsonSerializer/JsonVariant.cpp @@ -14,11 +14,11 @@ void check(JsonVariant variant, const std::string &expected) { } TEST_CASE("serializeJson(JsonVariant)") { - SECTION("Empty") { - check(JsonVariant(), ""); + SECTION("Undefined") { + check(JsonVariant(), "null"); } - SECTION("Null") { + SECTION("Null string") { check(static_cast(0), "null"); } diff --git a/test/JsonSerializer/std_stream.cpp b/test/JsonSerializer/std_stream.cpp index 0d52846a..0357fba9 100644 --- a/test/JsonSerializer/std_stream.cpp +++ b/test/JsonSerializer/std_stream.cpp @@ -27,7 +27,7 @@ TEST_CASE("operator<<(std::ostream)") { } SECTION("JsonObject") { - JsonObject& object = doc.to(); + JsonObject object = doc.to(); object["key"] = "value"; os << object; @@ -36,7 +36,7 @@ TEST_CASE("operator<<(std::ostream)") { } SECTION("JsonObjectSubscript") { - JsonObject& object = doc.to(); + JsonObject object = doc.to(); object["key"] = "value"; os << object["key"]; @@ -45,7 +45,7 @@ TEST_CASE("operator<<(std::ostream)") { } SECTION("JsonArray") { - JsonArray& array = doc.to(); + JsonArray array = doc.to(); array.add("value"); os << array; @@ -54,7 +54,7 @@ TEST_CASE("operator<<(std::ostream)") { } SECTION("JsonArraySubscript") { - JsonArray& array = doc.to(); + JsonArray array = doc.to(); array.add("value"); os << array[0]; diff --git a/test/JsonSerializer/std_string.cpp b/test/JsonSerializer/std_string.cpp index 2864a693..5099017e 100644 --- a/test/JsonSerializer/std_string.cpp +++ b/test/JsonSerializer/std_string.cpp @@ -7,7 +7,7 @@ TEST_CASE("serialize JsonArray to std::string") { DynamicJsonDocument doc; - JsonArray &array = doc.to(); + JsonArray array = doc.to(); array.add(4); array.add(2); @@ -28,7 +28,7 @@ TEST_CASE("serialize JsonArray to std::string") { TEST_CASE("serialize JsonObject to std::string") { DynamicJsonDocument doc; - JsonObject &obj = doc.to(); + JsonObject obj = doc.to(); obj["key"] = "value"; SECTION("object") { diff --git a/test/JsonVariant/CMakeLists.txt b/test/JsonVariant/CMakeLists.txt index 2ceb9277..9004b3be 100644 --- a/test/JsonVariant/CMakeLists.txt +++ b/test/JsonVariant/CMakeLists.txt @@ -7,10 +7,10 @@ add_executable(JsonVariantTests compare.cpp copy.cpp is.cpp + isnull.cpp or.cpp set_get.cpp subscript.cpp - success.cpp undefined.cpp ) diff --git a/test/JsonVariant/as.cpp b/test/JsonVariant/as.cpp index 8254b101..b324161e 100644 --- a/test/JsonVariant/as.cpp +++ b/test/JsonVariant/as.cpp @@ -193,7 +193,7 @@ TEST_CASE("JsonVariant::as()") { SECTION("ObjectAsString") { DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj["key"] = "value"; @@ -203,7 +203,7 @@ TEST_CASE("JsonVariant::as()") { SECTION("ArrayAsString") { DynamicJsonDocument doc; - JsonArray& arr = doc.to(); + JsonArray arr = doc.to(); arr.add(4); arr.add(2); @@ -213,19 +213,19 @@ TEST_CASE("JsonVariant::as()") { SECTION("ArrayAsJsonArray") { DynamicJsonDocument doc; - JsonArray& arr = doc.to(); + JsonArray arr = doc.to(); JsonVariant variant = arr; - REQUIRE(&arr == &variant.as()); - REQUIRE(&arr == &variant.as()); // <- shorthand + REQUIRE(arr == variant.as()); + REQUIRE(arr == variant.as()); // <- shorthand } SECTION("ObjectAsJsonObject") { DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); JsonVariant variant = obj; - REQUIRE(&obj == &variant.as()); - REQUIRE(&obj == &variant.as()); // <- shorthand + REQUIRE(obj == variant.as()); + REQUIRE(obj == variant.as()); // <- shorthand } } diff --git a/test/JsonVariant/compare.cpp b/test/JsonVariant/compare.cpp index 4411fe02..c7681f08 100644 --- a/test/JsonVariant/compare.cpp +++ b/test/JsonVariant/compare.cpp @@ -213,9 +213,9 @@ TEST_CASE("JsonVariant comparisons") { SECTION("ArrayInVariant") { DynamicJsonDocument doc1; - JsonArray& array1 = doc1.to(); + JsonArray array1 = doc1.to(); DynamicJsonDocument doc2; - JsonArray& array2 = doc2.to(); + JsonArray array2 = doc2.to(); JsonVariant variant1 = array1; JsonVariant variant2 = array1; @@ -230,9 +230,9 @@ TEST_CASE("JsonVariant comparisons") { SECTION("ObjectInVariant") { DynamicJsonDocument doc1; - JsonObject& obj1 = doc1.to(); + JsonObject obj1 = doc1.to(); DynamicJsonDocument doc2; - JsonObject& obj2 = doc2.to(); + JsonObject obj2 = doc2.to(); JsonVariant variant1 = obj1; JsonVariant variant2 = obj1; @@ -247,10 +247,10 @@ TEST_CASE("JsonVariant comparisons") { SECTION("VariantsOfDifferentTypes") { DynamicJsonDocument doc1; - JsonObject& obj = doc1.to(); + JsonObject obj = doc1.to(); DynamicJsonDocument doc2; - JsonArray& arr = doc2.to(); + JsonArray arr = doc2.to(); JsonVariant variants[] = { true, 42, 666.667, "hello", arr, obj, }; diff --git a/test/JsonVariant/copy.cpp b/test/JsonVariant/copy.cpp index d894f607..890db3a8 100644 --- a/test/JsonVariant/copy.cpp +++ b/test/JsonVariant/copy.cpp @@ -43,7 +43,7 @@ TEST_CASE("JsonVariant copy") { SECTION("ObjectsAreCopiedByReference") { DynamicJsonDocument doc; - JsonObject& object = doc.to(); + JsonObject object = doc.to(); _variant1 = object; @@ -54,7 +54,7 @@ TEST_CASE("JsonVariant copy") { SECTION("ArraysAreCopiedByReference") { DynamicJsonDocument doc; - JsonArray& array = doc.to(); + JsonArray array = doc.to(); _variant1 = array; diff --git a/test/JsonVariant/is.cpp b/test/JsonVariant/is.cpp index d5776553..e3f26f44 100644 --- a/test/JsonVariant/is.cpp +++ b/test/JsonVariant/is.cpp @@ -7,9 +7,9 @@ void checkIsArray(JsonVariant var) { REQUIRE(var.is()); - REQUIRE(var.is()); + REQUIRE(var.is()); + REQUIRE(var.is()); REQUIRE(var.is()); - REQUIRE(var.is()); REQUIRE_FALSE(var.is()); REQUIRE_FALSE(var.is()); @@ -71,7 +71,7 @@ void checkIsString(JsonVariant var) { TEST_CASE("JsonVariant::is()") { SECTION("JsonArray") { DynamicJsonDocument doc; - JsonArray& array = doc.to(); + JsonArray array = doc.to(); checkIsArray(array); } diff --git a/test/JsonVariant/success.cpp b/test/JsonVariant/isnull.cpp similarity index 56% rename from test/JsonVariant/success.cpp rename to test/JsonVariant/isnull.cpp index ecef9ac5..e4072c1a 100644 --- a/test/JsonVariant/success.cpp +++ b/test/JsonVariant/isnull.cpp @@ -5,40 +5,40 @@ #include #include -TEST_CASE("JsonVariant::success()") { +TEST_CASE("JsonVariant::isNull()") { SECTION("ReturnsFalse_WhenUndefined") { JsonVariant variant; - REQUIRE(false == variant.success()); + REQUIRE(variant.isNull() == true); } SECTION("ReturnsTrue_WhenInteger") { JsonVariant variant = 0; - REQUIRE(true == variant.success()); + REQUIRE(variant.isNull() == false); } SECTION("ReturnsTrue_WhenEmptyArray") { DynamicJsonDocument doc; - JsonArray& array = doc.to(); + JsonArray array = doc.to(); JsonVariant variant = array; - REQUIRE(true == variant.success()); + REQUIRE(variant.isNull() == false); } SECTION("ReturnsTrue_WhenEmptyObject") { DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); JsonVariant variant = obj; - REQUIRE(true == variant.success()); + REQUIRE(variant.isNull() == false); } SECTION("ReturnsFalse_WhenInvalidArray") { - JsonVariant variant = JsonArray::invalid(); - REQUIRE(false == variant.success()); + JsonVariant variant = JsonArray(); + REQUIRE(variant.isNull() == true); } SECTION("ReturnsFalse_WhenInvalidObject") { - JsonVariant variant = JsonObject::invalid(); - REQUIRE(false == variant.success()); + JsonVariant variant = JsonObject(); + REQUIRE(variant.isNull() == true); } } diff --git a/test/JsonVariant/set_get.cpp b/test/JsonVariant/set_get.cpp index b63775ba..772df2a6 100644 --- a/test/JsonVariant/set_get.cpp +++ b/test/JsonVariant/set_get.cpp @@ -123,8 +123,8 @@ TEST_CASE("JsonVariant set()/get()") { SECTION("CanStoreObject") { DynamicJsonDocument doc; - JsonObject &object = doc.to(); + JsonObject object = doc.to(); - checkReference(object); + checkValue(object); } } diff --git a/test/JsonVariant/subscript.cpp b/test/JsonVariant/subscript.cpp index 72783292..c2192466 100644 --- a/test/JsonVariant/subscript.cpp +++ b/test/JsonVariant/subscript.cpp @@ -8,7 +8,7 @@ TEST_CASE("JsonVariant::operator[]") { SECTION("Array") { DynamicJsonDocument doc; - JsonArray& array = doc.to(); + JsonArray array = doc.to(); array.add("element at index 0"); array.add("element at index 1"); @@ -19,14 +19,14 @@ TEST_CASE("JsonVariant::operator[]") { REQUIRE(std::string("element at index 1") == var[1]); REQUIRE(std::string("element at index 0") == var[static_cast(0)]); // issue #381 - REQUIRE_FALSE(var[666].success()); - REQUIRE_FALSE(var[3].success()); - REQUIRE_FALSE(var["0"].success()); + REQUIRE(var[666].isNull()); + REQUIRE(var[3].isNull()); + REQUIRE(var["0"].isNull()); } SECTION("Object") { DynamicJsonDocument doc; - JsonObject& object = doc.to(); + JsonObject object = doc.to(); object["a"] = "element at key \"a\""; object["b"] = "element at key \"b\""; @@ -35,27 +35,27 @@ TEST_CASE("JsonVariant::operator[]") { REQUIRE(2 == var.size()); REQUIRE(std::string("element at key \"a\"") == var["a"]); REQUIRE(std::string("element at key \"b\"") == var["b"]); - REQUIRE_FALSE(var["c"].success()); - REQUIRE_FALSE(var[0].success()); + REQUIRE(var["c"].isNull()); + REQUIRE(var[0].isNull()); } SECTION("Undefined") { JsonVariant var = JsonVariant(); REQUIRE(0 == var.size()); - REQUIRE_FALSE(var["0"].success()); - REQUIRE_FALSE(var[0].success()); + REQUIRE(var["0"].isNull()); + REQUIRE(var[0].isNull()); } SECTION("String") { JsonVariant var = "hello world"; REQUIRE(0 == var.size()); - REQUIRE_FALSE(var["0"].success()); - REQUIRE_FALSE(var[0].success()); + REQUIRE(var["0"].isNull()); + REQUIRE(var[0].isNull()); } SECTION("ObjectSetValue") { DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); JsonVariant var = obj; var["hello"] = "world"; REQUIRE(1 == var.size()); @@ -64,7 +64,7 @@ TEST_CASE("JsonVariant::operator[]") { SECTION("ArraySetValue") { DynamicJsonDocument doc; - JsonArray& arr = doc.to(); + JsonArray arr = doc.to(); arr.add("hello"); JsonVariant var = arr; var[0] = "world"; diff --git a/test/JsonVariant/undefined.cpp b/test/JsonVariant/undefined.cpp index 9615f0f4..cfeadbec 100644 --- a/test/JsonVariant/undefined.cpp +++ b/test/JsonVariant/undefined.cpp @@ -29,26 +29,26 @@ TEST_CASE("JsonVariant undefined") { } SECTION("AsArrayReturnInvalid") { - REQUIRE(JsonArray::invalid() == variant.as()); + REQUIRE(JsonArray() == variant.as()); } SECTION("AsConstArrayReturnInvalid") { - REQUIRE(JsonArray::invalid() == variant.as()); + REQUIRE(JsonArray() == variant.as()); } SECTION("AsObjectReturnInvalid") { - REQUIRE(JsonObject::invalid() == variant.as()); + REQUIRE(JsonObject() == variant.as()); } SECTION("AsConstObjectReturnInvalid") { - REQUIRE(JsonObject::invalid() == variant.as()); + REQUIRE(JsonObject() == variant.as()); } SECTION("AsArrayWrapperReturnInvalid") { - REQUIRE(JsonArray::invalid() == variant.as()); + REQUIRE(JsonArray() == variant.as()); } SECTION("AsObjectWrapperReturnInvalid") { - REQUIRE(JsonObject::invalid() == variant.as()); + REQUIRE(JsonObject() == variant.as()); } } diff --git a/test/Misc/unsigned_char.cpp b/test/Misc/unsigned_char.cpp index 04b70f82..b8a9ed24 100644 --- a/test/Misc/unsigned_char.cpp +++ b/test/Misc/unsigned_char.cpp @@ -101,7 +101,7 @@ TEST_CASE("unsigned char[]") { unsigned char key[] = "hello"; DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj[key] = "world"; REQUIRE(std::string("world") == obj["hello"]); @@ -113,7 +113,7 @@ TEST_CASE("unsigned char[]") { DynamicJsonDocument doc; deserializeJson(doc, "{\"hello\":\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(std::string("world") == obj[key]); } #endif @@ -123,7 +123,7 @@ TEST_CASE("unsigned char[]") { DynamicJsonDocument doc; deserializeJson(doc, "{\"hello\":\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(std::string("world") == obj.get(key)); } @@ -131,7 +131,7 @@ TEST_CASE("unsigned char[]") { unsigned char key[] = "hello"; DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj.set(key, "world"); REQUIRE(std::string("world") == obj["hello"]); @@ -141,7 +141,7 @@ TEST_CASE("unsigned char[]") { unsigned char value[] = "world"; DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj.set("hello", value); REQUIRE(std::string("world") == obj["hello"]); @@ -151,7 +151,7 @@ TEST_CASE("unsigned char[]") { unsigned char key[] = "world"; DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj.set(key, key); REQUIRE(std::string("world") == obj["world"]); @@ -162,7 +162,7 @@ TEST_CASE("unsigned char[]") { DynamicJsonDocument doc; deserializeJson(doc, "{\"hello\":\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(true == obj.containsKey(key)); } @@ -171,7 +171,7 @@ TEST_CASE("unsigned char[]") { DynamicJsonDocument doc; deserializeJson(doc, "{\"hello\":\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); obj.remove(key); REQUIRE(0 == obj.size()); @@ -182,7 +182,7 @@ TEST_CASE("unsigned char[]") { DynamicJsonDocument doc; deserializeJson(doc, "{\"hello\":42}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(true == obj.is(key)); } @@ -191,7 +191,7 @@ TEST_CASE("unsigned char[]") { unsigned char key[] = "hello"; DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj.createNestedArray(key); } @@ -199,7 +199,7 @@ TEST_CASE("unsigned char[]") { unsigned char key[] = "hello"; DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj.createNestedObject(key); } } @@ -209,7 +209,7 @@ TEST_CASE("unsigned char[]") { unsigned char value[] = "world"; DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj["hello"] = value; REQUIRE(std::string("world") == obj["hello"]); @@ -219,7 +219,7 @@ TEST_CASE("unsigned char[]") { unsigned char value[] = "world"; DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj["hello"].set(value); REQUIRE(std::string("world") == obj["hello"]); @@ -231,7 +231,7 @@ TEST_CASE("unsigned char[]") { unsigned char value[] = "world"; DynamicJsonDocument doc; - JsonArray& arr = doc.to(); + JsonArray arr = doc.to(); arr.add(value); REQUIRE(std::string("world") == arr[0]); @@ -241,7 +241,7 @@ TEST_CASE("unsigned char[]") { unsigned char value[] = "world"; DynamicJsonDocument doc; - JsonArray& arr = doc.to(); + JsonArray arr = doc.to(); arr.add("hello"); arr.set(0, value); @@ -254,7 +254,7 @@ TEST_CASE("unsigned char[]") { unsigned char value[] = "world"; DynamicJsonDocument doc; - JsonArray& arr = doc.to(); + JsonArray arr = doc.to(); arr.add("hello"); arr[0].set(value); @@ -265,7 +265,7 @@ TEST_CASE("unsigned char[]") { unsigned char value[] = "world"; DynamicJsonDocument doc; - JsonArray& arr = doc.to(); + JsonArray arr = doc.to(); arr.add("hello"); arr[0] = value; diff --git a/test/Misc/vla.cpp b/test/Misc/vla.cpp index b7b5929a..e5ac70bb 100644 --- a/test/Misc/vla.cpp +++ b/test/Misc/vla.cpp @@ -126,7 +126,7 @@ TEST_CASE("Variable Length Array") { strcpy(vla, "hello"); DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj[vla] = "world"; REQUIRE(std::string("world") == obj["hello"]); @@ -142,7 +142,7 @@ TEST_CASE("Variable Length Array") { DynamicJsonDocument doc; deserializeJson(doc, "{\"hello\":\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(std::string("world") == obj[vla]); } #endif @@ -155,7 +155,7 @@ TEST_CASE("Variable Length Array") { DynamicJsonDocument doc; deserializeJson(doc, "{\"hello\":\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(std::string("world") == obj.get(vla)); } @@ -165,7 +165,7 @@ TEST_CASE("Variable Length Array") { strcpy(vla, "hello"); DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj.set(vla, "world"); REQUIRE(std::string("world") == obj["hello"]); @@ -177,7 +177,7 @@ TEST_CASE("Variable Length Array") { strcpy(vla, "world"); DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj.set("hello", vla); REQUIRE(std::string("world") == obj["hello"]); @@ -189,7 +189,7 @@ TEST_CASE("Variable Length Array") { strcpy(vla, "world"); DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj.set(vla, vla); REQUIRE(std::string("world") == obj["world"]); @@ -203,7 +203,7 @@ TEST_CASE("Variable Length Array") { DynamicJsonDocument doc; deserializeJson(doc, "{\"hello\":\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(true == obj.containsKey(vla)); } @@ -214,7 +214,7 @@ TEST_CASE("Variable Length Array") { DynamicJsonDocument doc; deserializeJson(doc, "{\"hello\":\"world\"}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); obj.remove(vla); REQUIRE(0 == obj.size()); @@ -227,7 +227,7 @@ TEST_CASE("Variable Length Array") { DynamicJsonDocument doc; deserializeJson(doc, "{\"hello\":42}"); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(true == obj.is(vla)); } @@ -238,7 +238,7 @@ TEST_CASE("Variable Length Array") { strcpy(vla, "hello"); DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj.createNestedArray(vla); } @@ -248,7 +248,7 @@ TEST_CASE("Variable Length Array") { strcpy(vla, "hello"); DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj.createNestedObject(vla); } } @@ -260,7 +260,7 @@ TEST_CASE("Variable Length Array") { strcpy(vla, "world"); DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj["hello"] = vla; REQUIRE(std::string("world") == obj["hello"].as()); @@ -272,7 +272,7 @@ TEST_CASE("Variable Length Array") { strcpy(vla, "world"); DynamicJsonDocument doc; - JsonObject& obj = doc.to(); + JsonObject obj = doc.to(); obj["hello"].set(vla); REQUIRE(std::string("world") == obj["hello"].as()); @@ -286,7 +286,7 @@ TEST_CASE("Variable Length Array") { strcpy(vla, "world"); DynamicJsonDocument doc; - JsonArray& arr = doc.to(); + JsonArray arr = doc.to(); arr.add(vla); REQUIRE(std::string("world") == arr[0]); @@ -298,7 +298,7 @@ TEST_CASE("Variable Length Array") { strcpy(vla, "world"); DynamicJsonDocument doc; - JsonArray& arr = doc.to(); + JsonArray arr = doc.to(); arr.add("hello"); arr.set(0, vla); @@ -313,7 +313,7 @@ TEST_CASE("Variable Length Array") { strcpy(vla, "world"); DynamicJsonDocument doc; - JsonArray& arr = doc.to(); + JsonArray arr = doc.to(); arr.add("hello"); arr[0].set(vla); @@ -326,7 +326,7 @@ TEST_CASE("Variable Length Array") { strcpy(vla, "world"); DynamicJsonDocument doc; - JsonArray& arr = doc.to(); + JsonArray arr = doc.to(); arr.add("hello"); arr[0] = vla; diff --git a/test/MsgPackDeserializer/deserializeArray.cpp b/test/MsgPackDeserializer/deserializeArray.cpp index 9ac4c068..5ddc9c87 100644 --- a/test/MsgPackDeserializer/deserializeArray.cpp +++ b/test/MsgPackDeserializer/deserializeArray.cpp @@ -13,7 +13,7 @@ TEST_CASE("deserialize MsgPack array") { const char* input = "\x90"; DeserializationError error = deserializeMsgPack(doc, input); - JsonArray& array = doc.as(); + JsonArray array = doc.as(); REQUIRE(error == DeserializationError::Ok); REQUIRE(array.size() == 0); @@ -23,7 +23,7 @@ TEST_CASE("deserialize MsgPack array") { const char* input = "\x92\x01\x02"; DeserializationError error = deserializeMsgPack(doc, input); - JsonArray& array = doc.as(); + JsonArray array = doc.as(); REQUIRE(error == DeserializationError::Ok); REQUIRE(array.size() == 2); @@ -37,7 +37,7 @@ TEST_CASE("deserialize MsgPack array") { const char* input = "\xDC\x00\x00"; DeserializationError error = deserializeMsgPack(doc, input); - JsonArray& array = doc.as(); + JsonArray array = doc.as(); REQUIRE(error == DeserializationError::Ok); REQUIRE(array.size() == 0); @@ -47,7 +47,7 @@ TEST_CASE("deserialize MsgPack array") { const char* input = "\xDC\x00\x02\xA5hello\xA5world"; DeserializationError error = deserializeMsgPack(doc, input); - JsonArray& array = doc.as(); + JsonArray array = doc.as(); REQUIRE(error == DeserializationError::Ok); REQUIRE(array.size() == 2); @@ -61,7 +61,7 @@ TEST_CASE("deserialize MsgPack array") { const char* input = "\xDD\x00\x00\x00\x00"; DeserializationError error = deserializeMsgPack(doc, input); - JsonArray& array = doc.as(); + JsonArray array = doc.as(); REQUIRE(error == DeserializationError::Ok); REQUIRE(array.size() == 0); @@ -72,7 +72,7 @@ TEST_CASE("deserialize MsgPack array") { "\xDD\x00\x00\x00\x02\xCA\x00\x00\x00\x00\xCA\x40\x48\xF5\xC3"; DeserializationError error = deserializeMsgPack(doc, input); - JsonArray& array = doc.as(); + JsonArray array = doc.as(); REQUIRE(error == DeserializationError::Ok); REQUIRE(array.size() == 2); diff --git a/test/MsgPackDeserializer/deserializeObject.cpp b/test/MsgPackDeserializer/deserializeObject.cpp index 1530beb7..a52d4d87 100644 --- a/test/MsgPackDeserializer/deserializeObject.cpp +++ b/test/MsgPackDeserializer/deserializeObject.cpp @@ -13,7 +13,7 @@ TEST_CASE("deserialize MsgPack object") { const char* input = "\x80"; DeserializationError error = deserializeMsgPack(doc, input); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(error == DeserializationError::Ok); REQUIRE(doc.is()); @@ -24,7 +24,7 @@ TEST_CASE("deserialize MsgPack object") { const char* input = "\x82\xA3one\x01\xA3two\x02"; DeserializationError error = deserializeMsgPack(doc, input); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(error == DeserializationError::Ok); REQUIRE(doc.is()); @@ -39,7 +39,7 @@ TEST_CASE("deserialize MsgPack object") { const char* input = "\xDE\x00\x00"; DeserializationError error = deserializeMsgPack(doc, input); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(error == DeserializationError::Ok); REQUIRE(doc.is()); @@ -50,7 +50,7 @@ TEST_CASE("deserialize MsgPack object") { const char* input = "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world"; DeserializationError error = deserializeMsgPack(doc, input); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(error == DeserializationError::Ok); REQUIRE(doc.is()); @@ -65,7 +65,7 @@ TEST_CASE("deserialize MsgPack object") { const char* input = "\xDF\x00\x00\x00\x00"; DeserializationError error = deserializeMsgPack(doc, input); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(error == DeserializationError::Ok); REQUIRE(doc.is()); @@ -78,7 +78,7 @@ TEST_CASE("deserialize MsgPack object") { "\xF5\xC3"; DeserializationError error = deserializeMsgPack(doc, input); - JsonObject& obj = doc.as(); + JsonObject obj = doc.as(); REQUIRE(error == DeserializationError::Ok); REQUIRE(doc.is()); diff --git a/test/MsgPackDeserializer/std_istream.cpp b/test/MsgPackDeserializer/std_istream.cpp index dc6eedba..cae588fc 100644 --- a/test/MsgPackDeserializer/std_istream.cpp +++ b/test/MsgPackDeserializer/std_istream.cpp @@ -14,7 +14,7 @@ TEST_CASE("deserializeMsgPack(std::istream&)") { DeserializationError err = deserializeMsgPack(doc, input); REQUIRE(err == DeserializationError::Ok); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(arr[0] == 0); REQUIRE(arr[1] == 2); } diff --git a/test/MsgPackDeserializer/std_string.cpp b/test/MsgPackDeserializer/std_string.cpp index 7b03e51d..f04b8183 100644 --- a/test/MsgPackDeserializer/std_string.cpp +++ b/test/MsgPackDeserializer/std_string.cpp @@ -29,7 +29,7 @@ TEST_CASE("deserializeMsgPack(const std::string&)") { DeserializationError err = deserializeMsgPack(doc, input); input[2] = 'X'; // alter the string tomake sure we made a copy - JsonArray& array = doc.as(); + JsonArray array = doc.as(); REQUIRE(err == DeserializationError::Ok); REQUIRE(std::string("hello") == array[0]); } @@ -39,7 +39,7 @@ TEST_CASE("deserializeMsgPack(const std::string&)") { deserializeMsgPack(doc, std::string("\x92\x00\x02", 3)); REQUIRE(err == DeserializationError::Ok); - JsonArray& arr = doc.as(); + JsonArray arr = doc.as(); REQUIRE(arr[0] == 0); REQUIRE(arr[1] == 2); } diff --git a/test/MsgPackSerializer/destination_types.cpp b/test/MsgPackSerializer/destination_types.cpp index 56ad77b9..369cce80 100644 --- a/test/MsgPackSerializer/destination_types.cpp +++ b/test/MsgPackSerializer/destination_types.cpp @@ -7,7 +7,7 @@ TEST_CASE("serialize MsgPack to various destination types") { DynamicJsonDocument doc; - JsonObject &object = doc.to(); + JsonObject object = doc.to(); object["hello"] = "world"; const char *expected_result = "\x81\xA5hello\xA5world"; const size_t expected_length = 13; diff --git a/test/MsgPackSerializer/measure.cpp b/test/MsgPackSerializer/measure.cpp index da7d3aae..3104e941 100644 --- a/test/MsgPackSerializer/measure.cpp +++ b/test/MsgPackSerializer/measure.cpp @@ -7,7 +7,7 @@ TEST_CASE("measureMsgPack()") { DynamicJsonDocument doc; - JsonObject &object = doc.to(); + JsonObject object = doc.to(); object["hello"] = "world"; REQUIRE(measureMsgPack(doc) == 13); diff --git a/test/MsgPackSerializer/serializeArray.cpp b/test/MsgPackSerializer/serializeArray.cpp index 5a153ca2..5a009ace 100644 --- a/test/MsgPackSerializer/serializeArray.cpp +++ b/test/MsgPackSerializer/serializeArray.cpp @@ -5,7 +5,7 @@ #include #include -static void check(const JsonArray& array, const char* expected_data, +static void check(const JsonArray array, const char* expected_data, size_t expected_len) { std::string expected(expected_data, expected_data + expected_len); std::string actual; @@ -16,19 +16,19 @@ static void check(const JsonArray& array, const char* expected_data, } template -static void check(const JsonArray& array, const char (&expected_data)[N]) { +static void check(const JsonArray array, const char (&expected_data)[N]) { const size_t expected_len = N - 1; check(array, expected_data, expected_len); } // TODO: this function is used by the commented test -// static void check(const JsonArray& array, const std::string& expected) { +// static void check(const JsonArray array, const std::string& expected) { // check(array, expected.data(), expected.length()); // } TEST_CASE("serialize MsgPack array") { DynamicJsonDocument doc; - JsonArray& array = doc.to(); + JsonArray array = doc.to(); SECTION("empty") { check(array, "\x90"); diff --git a/test/MsgPackSerializer/serializeObject.cpp b/test/MsgPackSerializer/serializeObject.cpp index 7ad9afd2..e17167c6 100644 --- a/test/MsgPackSerializer/serializeObject.cpp +++ b/test/MsgPackSerializer/serializeObject.cpp @@ -6,7 +6,7 @@ #include #include -static void check(const JsonObject& object, const char* expected_data, +static void check(const JsonObject object, const char* expected_data, size_t expected_len) { std::string expected(expected_data, expected_data + expected_len); std::string actual; @@ -17,19 +17,19 @@ static void check(const JsonObject& object, const char* expected_data, } template -static void check(const JsonObject& object, const char (&expected_data)[N]) { +static void check(const JsonObject object, const char (&expected_data)[N]) { const size_t expected_len = N - 1; check(object, expected_data, expected_len); } // TODO: used by the commented test -// static void check(const JsonObject& object, const std::string& expected) { +// static void check(const JsonObject object, const std::string& expected) { // check(object, expected.data(), expected.length()); //} TEST_CASE("serialize MsgPack object") { DynamicJsonDocument doc; - JsonObject& object = doc.to(); + JsonObject object = doc.to(); SECTION("empty") { check(object, "\x80");