2014-07-03 13:58:08 +02:00
|
|
|
/*
|
|
|
|
* Arduino JSON library
|
|
|
|
* Benoit Blanchon 2014 - MIT License
|
2014-01-11 16:41:18 +01:00
|
|
|
*/
|
2014-01-11 15:05:35 +01:00
|
|
|
|
2014-07-18 15:43:20 +02:00
|
|
|
#include <string.h> // for strcmp()
|
2014-07-16 13:26:11 +02:00
|
|
|
#include "JsonArray.h"
|
2014-07-18 16:46:01 +02:00
|
|
|
#include "JsonObject.h"
|
2014-07-16 13:26:11 +02:00
|
|
|
#include "JsonValue.h"
|
2014-01-11 15:05:35 +01:00
|
|
|
|
2014-07-03 14:00:51 +02:00
|
|
|
using namespace ArduinoJson::Parser;
|
2014-01-11 15:05:35 +01:00
|
|
|
|
2014-07-18 16:46:01 +02:00
|
|
|
DEPRECATED JsonArray JsonObject::getArray(const char* key)
|
2014-01-11 15:15:52 +01:00
|
|
|
{
|
2014-07-19 14:41:29 +02:00
|
|
|
return operator[](key);
|
2014-07-18 15:43:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the token for the value associated with the specified key
|
|
|
|
*/
|
2014-07-19 14:41:29 +02:00
|
|
|
JsonValue JsonObject::operator[](const char* desiredKey)
|
2014-07-18 15:43:20 +02:00
|
|
|
{
|
|
|
|
// sanity check
|
2014-07-19 12:44:27 +02:00
|
|
|
if (desiredKey == 0 || !isObject())
|
2014-07-19 14:41:29 +02:00
|
|
|
return null();
|
2014-07-18 15:43:20 +02:00
|
|
|
|
|
|
|
// skip first token, it's the whole object
|
2014-07-19 12:44:27 +02:00
|
|
|
JsonToken runningToken = firstChild();
|
2014-07-18 15:43:20 +02:00
|
|
|
|
|
|
|
// scan each keys
|
2014-07-19 14:55:16 +02:00
|
|
|
for (int i = 0; i < childrenCount() / 2; i++)
|
2014-07-18 15:43:20 +02:00
|
|
|
{
|
|
|
|
// get 'key' token string
|
2014-07-19 12:44:27 +02:00
|
|
|
char* key = runningToken.getText();
|
2014-07-18 15:43:20 +02:00
|
|
|
|
|
|
|
// move to the 'value' token
|
2014-07-18 15:54:49 +02:00
|
|
|
runningToken = runningToken.nextSibling();
|
2014-07-18 15:43:20 +02:00
|
|
|
|
|
|
|
// compare with desired name
|
|
|
|
if (strcmp(desiredKey, key) == 0)
|
|
|
|
{
|
|
|
|
// return the value token that follows the key token
|
2014-07-19 12:44:27 +02:00
|
|
|
return runningToken;
|
2014-07-18 15:43:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// skip nested tokens
|
2014-07-18 15:54:49 +02:00
|
|
|
runningToken = runningToken.nextSibling();
|
2014-07-18 15:43:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// nothing found, return NULL
|
2014-07-19 12:44:27 +02:00
|
|
|
return null();
|
2014-01-11 15:05:35 +01:00
|
|
|
}
|