mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-18 04:52:22 +02:00
Moved implemntation of operator[] into JsonObject
This commit is contained in:
@ -12,25 +12,3 @@ DEPRECATED JsonObject JsonArray::getHashTable(int index)
|
||||
{
|
||||
return operator[](index);
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the token for the value at the specified index
|
||||
*/
|
||||
JsonValue JsonArray::operator[](int index)
|
||||
{
|
||||
// sanity check
|
||||
if (index < 0 || !isArray() || index >= size())
|
||||
return null();
|
||||
|
||||
// skip first token, it's the whole object
|
||||
JsonToken runningToken = firstChild();
|
||||
|
||||
// skip all tokens before the specified index
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
// move forward: current + nested tokens
|
||||
runningToken = runningToken.nextSibling();
|
||||
}
|
||||
|
||||
return runningToken;
|
||||
}
|
@ -33,13 +33,16 @@ namespace ArduinoJson
|
||||
return isArray();
|
||||
}
|
||||
|
||||
JsonValue operator[](int index)
|
||||
{
|
||||
return JsonValue::operator[](index);
|
||||
}
|
||||
|
||||
int size()
|
||||
{
|
||||
return isArray() ? childrenCount() : 0;
|
||||
}
|
||||
|
||||
JsonValue operator[](int index);
|
||||
|
||||
JsonArrayIterator begin()
|
||||
{
|
||||
return isArray() ? firstChild() : null();
|
||||
|
@ -3,7 +3,6 @@
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include <string.h> // for strcmp()
|
||||
#include "JsonArray.h"
|
||||
#include "JsonObject.h"
|
||||
#include "JsonValue.h"
|
||||
@ -14,39 +13,3 @@ DEPRECATED JsonArray JsonObject::getArray(const char* key)
|
||||
{
|
||||
return operator[](key);
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the token for the value associated with the specified key
|
||||
*/
|
||||
JsonValue JsonObject::operator[](const char* desiredKey)
|
||||
{
|
||||
// sanity check
|
||||
if (desiredKey == 0 || !isObject())
|
||||
return null();
|
||||
|
||||
// skip first token, it's the whole object
|
||||
JsonToken runningToken = firstChild();
|
||||
|
||||
// scan each keys
|
||||
for (int i = 0; i < childrenCount() / 2; i++)
|
||||
{
|
||||
// get 'key' token string
|
||||
char* key = runningToken.getText();
|
||||
|
||||
// move to the 'value' token
|
||||
runningToken = runningToken.nextSibling();
|
||||
|
||||
// compare with desired name
|
||||
if (strcmp(desiredKey, key) == 0)
|
||||
{
|
||||
// return the value token that follows the key token
|
||||
return runningToken;
|
||||
}
|
||||
|
||||
// skip nested tokens
|
||||
runningToken = runningToken.nextSibling();
|
||||
}
|
||||
|
||||
// nothing found, return NULL
|
||||
return null();
|
||||
}
|
@ -33,7 +33,10 @@ namespace ArduinoJson
|
||||
return isObject();
|
||||
}
|
||||
|
||||
JsonValue operator[](const char* key);
|
||||
JsonValue operator[](const char* key)
|
||||
{
|
||||
return JsonValue::operator[](key);
|
||||
}
|
||||
|
||||
bool containsKey(const char* key)
|
||||
{
|
||||
|
@ -4,22 +4,13 @@
|
||||
*/
|
||||
|
||||
#include <stdlib.h> // for strtol, strtod
|
||||
#include <string.h> // for strcmp()
|
||||
#include "JsonArray.h"
|
||||
#include "JsonObject.h"
|
||||
#include "JsonValue.h"
|
||||
|
||||
using namespace ArduinoJson::Parser;
|
||||
|
||||
JsonValue JsonValue::operator[](int index)
|
||||
{
|
||||
return JsonArray(*this)[index];
|
||||
}
|
||||
|
||||
JsonValue JsonValue::operator[](const char* key)
|
||||
{
|
||||
return JsonObject(*this)[key];
|
||||
}
|
||||
|
||||
JsonValue::operator bool()
|
||||
{
|
||||
if (!isPrimitive()) return 0;
|
||||
@ -53,3 +44,61 @@ JsonValue::operator char*()
|
||||
{
|
||||
return isString() || isPrimitive() ? getText() : 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the token for the value associated with the specified key
|
||||
*/
|
||||
JsonValue JsonValue::operator[](const char* desiredKey)
|
||||
{
|
||||
// sanity check
|
||||
if (desiredKey == 0 || !isObject())
|
||||
return null();
|
||||
|
||||
// skip first token, it's the whole object
|
||||
JsonToken runningToken = firstChild();
|
||||
|
||||
// scan each keys
|
||||
for (int i = 0; i < childrenCount() / 2; i++)
|
||||
{
|
||||
// get 'key' token string
|
||||
char* key = runningToken.getText();
|
||||
|
||||
// move to the 'value' token
|
||||
runningToken = runningToken.nextSibling();
|
||||
|
||||
// compare with desired name
|
||||
if (strcmp(desiredKey, key) == 0)
|
||||
{
|
||||
// return the value token that follows the key token
|
||||
return runningToken;
|
||||
}
|
||||
|
||||
// skip nested tokens
|
||||
runningToken = runningToken.nextSibling();
|
||||
}
|
||||
|
||||
// nothing found, return NULL
|
||||
return null();
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the token for the value at the specified index
|
||||
*/
|
||||
JsonValue JsonValue::operator[](int index)
|
||||
{
|
||||
// sanity check
|
||||
if (index < 0 || !isArray() || index >= childrenCount())
|
||||
return null();
|
||||
|
||||
// skip first token, it's the whole object
|
||||
JsonToken runningToken = firstChild();
|
||||
|
||||
// skip all tokens before the specified index
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
// move forward: current + nested tokens
|
||||
runningToken = runningToken.nextSibling();
|
||||
}
|
||||
|
||||
return runningToken;
|
||||
}
|
Reference in New Issue
Block a user