From 4f6253aa9fec99260b8bb7b9b2e9003f5259b600 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 17 Sep 2018 10:02:07 +0200 Subject: Import arduinojson and ubjson. Only partially working at the moment --- include/lib/ArduinoJson/JsonObject.hpp | 328 +++++++++++++++++++++++++++++++++ 1 file changed, 328 insertions(+) create mode 100644 include/lib/ArduinoJson/JsonObject.hpp (limited to 'include/lib/ArduinoJson/JsonObject.hpp') diff --git a/include/lib/ArduinoJson/JsonObject.hpp b/include/lib/ArduinoJson/JsonObject.hpp new file mode 100644 index 0000000..caf698a --- /dev/null +++ b/include/lib/ArduinoJson/JsonObject.hpp @@ -0,0 +1,328 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "Data/JsonBufferAllocated.hpp" +#include "Data/List.hpp" +#include "Data/ReferenceType.hpp" +#include "Data/ValueSaver.hpp" +#include "JsonPair.hpp" +#include "Serialization/JsonPrintable.hpp" +#include "StringTraits/StringTraits.hpp" +#include "TypeTraits/EnableIf.hpp" +#include "TypeTraits/IsArray.hpp" +#include "TypeTraits/IsFloatingPoint.hpp" +#include "TypeTraits/IsSame.hpp" + +// Returns the size (in bytes) of an object with n elements. +// Can be very handy to determine the size of a StaticJsonBuffer. +#define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \ + (sizeof(JsonObject) + (NUMBER_OF_ELEMENTS) * sizeof(JsonObject::node_type)) + +namespace ArduinoJson { + +// Forward declarations +class JsonArray; +class JsonBuffer; +namespace Internals { +template +class JsonObjectSubscript; +} + +// A dictionary of JsonVariant indexed by string (char*) +// +// The constructor is private, instances must be created via +// JsonBuffer::createObject() or JsonBuffer::parseObject(). +// A JsonObject can be serialized to a JSON string via JsonObject::printTo(). +// It can also be deserialized from a JSON string via JsonBuffer::parseObject(). +class JsonObject : public Internals::JsonPrintable, + 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. + // Instead, use JsonBuffer::createObject() or JsonBuffer.parseObject(). + explicit JsonObject(JsonBuffer* buffer) throw() + : Internals::List(buffer) {} + + // 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); + } + + // 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); + } + + // 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); + } + // + // 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); + } + // + // bool set(TKey, TValue, uint8_t decimals); + // TKey = const std::string&, const String& + // TValue = float, double + template + DEPRECATED("Second argument is not supported anymore") + typename Internals::EnableIf::value, + bool>::type + set(const TString& key, TValue value, uint8_t) { + return set_impl(key, + JsonVariant(value)); + } + // + // bool set(TKey, TValue, uint8_t decimals); + // TKey = char*, const char*, const FlashStringHelper* + // TValue = float, double + template + DEPRECATED("Second argument is not supported anymore") + typename Internals::EnableIf::value, + bool>::type + set(TString* key, TValue value, uint8_t) { + return set_impl(key, JsonVariant(value)); + } + + // Gets the value associated with the specified key. + // + // TValue get(TKey) const; + // TKey = const std::string&, const String& + // TValue = bool, char, long, int, short, float, double, + // std::string, String, JsonArray, JsonObject + template + typename Internals::JsonVariantAs::type get( + const TString& key) const { + return get_impl(key); + } + // + // TValue get(TKey) const; + // TKey = char*, const char*, const FlashStringHelper* + // TValue = bool, char, long, int, short, float, double, + // std::string, String, JsonArray, JsonObject + template + typename Internals::JsonVariantAs::type get(TString* key) const { + return get_impl(key); + } + + // Checks the type of the value associated with the specified key. + // + // + // bool is(TKey) const; + // TKey = const std::string&, const String& + // TValue = bool, char, long, int, short, float, double, + // std::string, String, JsonArray, JsonObject + template + bool is(const TString& key) const { + return is_impl(key); + } + // + // bool is(TKey) const; + // TKey = char*, const char*, const FlashStringHelper* + // TValue = bool, char, long, int, short, float, double, + // std::string, String, JsonArray, JsonObject + template + bool is(TString* key) const { + return is_impl(key); + } + + // Creates and adds a JsonArray. + // + // JsonArray& createNestedArray(TKey); + // TKey = const std::string&, const String& + template + JsonArray& createNestedArray(const TString& key) { + return createNestedArray_impl(key); + } + // JsonArray& createNestedArray(TKey); + // TKey = char*, const char*, char[], const char[], const FlashStringHelper* + template + JsonArray& createNestedArray(TString* key) { + return createNestedArray_impl(key); + } + + // Creates and adds a JsonObject. + // + // JsonObject& createNestedObject(TKey); + // TKey = const std::string&, const String& + template + JsonObject& createNestedObject(const TString& key) { + return createNestedObject_impl(key); + } + // + // JsonObject& createNestedObject(TKey); + // TKey = char*, const char*, char[], const char[], const FlashStringHelper* + template + JsonObject& createNestedObject(TString* key) { + return createNestedObject_impl(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 containsKey(TKey); + // TKey = char*, const char*, char[], const char[], const FlashStringHelper* + template + bool containsKey(TString* key) const { + return findKey(key) != end(); + } + + // Removes the specified key and the associated value. + // + // void remove(TKey); + // TKey = const std::string&, const String& + template + void remove(const TString& key) { + remove(findKey(key)); + } + // + // void remove(TKey); + // TKey = char*, const char*, char[], const char[], const FlashStringHelper* + template + void remove(TString* key) { + remove(findKey(key)); + } + // + // void remove(iterator) + using Internals::List::remove; + + // 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; + } + + private: + // Returns the list node that matches the specified key. + template + iterator findKey(TStringRef key) { + iterator it; + for (it = begin(); it != end(); ++it) { + if (Internals::StringTraits::equals(key, it->key)) break; + } + return it; + } + template + const_iterator findKey(TStringRef key) const { + return const_cast(this)->findKey(key); + } + + template + typename Internals::JsonVariantAs::type get_impl( + TStringRef key) const { + const_iterator it = findKey(key); + return it != end() ? it->value.as() + : 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); + return it != end() ? it->value.is() : false; + } + + template + JsonArray& createNestedArray_impl(TStringRef key); + + template + JsonObject& createNestedObject_impl(TStringRef key); +}; + +namespace Internals { +template <> +struct JsonVariantDefault { + static JsonObject& get() { + return JsonObject::invalid(); + } +}; +} // namespace Internals +} // namespace ArduinoJson -- cgit v1.2.3