Added JsonArrayParser

This commit is contained in:
Benoit Blanchon
2014-01-10 20:50:29 +01:00
parent a80eca9e91
commit 9c4eb4a062
2 changed files with 62 additions and 13 deletions

View File

@ -6,15 +6,18 @@
#include "ArduinoJsonParser.h"
bool JsonParserBase::parseTokens(char* jsonString)
bool JsonParserBase::parseAndCheckType(char* jsonString, jsmntype_t type)
{
buffer = jsonString;
if (JSMN_SUCCESS != jsmn_parse(&parser, jsonString, tokens, tokenCount))
if (JSMN_SUCCESS != jsmn_parse(&parser, jsonString, tokens, maxTokenCount))
return false;
if (tokens[0].type != type)
return false;
// Add null termination to each token
for (int i = 0; i < tokenCount; i++)
for (int i = 0; i < maxTokenCount; i++)
{
buffer[tokens[i].end] = 0;
}
@ -26,7 +29,7 @@ char* JsonParserBase::getValueByKey(char* name)
{
// Scan each keys, every two other token
// (skip index 0, because it's the whole json object)
for (int i = 1; i < tokenCount; i += 2)
for (int i = 1; i < maxTokenCount; i += 2)
{
// Early break if we reach the last token
if (i >= parser.toknext) break;
@ -42,3 +45,10 @@ char* JsonParserBase::getValueByKey(char* name)
}
}
char* JsonParserBase::getValueByIndex(int index)
{
if (index < 0 || index >= parser.toknext)
return NULL;
return buffer + tokens[index + 1].start;
}