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-03 14:00:51 +02:00
|
|
|
#include <string.h> // for strcmp()
|
2014-01-11 15:05:35 +01:00
|
|
|
#include "JsonHashTable.h"
|
2014-07-16 13:26:11 +02:00
|
|
|
#include "JsonArray.h"
|
|
|
|
#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-01-11 16:41:18 +01:00
|
|
|
/*
|
|
|
|
* Returns the token for the value associated with the specified key
|
|
|
|
*/
|
2014-07-16 13:26:11 +02:00
|
|
|
JsonValue JsonHashTable::operator [](const char* desiredKey)
|
2014-01-11 15:05:35 +01:00
|
|
|
{
|
|
|
|
// sanity check
|
2014-07-16 13:41:00 +02:00
|
|
|
if (!success() || desiredKey == 0)
|
2014-07-16 13:26:11 +02:00
|
|
|
return JsonValue();
|
2014-01-11 15:05:35 +01:00
|
|
|
|
|
|
|
// skip first token, it's the whole object
|
2014-01-11 16:53:20 +01:00
|
|
|
jsmntok_t* currentToken = tokens + 1;
|
2014-01-11 15:05:35 +01:00
|
|
|
|
|
|
|
// scan each keys
|
|
|
|
for (int i = 0; i < tokens[0].size / 2 ; i++)
|
|
|
|
{
|
2014-01-11 16:41:18 +01:00
|
|
|
// get key token string
|
2014-07-16 13:26:11 +02:00
|
|
|
char* key = JsonValue(json, currentToken);
|
2014-01-11 15:05:35 +01:00
|
|
|
|
2014-01-11 16:53:20 +01:00
|
|
|
// compare with desired name
|
2014-01-11 16:41:18 +01:00
|
|
|
if (strcmp(desiredKey, key) == 0)
|
2014-01-11 15:05:35 +01:00
|
|
|
{
|
2014-01-11 16:53:20 +01:00
|
|
|
// return the value token that follows the key token
|
2014-07-16 13:26:11 +02:00
|
|
|
return JsonValue(json, currentToken + 1);
|
2014-01-11 15:05:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// move forward: key + value + nested tokens
|
2014-01-11 15:23:08 +01:00
|
|
|
currentToken += 2 + getNestedTokenCount(currentToken + 1);
|
2014-01-11 15:05:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// nothing found, return NULL
|
2014-07-16 13:26:11 +02:00
|
|
|
return JsonValue();
|
2014-01-12 12:57:44 +01:00
|
|
|
}
|
|
|
|
|
2014-07-16 13:53:56 +02:00
|
|
|
DEPRECATED JsonArray JsonHashTable::getArray(const char* key)
|
2014-01-11 15:15:52 +01:00
|
|
|
{
|
2014-07-16 13:26:11 +02:00
|
|
|
return (JsonArray) (*this)[key];
|
2014-01-11 15:05:35 +01:00
|
|
|
}
|