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" #include "ArduinoJsonParser.h"
bool JsonParserBase::parseTokens(char* jsonString) bool JsonParserBase::parseAndCheckType(char* jsonString, jsmntype_t type)
{ {
buffer = jsonString; 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; return false;
// Add null termination to each token // 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; buffer[tokens[i].end] = 0;
} }
@ -26,7 +29,7 @@ char* JsonParserBase::getValueByKey(char* name)
{ {
// Scan each keys, every two other token // Scan each keys, every two other token
// (skip index 0, because it's the whole json object) // (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 // Early break if we reach the last token
if (i >= parser.toknext) break; 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;
}

View File

@ -12,33 +12,41 @@
class JsonParserBase class JsonParserBase
{ {
public:
protected: protected:
JsonParserBase(jsmntok_t* tokens, int tokenCount) JsonParserBase(jsmntok_t* tokens, int maxTokenCount)
{ {
this->tokenCount = tokenCount; this->maxTokenCount = maxTokenCount;
this->tokens = tokens; this->tokens = tokens;
jsmn_init(&parser); 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); char* getValueByKey(char* name);
private: private:
char* buffer; char* buffer;
jsmn_parser parser; jsmn_parser parser;
int tokenCount; int maxTokenCount;
jsmntok_t* tokens; jsmntok_t* tokens;
}; };
template <int N> template <int N>
class ArduinoJsonParser : JsonParserBase class JsonObjectParser : public JsonParserBase
{ {
public: public:
ArduinoJsonParser() JsonObjectParser()
: JsonParserBase(tokens, N * 2 + 1) : JsonParserBase(tokens, N * 2 + 1)
{ {
@ -46,9 +54,9 @@ public:
bool parse(char* json) bool parse(char* json)
{ {
return parseTokens(json); return parseAndCheckType(json, JSMN_OBJECT);
} }
char* getValue(char* name) char* getValue(char* name)
{ {
return getValueByKey(name); return getValueByKey(name);
@ -59,5 +67,36 @@ private:
jsmntok_t tokens[N * 2 + 1]; 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 #endif