forked from bblanchon/ArduinoJson
JsonHashTable is now a wrapper on to of JsonValue
This commit is contained in:
@ -3,46 +3,12 @@
|
|||||||
* Benoit Blanchon 2014 - MIT License
|
* Benoit Blanchon 2014 - MIT License
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <string.h> // for strcmp()
|
|
||||||
#include "JsonHashTable.h"
|
#include "JsonHashTable.h"
|
||||||
#include "JsonArray.h"
|
#include "JsonArray.h"
|
||||||
#include "JsonValue.h"
|
#include "JsonValue.h"
|
||||||
|
|
||||||
using namespace ArduinoJson::Parser;
|
using namespace ArduinoJson::Parser;
|
||||||
|
|
||||||
/*
|
|
||||||
* Returns the token for the value associated with the specified key
|
|
||||||
*/
|
|
||||||
JsonValue JsonHashTable::operator [](const char* desiredKey)
|
|
||||||
{
|
|
||||||
// sanity check
|
|
||||||
if (!success() || desiredKey == 0)
|
|
||||||
return JsonValue();
|
|
||||||
|
|
||||||
// skip first token, it's the whole object
|
|
||||||
jsmntok_t* currentToken = tokens + 1;
|
|
||||||
|
|
||||||
// scan each keys
|
|
||||||
for (int i = 0; i < tokens[0].size / 2 ; i++)
|
|
||||||
{
|
|
||||||
// get key token string
|
|
||||||
char* key = JsonValue(json, currentToken);
|
|
||||||
|
|
||||||
// compare with desired name
|
|
||||||
if (strcmp(desiredKey, key) == 0)
|
|
||||||
{
|
|
||||||
// return the value token that follows the key token
|
|
||||||
return JsonValue(json, currentToken + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// move forward: key + value + nested tokens
|
|
||||||
currentToken += 2 + getNestedTokenCount(currentToken + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// nothing found, return NULL
|
|
||||||
return JsonValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
DEPRECATED JsonArray JsonHashTable::getArray(const char* key)
|
DEPRECATED JsonArray JsonHashTable::getArray(const char* key)
|
||||||
{
|
{
|
||||||
return (JsonArray) (*this)[key];
|
return (JsonArray) (*this)[key];
|
||||||
|
@ -14,9 +14,8 @@ namespace ArduinoJson
|
|||||||
{
|
{
|
||||||
class JsonArray;
|
class JsonArray;
|
||||||
|
|
||||||
class JsonHashTable : public JsonObjectBase
|
class JsonHashTable
|
||||||
{
|
{
|
||||||
friend class JsonParserBase;
|
|
||||||
friend class JsonValue;
|
friend class JsonValue;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -25,50 +24,55 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
bool success()
|
bool success()
|
||||||
{
|
{
|
||||||
return JsonObjectBase::success() && tokens->type == JSMN_OBJECT;
|
return value.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue operator[](const char* key);
|
JsonValue operator[](const char* key)
|
||||||
|
{
|
||||||
|
return value[key];
|
||||||
|
}
|
||||||
|
|
||||||
bool containsKey(const char* key)
|
bool containsKey(const char* key)
|
||||||
{
|
{
|
||||||
return (*this)[key].success();
|
return value[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
DEPRECATED JsonArray getArray(const char* key);
|
DEPRECATED JsonArray getArray(const char* key);
|
||||||
|
|
||||||
DEPRECATED bool getBool(const char* key)
|
DEPRECATED bool getBool(const char* key)
|
||||||
{
|
{
|
||||||
return (bool) (*this)[key];
|
return value[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
DEPRECATED double getDouble(const char* key)
|
DEPRECATED double getDouble(const char* key)
|
||||||
{
|
{
|
||||||
return (double) (*this)[key];
|
return value[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
DEPRECATED JsonHashTable getHashTable(const char* key)
|
DEPRECATED JsonHashTable getHashTable(const char* key)
|
||||||
{
|
{
|
||||||
return (JsonHashTable) (*this)[key];
|
return value[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
DEPRECATED long getLong(const char* key)
|
DEPRECATED long getLong(const char* key)
|
||||||
{
|
{
|
||||||
return (long) (*this)[key];
|
return value[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
DEPRECATED char* getString(const char* key)
|
DEPRECATED char* getString(const char* key)
|
||||||
{
|
{
|
||||||
return (char*) (*this)[key];
|
return value[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
JsonHashTable(char* json, jsmntok_t* tokens)
|
JsonHashTable(JsonValue& value)
|
||||||
: JsonObjectBase(json, tokens)
|
: value(value)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonValue value;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -21,6 +21,8 @@ namespace ArduinoJson
|
|||||||
{
|
{
|
||||||
class JsonObjectBase
|
class JsonObjectBase
|
||||||
{
|
{
|
||||||
|
friend class JsonHashTable;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
JsonObjectBase()
|
JsonObjectBase()
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h> // for strtol, strtod
|
#include <stdlib.h> // for strtol, strtod
|
||||||
|
#include <string.h> // for strcmp()
|
||||||
#include "JsonArray.h"
|
#include "JsonArray.h"
|
||||||
#include "JsonHashTable.h"
|
#include "JsonHashTable.h"
|
||||||
#include "JsonValue.h"
|
#include "JsonValue.h"
|
||||||
@ -54,10 +55,47 @@ JsonValue::operator char*()
|
|||||||
|
|
||||||
JsonValue::operator JsonArray()
|
JsonValue::operator JsonArray()
|
||||||
{
|
{
|
||||||
return JsonArray(json, tokens);
|
return tokens->type != JSMN_ARRAY
|
||||||
|
? JsonArray(*this)
|
||||||
|
: JsonArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue::operator JsonHashTable()
|
JsonValue::operator JsonHashTable()
|
||||||
{
|
{
|
||||||
return JsonHashTable(json, tokens);
|
return tokens->type != JSMN_OBJECT
|
||||||
|
? JsonHashTable(*this)
|
||||||
|
: JsonHashTable();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns the token for the value associated with the specified key
|
||||||
|
*/
|
||||||
|
JsonValue JsonValue::operator [](const char* desiredKey)
|
||||||
|
{
|
||||||
|
// sanity check
|
||||||
|
if (!json || !desiredKey || tokens->type != JSMN_OBJECT)
|
||||||
|
return JsonValue();
|
||||||
|
|
||||||
|
// skip first token, it's the whole object
|
||||||
|
jsmntok_t* currentToken = tokens + 1;
|
||||||
|
|
||||||
|
// scan each keys
|
||||||
|
for (int i = 0; i < tokens[0].size / 2; i++)
|
||||||
|
{
|
||||||
|
// get key token string
|
||||||
|
char* key = JsonValue(json, currentToken);
|
||||||
|
|
||||||
|
// compare with desired name
|
||||||
|
if (strcmp(desiredKey, key) == 0)
|
||||||
|
{
|
||||||
|
// return the value token that follows the key token
|
||||||
|
return JsonValue(json, currentToken + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// move forward: key + value + nested tokens
|
||||||
|
currentToken += 2 + getNestedTokenCount(currentToken + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// nothing found, return NULL
|
||||||
|
return JsonValue();
|
||||||
}
|
}
|
@ -32,6 +32,8 @@ namespace ArduinoJson
|
|||||||
operator char*();
|
operator char*();
|
||||||
operator JsonArray();
|
operator JsonArray();
|
||||||
operator JsonHashTable();
|
operator JsonHashTable();
|
||||||
|
|
||||||
|
JsonValue operator[](const char* key);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user