2014-01-11 15:05:35 +01:00
|
|
|
/*
|
|
|
|
* malloc-free JSON parser for Arduino
|
|
|
|
* Benoit Blanchon 2014
|
|
|
|
* MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "JsonArray.h"
|
2014-01-11 15:11:23 +01:00
|
|
|
#include "JsonHashTable.h"
|
2014-01-11 15:05:35 +01:00
|
|
|
|
|
|
|
jsmntok_t* JsonArray::getToken(int index)
|
|
|
|
{
|
|
|
|
if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// skip first token, it's the whole object
|
|
|
|
int currentToken = 1;
|
|
|
|
|
|
|
|
for (int i = 0; i < index; i++)
|
|
|
|
{
|
|
|
|
// move forward: current + nested tokens
|
|
|
|
currentToken += 1 + getNestedTokenCounts(currentToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tokens[currentToken];
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonArray JsonArray::getArray(int index)
|
|
|
|
{
|
|
|
|
jsmntok_t* token = getToken(index);
|
|
|
|
return JsonArray(json, token);
|
2014-01-11 15:11:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonHashTable JsonArray::getHashTable(int index)
|
|
|
|
{
|
|
|
|
jsmntok_t* token = getToken(index);
|
|
|
|
return JsonHashTable(json, token);
|
2014-01-11 15:15:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char* JsonArray::getString(int index)
|
|
|
|
{
|
|
|
|
jsmntok_t* token = getToken(index);
|
|
|
|
return token != 0 ? json + token->start : 0;
|
2014-01-11 15:05:35 +01:00
|
|
|
}
|