forked from bblanchon/ArduinoJson
Added JsonArrayParser
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
@ -12,33 +12,41 @@
|
||||
|
||||
class JsonParserBase
|
||||
{
|
||||
public:
|
||||
|
||||
protected:
|
||||
|
||||
JsonParserBase(jsmntok_t* tokens, int tokenCount)
|
||||
JsonParserBase(jsmntok_t* tokens, int maxTokenCount)
|
||||
{
|
||||
this->tokenCount = tokenCount;
|
||||
this->maxTokenCount = maxTokenCount;
|
||||
this->tokens = tokens;
|
||||
|
||||
jsmn_init(&parser);
|
||||
}
|
||||
|
||||
bool parseTokens(char* jsonString);
|
||||
int getTokenCount()
|
||||
{
|
||||
return parser.toknext - 1;
|
||||
}
|
||||
|
||||
bool parseAndCheckType(char* json, jsmntype_t type);
|
||||
char* getValueByIndex(int index);
|
||||
char* getValueByKey(char* name);
|
||||
|
||||
private:
|
||||
|
||||
char* buffer;
|
||||
jsmn_parser parser;
|
||||
int tokenCount;
|
||||
int maxTokenCount;
|
||||
jsmntok_t* tokens;
|
||||
};
|
||||
|
||||
template <int N>
|
||||
class ArduinoJsonParser : JsonParserBase
|
||||
class JsonObjectParser : public JsonParserBase
|
||||
{
|
||||
public:
|
||||
|
||||
ArduinoJsonParser()
|
||||
JsonObjectParser()
|
||||
: JsonParserBase(tokens, N * 2 + 1)
|
||||
{
|
||||
|
||||
@ -46,7 +54,7 @@ public:
|
||||
|
||||
bool parse(char* json)
|
||||
{
|
||||
return parseTokens(json);
|
||||
return parseAndCheckType(json, JSMN_OBJECT);
|
||||
}
|
||||
|
||||
char* getValue(char* name)
|
||||
@ -59,5 +67,36 @@ private:
|
||||
jsmntok_t tokens[N * 2 + 1];
|
||||
};
|
||||
|
||||
template <int N>
|
||||
class JsonArrayParser : public JsonParserBase
|
||||
{
|
||||
public:
|
||||
|
||||
JsonArrayParser()
|
||||
: JsonParserBase(tokens, N + 1)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool parse(char* json)
|
||||
{
|
||||
return parseAndCheckType(json, JSMN_ARRAY);
|
||||
}
|
||||
|
||||
int getCount()
|
||||
{
|
||||
return getTokenCount();
|
||||
}
|
||||
|
||||
char* getValue(int index)
|
||||
{
|
||||
return getValueByIndex(index);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
jsmntok_t tokens[N + 1];
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user