Added verification of the type of token

This commit is contained in:
Benoit Blanchon
2014-01-11 16:41:18 +01:00
parent da37162a94
commit 4713e90f12
7 changed files with 74 additions and 74 deletions

View File

@ -1,18 +1,27 @@
/*
* malloc-free JSON parser for Arduino
* Benoit Blanchon 2014
* MIT License
*/
* malloc-free JSON parser for Arduino
* Benoit Blanchon 2014 - MIT License
*/
#include "JsonArray.h"
#include "JsonHashTable.h"
#include <string.h> // for strcmp()
jsmntok_t* JsonHashTable::getToken(char* name)
JsonHashTable::JsonHashTable(char* json, jsmntok_t* tokens)
: JsonObjectBase(json, tokens)
{
if (tokens[0].type != JSMN_OBJECT)
makeInvalid();
}
/*
* Returns the token for the value associated with the specified key
*/
jsmntok_t* JsonHashTable::getToken(char* desiredKey)
{
// sanity check
if (json == 0 || tokens == 0 || name == 0)
if (json == 0 || tokens == 0 || desiredKey == 0)
return 0;
// skip first token, it's the whole object
@ -21,11 +30,11 @@ jsmntok_t* JsonHashTable::getToken(char* name)
// scan each keys
for (int i = 0; i < tokens[0].size / 2 ; i++)
{
// Get key token string
// get key token string
char* key = json + tokens[currentToken].start;
// Compare with desired name
if (strcmp(name, key) == 0)
if (strcmp(desiredKey, key) == 0)
{
return &tokens[currentToken + 1];
}
@ -40,14 +49,12 @@ jsmntok_t* JsonHashTable::getToken(char* name)
JsonArray JsonHashTable::getArray(char* key)
{
jsmntok_t* token = getToken(key);
return JsonArray(json, token);
return JsonArray(json, getToken(key));
}
JsonHashTable JsonHashTable::getHashTable(char* key)
{
jsmntok_t* token = getToken(key);
return JsonHashTable(json, token);
return JsonHashTable(json, getToken(key));
}
char* JsonHashTable::getString(char* key)