Files
ArduinoJson/JsonArray.cpp

52 lines
1.1 KiB
C++
Raw Normal View History

2014-01-11 15:05:35 +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
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)
{
// 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
jsmntok_t* currentToken = tokens + 1;
2014-01-11 15:05:35 +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
currentToken += 1 + getNestedTokenCount(currentToken);
2014-01-11 15:05:35 +01:00
}
return currentToken;
2014-01-11 15:05:35 +01:00
}
JsonArray JsonArray::getArray(int index)
{
return JsonArray(json, getToken(index));
2014-01-11 15:11:23 +01:00
}
JsonHashTable JsonArray::getHashTable(int index)
{
return JsonHashTable(json, getToken(index));
}
char* JsonArray::getString(int index)
{
return getTokenString(getToken(index));
2014-01-11 15:05:35 +01:00
}