Simplified string handling in JsonObject

This commit is contained in:
Benoit Blanchon
2018-10-10 09:18:36 +02:00
parent b11ad4077b
commit d1003ff6c9
2 changed files with 13 additions and 15 deletions

View File

@ -43,14 +43,14 @@ class JsonObject {
// TKey = const std::string&, const String& // TKey = const std::string&, const String&
template <typename TString> template <typename TString>
FORCE_INLINE bool containsKey(const TString& key) const { FORCE_INLINE bool containsKey(const TString& key) const {
return containsKey_impl<const TString&>(key); return containsKey_impl(makeString(key));
} }
// //
// bool containsKey(TKey); // bool containsKey(TKey);
// TKey = char*, const char*, char[], const char[], const FlashStringHelper* // TKey = char*, const char*, char[], const char[], const FlashStringHelper*
template <typename TString> template <typename TString>
FORCE_INLINE bool containsKey(TString* key) const { FORCE_INLINE bool containsKey(TString* key) const {
return containsKey_impl<TString*>(key); return containsKey_impl(makeString(key));
} }
bool copyFrom(JsonObject src) { bool copyFrom(JsonObject src) {
@ -87,14 +87,14 @@ class JsonObject {
template <typename TString> template <typename TString>
FORCE_INLINE JsonObject createNestedObject(const TString& key) { FORCE_INLINE JsonObject createNestedObject(const TString& key) {
if (!_data) return JsonObject(); if (!_data) return JsonObject();
return createNestedObject_impl<const TString&>(key); return createNestedObject_impl(makeString(key));
} }
// //
// JsonObject createNestedObject(TKey); // JsonObject createNestedObject(TKey);
// TKey = char*, const char*, char[], const char[], const FlashStringHelper* // TKey = char*, const char*, char[], const char[], const FlashStringHelper*
template <typename TString> template <typename TString>
FORCE_INLINE JsonObject createNestedObject(TString* key) { FORCE_INLINE JsonObject createNestedObject(TString* key) {
return createNestedObject_impl<TString*>(key); return createNestedObject_impl(makeString(key));
} }
// Gets the value associated with the specified key. // Gets the value associated with the specified key.
@ -127,7 +127,7 @@ class JsonObject {
// std::string, String, JsonArray, JsonObject // std::string, String, JsonArray, JsonObject
template <typename TValue, typename TString> template <typename TValue, typename TString>
FORCE_INLINE bool is(const TString& key) const { FORCE_INLINE bool is(const TString& key) const {
return is_impl<const TString&, TValue>(key); return is_impl<TValue>(makeString(key));
} }
// //
// bool is<TValue>(TKey) const; // bool is<TValue>(TKey) const;
@ -136,7 +136,7 @@ class JsonObject {
// std::string, String, JsonArray, JsonObject // std::string, String, JsonArray, JsonObject
template <typename TValue, typename TString> template <typename TValue, typename TString>
FORCE_INLINE bool is(TString* key) const { FORCE_INLINE bool is(TString* key) const {
return is_impl<TString*, TValue>(key); return is_impl<TValue>(makeString(key));
} }
// Gets or sets the value associated with the specified key. // Gets or sets the value associated with the specified key.
@ -288,7 +288,7 @@ class JsonObject {
private: private:
template <typename TStringRef> template <typename TStringRef>
FORCE_INLINE bool containsKey_impl(TStringRef key) const { FORCE_INLINE bool containsKey_impl(TStringRef key) const {
return findSlot(makeString(key)) != 0; return findSlot(key) != 0;
} }
template <typename TStringRef> template <typename TStringRef>
@ -321,9 +321,9 @@ class JsonObject {
: TValue(); : TValue();
} }
template <typename TStringRef, typename TValue> template <typename TValue, typename TStringRef>
FORCE_INLINE bool is_impl(TStringRef key) const { FORCE_INLINE bool is_impl(TStringRef key) const {
Slot* slot = findSlot(makeString(key)); Slot* slot = findSlot(key);
return slot ? JsonVariant(_memoryPool, &slot->value).is<TValue>() : false; return slot ? JsonVariant(_memoryPool, &slot->value).is<TValue>() : false;
} }

View File

@ -11,23 +11,21 @@ namespace ARDUINOJSON_NAMESPACE {
template <typename TString> template <typename TString>
inline JsonArray JsonObject::createNestedArray(const TString& key) { inline JsonArray JsonObject::createNestedArray(const TString& key) {
return createNestedArray_impl<const TString&>(key); return createNestedArray_impl(makeString(key));
} }
template <typename TString> template <typename TString>
inline JsonArray JsonObject::createNestedArray(TString* key) { inline JsonArray JsonObject::createNestedArray(TString* key) {
return createNestedArray_impl<TString*>(key); return createNestedArray_impl(makeString(key));
} }
template <typename TStringRef> template <typename TStringRef>
inline JsonArray JsonObject::createNestedArray_impl(TStringRef key) { inline JsonArray JsonObject::createNestedArray_impl(TStringRef key) {
if (!_data) return JsonArray(); return set_impl(key).template to<JsonArray>();
return set(key).template to<JsonArray>();
} }
template <typename TStringRef> template <typename TStringRef>
inline JsonObject JsonObject::createNestedObject_impl(TStringRef key) { inline JsonObject JsonObject::createNestedObject_impl(TStringRef key) {
if (!_data) return JsonObject(); return set_impl(key).template to<JsonObject>();
return set(key).template to<JsonObject>();
} }
} // namespace ARDUINOJSON_NAMESPACE } // namespace ARDUINOJSON_NAMESPACE