From c81e8fc93aac2cb7381cc3010c1453c25317752e Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Mon, 29 Mar 2021 09:20:49 +0200 Subject: [PATCH] README: added a link to ARDUINOJSON_ENABLE_ARDUINO_STREAM --- README.md | 6 +- src/ArduinoJson/Variant/ConverterImpl.hpp | 2 +- src/ArduinoJson/Variant/VariantData.hpp | 84 ++++++++++------------- 3 files changed, 42 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 4f4d9704..a3ee6283 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,8 @@ ArduinoJson is a C++ JSON library for Arduino and IoT (Internet Of Things). * Deduplicates strings * Versatile * [Supports custom allocators (to use external RAM chip, for example)](https://arduinojson.org/v6/how-to/use-external-ram-on-esp32/?utm_source=github&utm_medium=readme) - * Supports [Arduino's `String`](https://arduinojson.org/v6/api/config/enable_arduino_string/) and [STL's `std::string`](https://arduinojson.org/v6/api/config/enable_std_string/?utm_source=github&utm_medium=readme) - * Supports Arduino's `Stream` and [STL's `std::istream`/`std::ostream`](https://arduinojson.org/v6/api/config/enable_std_stream/?utm_source=github&utm_medium=readme) + * Supports [Arduino's `String`](https://arduinojson.org/v6/api/config/enable_arduino_string/?utm_source=github&utm_medium=readme) and [STL's `std::string`](https://arduinojson.org/v6/api/config/enable_std_string/?utm_source=github&utm_medium=readme) + * Supports [Arduino's `Stream`](https://arduinojson.org/v6/api/config/enable_arduino_stream/?utm_source=github&utm_medium=readme) and [STL's `std::istream`/`std::ostream`](https://arduinojson.org/v6/api/config/enable_std_stream/?utm_source=github&utm_medium=readme) * [Supports Flash strings](https://arduinojson.org/v6/api/config/enable_progmem/?utm_source=github&utm_medium=readme) * Supports [custom readers](https://arduinojson.org/v6/api/json/deserializejson/?utm_source=github&utm_medium=readme#custom-reader) and [custom writers](https://arduinojson.org/v6/api/json/serializejson/?utm_source=github&utm_medium=readme#custom-writer) * Supports custom converters @@ -87,7 +87,7 @@ ArduinoJson is a C++ JSON library for Arduino and IoT (Internet Of Things). * [How-tos](https://arduinojson.org/v6/example/?utm_source=github&utm_medium=readme) * [FAQ](https://arduinojson.org/v6/faq/?utm_source=github&utm_medium=readme) * [Book](https://arduinojson.org/book/?utm_source=github&utm_medium=readme) - * [Changelog](changelog.md) + * [Changelog](CHANGELOG.md) * Vibrant user community * Most popular of all Arduino libraries on [GitHub](https://github.com/search?o=desc&q=arduino+library&s=stars&type=Repositories) and [PlatformIO](https://platformio.org/lib/search) * [Used in hundreds of projects](https://www.hackster.io/search?i=projects&q=arduinojson) diff --git a/src/ArduinoJson/Variant/ConverterImpl.hpp b/src/ArduinoJson/Variant/ConverterImpl.hpp index f191176c..0f4ae4c4 100644 --- a/src/ArduinoJson/Variant/ConverterImpl.hpp +++ b/src/ArduinoJson/Variant/ConverterImpl.hpp @@ -263,7 +263,7 @@ inline bool convertToJson(VariantRef variant, const ::Printable& value) { data->setNull(); return false; } - data->setOwnedString(print.c_str()); + data->setStringPointer(print.c_str(), storage_policies::store_by_copy()); return true; } diff --git a/src/ArduinoJson/Variant/VariantData.hpp b/src/ArduinoJson/Variant/VariantData.hpp index 1e6d5c40..27386691 100644 --- a/src/ArduinoJson/Variant/VariantData.hpp +++ b/src/ArduinoJson/Variant/VariantData.hpp @@ -244,64 +244,21 @@ class VariantData { setType(VALUE_IS_NULL); } - void setOwnedString(const char *s) { + void setStringPointer(const char *s, storage_policies::store_by_copy) { ARDUINOJSON_ASSERT(s != 0); setType(VALUE_IS_OWNED_STRING); _content.asString = s; } - void setLinkedString(const char *s) { + void setStringPointer(const char *s, storage_policies::store_by_address) { ARDUINOJSON_ASSERT(s != 0); setType(VALUE_IS_LINKED_STRING); _content.asString = s; } - void setStringPointer(const char *s, storage_policies::store_by_copy) { - setOwnedString(s); - } - - void setStringPointer(const char *s, storage_policies::store_by_address) { - setLinkedString(s); - } - template bool setString(TAdaptedString value, MemoryPool *pool) { - return setString(value, pool, typename TAdaptedString::storage_policy()); - } - - template - inline bool setString(TAdaptedString value, MemoryPool *pool, - storage_policies::decide_at_runtime) { - if (value.isStatic()) - return setString(value, pool, storage_policies::store_by_address()); - else - return setString(value, pool, storage_policies::store_by_copy()); - } - - template - inline bool setString(TAdaptedString value, MemoryPool *, - storage_policies::store_by_address) { - if (value.isNull()) - setNull(); - else - setStringPointer(value.data(), storage_policies::store_by_address()); - return true; - } - - template - inline bool setString(TAdaptedString value, MemoryPool *pool, - storage_policies::store_by_copy) { - if (value.isNull()) { - setNull(); - return true; - } - const char *copy = pool->saveString(value); - if (!copy) { - setNull(); - return false; - } - setStringPointer(copy, storage_policies::store_by_copy()); - return true; + return storeString(value, pool, typename TAdaptedString::storage_policy()); } CollectionData &toArray() { @@ -388,6 +345,41 @@ class VariantData { _flags &= KEY_IS_OWNED; _flags |= t; } + + template + inline bool storeString(TAdaptedString value, MemoryPool *pool, + storage_policies::decide_at_runtime) { + if (value.isStatic()) + return storeString(value, pool, storage_policies::store_by_address()); + else + return storeString(value, pool, storage_policies::store_by_copy()); + } + + template + inline bool storeString(TAdaptedString value, MemoryPool *, + storage_policies::store_by_address) { + if (value.isNull()) + setNull(); + else + setStringPointer(value.data(), storage_policies::store_by_address()); + return true; + } + + template + inline bool storeString(TAdaptedString value, MemoryPool *pool, + storage_policies::store_by_copy) { + if (value.isNull()) { + setNull(); + return true; + } + const char *copy = pool->saveString(value); + if (!copy) { + setNull(); + return false; + } + setStringPointer(copy, storage_policies::store_by_copy()); + return true; + } }; } // namespace ARDUINOJSON_NAMESPACE