Added class JsonHashTable

This commit is contained in:
Benoit Blanchon
2014-01-11 10:45:14 +01:00
parent e58f09db2a
commit a143fbf298
2 changed files with 85 additions and 43 deletions

View File

@ -6,7 +6,7 @@
#include "ArduinoJsonParser.h"
int JsonParserBase::getNestedTokenCounts(int tokenIndex)
int JsonObjectBase::getNestedTokenCounts(int tokenIndex)
{
int count = 0;
@ -18,48 +18,46 @@ int JsonParserBase::getNestedTokenCounts(int tokenIndex)
return count;
}
bool JsonParserBase::parseAndCheckType(char* jsonString, jsmntype_t type)
bool JsonParserBase::parse(char* jsonString)
{
buffer = jsonString;
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 = 1; i < parser.toknext; i++)
{
buffer[tokens[i].end] = 0;
// skip nested objects
i += getNestedTokenCounts(i);
}
return true;
}
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 < parser.toknext; i += 2)
char* JsonHashTable::getString(char* name)
{
// skip first token, it's the whole object
int currentToken = 1;
// Scan each keys
for (int i = 0; i < tokens[0].size / 2 ; i++)
{
// Get key token string
char* key = buffer + tokens[i].start;
char* key = json + tokens[currentToken].start;
// Compare with desired name
if (strcmp(name, key) == 0)
{
return buffer + tokens[i + 1].start;
return json + tokens[currentToken + 1].start;
}
// skip nested objects
i += getNestedTokenCounts(i);
// move forward: key + value + nested tokens
currentToken += 2 + getNestedTokenCounts(currentToken + 1);
}
}
return NULL;
}
/*
char* JsonParserBase::getValueByIndex(int index)
{
for (int i = 1; i < parser.toknext; i++)
@ -92,3 +90,4 @@ int JsonParserBase::getArraySize()
return size;
}
*/

View File

@ -10,10 +10,69 @@
#include <Arduino.h>
#include "utility/jsmn.h"
class JsonObjectBase
{
public:
JsonObjectBase()
{
json = NULL;
tokens = NULL;
}
bool success()
{
return json != NULL && tokens != NULL;
}
protected:
JsonObjectBase(char* json, jsmntok_t* tokens)
{
this->json = json;
this->tokens = tokens;
}
int getNestedTokenCounts(int tokenIndex);
char* json;
jsmntok_t* tokens;
};
class JsonHashTable : public JsonObjectBase
{
friend class JsonParserBase;
public:
JsonHashTable()
{
}
char* getString(char* key);
private:
JsonHashTable(char* json, jsmntok_t* tokens)
: JsonObjectBase(json, tokens)
{
}
};
class JsonParserBase
{
public:
JsonHashTable parseHashTable(char* json)
{
if (!parse(json) || tokens[0].type != JSMN_OBJECT)
return JsonHashTable();
return JsonHashTable(json, tokens);
}
protected:
JsonParserBase(jsmntok_t* tokens, int maxTokenCount)
@ -24,20 +83,14 @@ protected:
jsmn_init(&parser);
}
int getTokenCount()
{
return parser.toknext - 1;
}
bool parseAndCheckType(char* json, jsmntype_t type);
bool parse(char* json);
/*
char* getValueByIndex(int index);
char* getValueByKey(char* name);
int getArraySize();
*/
private:
int getNestedTokenCounts(int tokenIndex);
char* buffer;
jsmn_parser parser;
int maxTokenCount;
@ -45,31 +98,21 @@ private:
};
template <int N>
class JsonObjectParser : public JsonParserBase
class JsonParser : public JsonParserBase
{
public:
JsonObjectParser()
: JsonParserBase(tokens, N + 1)
JsonParser()
: JsonParserBase(tokens, N)
{
}
bool parse(char* json)
{
return parseAndCheckType(json, JSMN_OBJECT);
}
char* getValue(char* name)
{
return getValueByKey(name);
}
private:
jsmntok_t tokens[N + 1];
jsmntok_t tokens[N];
};
/*
template <int N>
class JsonArrayParser : public JsonParserBase
{
@ -100,6 +143,6 @@ private:
jsmntok_t tokens[N + 1];
};
*/
#endif