forked from bblanchon/ArduinoJson
Added JsonObjectBase::containsKey()
This commit is contained in:
@ -4,15 +4,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "JsonObjectBase.h"
|
#include "JsonObjectBase.h"
|
||||||
#include <string.h> // for strcmp
|
|
||||||
|
|
||||||
using namespace ArduinoJson::Generator;
|
using namespace ArduinoJson::Generator;
|
||||||
using namespace ArduinoJson::Internals;
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
|
|
||||||
JsonValue JsonObjectBase::nullValue;
|
JsonValue JsonObjectBase::nullValue;
|
||||||
|
|
||||||
|
|
||||||
size_t JsonObjectBase::printTo(Print& p) const
|
size_t JsonObjectBase::printTo(Print& p) const
|
||||||
{
|
{
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
@ -47,9 +44,7 @@ JsonValue& JsonObjectBase::operator[](char const* key)
|
|||||||
|
|
||||||
for (int i = count; i > 0; --i)
|
for (int i = count; i > 0; --i)
|
||||||
{
|
{
|
||||||
bool keyMatches = strcmp(p->key, key) == 0;
|
if (p->matches(key))
|
||||||
|
|
||||||
if (keyMatches)
|
|
||||||
return p->value;
|
return p->value;
|
||||||
|
|
||||||
p++;
|
p++;
|
||||||
@ -71,3 +66,18 @@ JsonValue& JsonObjectBase::operator[](char const* key)
|
|||||||
value->reset();
|
value->reset();
|
||||||
return *value;
|
return *value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool JsonObjectBase::containsKey(char const* key) const
|
||||||
|
{
|
||||||
|
KeyValuePair* p = items;
|
||||||
|
|
||||||
|
for (int i = count; i > 0; --i)
|
||||||
|
{
|
||||||
|
if (p->matches(key))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "JsonPrintable.h"
|
#include "JsonPrintable.h"
|
||||||
#include "EscapedString.h"
|
#include "EscapedString.h"
|
||||||
|
#include <string.h> // for strcmp
|
||||||
|
|
||||||
namespace ArduinoJson
|
namespace ArduinoJson
|
||||||
{
|
{
|
||||||
@ -18,6 +19,8 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
JsonValue& operator[](const char*);
|
JsonValue& operator[](const char*);
|
||||||
|
|
||||||
|
bool containsKey(const char*) const;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void add(const char* key, T value)
|
void add(const char* key, T value)
|
||||||
{
|
{
|
||||||
@ -40,6 +43,11 @@ namespace ArduinoJson
|
|||||||
{
|
{
|
||||||
const char* key;
|
const char* key;
|
||||||
JsonValue value;
|
JsonValue value;
|
||||||
|
|
||||||
|
bool matches(const char* candidateKey) const
|
||||||
|
{
|
||||||
|
return strcmp(key, candidateKey) == 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
JsonObjectBase(KeyValuePair* items, int capacity)
|
JsonObjectBase(KeyValuePair* items, int capacity)
|
||||||
|
@ -35,12 +35,16 @@ namespace JsonGeneratorTests
|
|||||||
|
|
||||||
void mustContain(const char* key, const char* expected)
|
void mustContain(const char* key, const char* expected)
|
||||||
{
|
{
|
||||||
|
Assert::IsTrue(object.containsKey(key));
|
||||||
|
|
||||||
const char* actual = object[key];
|
const char* actual = object[key];
|
||||||
Assert::AreEqual(expected, actual);
|
Assert::AreEqual(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mustNotContain(const char* key)
|
void mustNotContain(const char* key)
|
||||||
{
|
{
|
||||||
|
Assert::IsFalse(object.containsKey(key));
|
||||||
|
|
||||||
const char* actual = object[key];
|
const char* actual = object[key];
|
||||||
Assert::IsNull(actual);
|
Assert::IsNull(actual);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user