forked from bblanchon/ArduinoJson
Added a verification of the token type before converting to string
This commit is contained in:
@ -24,7 +24,7 @@ jsmntok_t* JsonArray::getToken(int index)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
// skip first token, it's the whole object
|
// skip first token, it's the whole object
|
||||||
int currentToken = 1;
|
jsmntok_t* currentToken = tokens + 1;
|
||||||
|
|
||||||
// skip all tokens before the specified index
|
// skip all tokens before the specified index
|
||||||
for (int i = 0; i < index; i++)
|
for (int i = 0; i < index; i++)
|
||||||
@ -33,7 +33,7 @@ jsmntok_t* JsonArray::getToken(int index)
|
|||||||
currentToken += 1 + getNestedTokenCount(currentToken);
|
currentToken += 1 + getNestedTokenCount(currentToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
return &tokens[currentToken];
|
return currentToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArray JsonArray::getArray(int index)
|
JsonArray JsonArray::getArray(int index)
|
||||||
@ -48,6 +48,5 @@ JsonHashTable JsonArray::getHashTable(int index)
|
|||||||
|
|
||||||
char* JsonArray::getString(int index)
|
char* JsonArray::getString(int index)
|
||||||
{
|
{
|
||||||
jsmntok_t* token = getToken(index);
|
return getTokenString(getToken(index));
|
||||||
return token != 0 ? json + token->start : 0;
|
|
||||||
}
|
}
|
@ -25,18 +25,19 @@ jsmntok_t* JsonHashTable::getToken(char* desiredKey)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
// skip first token, it's the whole object
|
// skip first token, it's the whole object
|
||||||
int currentToken = 1;
|
jsmntok_t* currentToken = tokens + 1;
|
||||||
|
|
||||||
// scan each keys
|
// scan each keys
|
||||||
for (int i = 0; i < tokens[0].size / 2 ; i++)
|
for (int i = 0; i < tokens[0].size / 2 ; i++)
|
||||||
{
|
{
|
||||||
// get key token string
|
// get key token string
|
||||||
char* key = json + tokens[currentToken].start;
|
char* key = getTokenString(currentToken);
|
||||||
|
|
||||||
// Compare with desired name
|
// compare with desired name
|
||||||
if (strcmp(desiredKey, key) == 0)
|
if (strcmp(desiredKey, key) == 0)
|
||||||
{
|
{
|
||||||
return &tokens[currentToken + 1];
|
// return the value token that follows the key token
|
||||||
|
return currentToken + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// move forward: key + value + nested tokens
|
// move forward: key + value + nested tokens
|
||||||
@ -59,6 +60,5 @@ JsonHashTable JsonHashTable::getHashTable(char* key)
|
|||||||
|
|
||||||
char* JsonHashTable::getString(char* key)
|
char* JsonHashTable::getString(char* key)
|
||||||
{
|
{
|
||||||
jsmntok_t* token = getToken(key);
|
return getTokenString(getToken(key));
|
||||||
return token != 0 ? json + token->start : 0;
|
|
||||||
}
|
}
|
@ -6,14 +6,22 @@
|
|||||||
|
|
||||||
#include "JsonObjectBase.h"
|
#include "JsonObjectBase.h"
|
||||||
|
|
||||||
int JsonObjectBase::getNestedTokenCount(int tokenIndex)
|
int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
for (int i = 0; i < tokens[tokenIndex].size; i++)
|
for (int i = 0; i < token->size; i++)
|
||||||
{
|
{
|
||||||
count += 1 + getNestedTokenCount(tokenIndex + 1 + i);
|
count += 1 + getNestedTokenCount(token + 1 + i);
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* JsonObjectBase::getTokenString(jsmntok_t* token)
|
||||||
|
{
|
||||||
|
if (token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return json + token->start;
|
||||||
}
|
}
|
@ -36,8 +36,9 @@ protected:
|
|||||||
json = 0;
|
json = 0;
|
||||||
tokens = 0;
|
tokens = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getNestedTokenCount(int tokenIndex);
|
static int getNestedTokenCount(jsmntok_t* token);
|
||||||
|
char* getTokenString(jsmntok_t* token);
|
||||||
|
|
||||||
char* json;
|
char* json;
|
||||||
jsmntok_t* tokens;
|
jsmntok_t* tokens;
|
||||||
|
Reference in New Issue
Block a user