2014-01-11 15:05:35 +01:00
|
|
|
/*
|
2014-01-11 16:41:18 +01:00
|
|
|
* malloc-free JSON parser for Arduino
|
|
|
|
* Benoit Blanchon 2014 - MIT License
|
|
|
|
*/
|
2014-01-11 15:05:35 +01:00
|
|
|
|
|
|
|
#include "JsonArray.h"
|
2014-01-11 15:11:23 +01:00
|
|
|
#include "JsonHashTable.h"
|
2014-01-11 15:05:35 +01:00
|
|
|
|
2014-01-11 16:41:18 +01:00
|
|
|
JsonArray::JsonArray(char* json, jsmntok_t* tokens)
|
|
|
|
: JsonObjectBase(json, tokens)
|
|
|
|
{
|
|
|
|
if (tokens[0].type != JSMN_ARRAY)
|
|
|
|
makeInvalid();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the token for the value at the specified index
|
|
|
|
*/
|
2014-01-11 15:05:35 +01:00
|
|
|
jsmntok_t* JsonArray::getToken(int index)
|
|
|
|
{
|
2014-01-11 16:41:18 +01:00
|
|
|
// sanity check
|
2014-01-11 15:05:35 +01:00
|
|
|
if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2014-01-11 16:41:18 +01:00
|
|
|
// skip all tokens before the specified index
|
2014-01-11 15:05:35 +01:00
|
|
|
for (int i = 0; i < index; i++)
|
|
|
|
{
|
|
|
|
// move forward: current + nested tokens
|
2014-01-11 15:23:08 +01:00
|
|
|
currentToken += 1 + getNestedTokenCount(currentToken);
|
2014-01-11 15:05:35 +01:00
|
|
|
}
|
|
|
|
|
2014-01-11 16:53:20 +01:00
|
|
|
return currentToken;
|
2014-01-11 15:05:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonArray JsonArray::getArray(int index)
|
|
|
|
{
|
2014-01-11 16:41:18 +01:00
|
|
|
return JsonArray(json, getToken(index));
|
2014-01-11 15:11:23 +01:00
|
|
|
}
|
|
|
|
|
2014-01-12 17:08:22 +01:00
|
|
|
double JsonArray::getDouble(int index)
|
|
|
|
{
|
|
|
|
return getDoubleFromToken(getToken(index));
|
|
|
|
}
|
|
|
|
|
2014-01-11 15:11:23 +01:00
|
|
|
JsonHashTable JsonArray::getHashTable(int index)
|
|
|
|
{
|
2014-01-11 16:41:18 +01:00
|
|
|
return JsonHashTable(json, getToken(index));
|
2014-01-11 15:15:52 +01:00
|
|
|
}
|
|
|
|
|
2014-01-12 12:57:44 +01:00
|
|
|
long JsonArray::getLong(int index)
|
|
|
|
{
|
|
|
|
return getLongFromToken(getToken(index));
|
|
|
|
}
|
|
|
|
|
2014-01-11 15:15:52 +01:00
|
|
|
char* JsonArray::getString(int index)
|
|
|
|
{
|
2014-01-12 12:57:44 +01:00
|
|
|
return getStringFromToken(getToken(index));
|
2014-01-11 15:05:35 +01:00
|
|
|
}
|