Added JsonArray

This commit is contained in:
Benoit Blanchon
2014-01-11 10:57:55 +01:00
parent a143fbf298
commit 410b836667
2 changed files with 55 additions and 30 deletions

View File

@ -57,37 +57,22 @@ char* JsonHashTable::getString(char* name)
return NULL; return NULL;
} }
/*
char* JsonParserBase::getValueByIndex(int index) char* JsonArray::getString(int index)
{ {
for (int i = 1; i < parser.toknext; i++) if (json == NULL) return NULL;
if (tokens == NULL) return NULL;
if (index < 0) return NULL;
if (index >= tokens[0].size) return NULL;
// skip first token, it's the whole object
int currentToken = 1;
for (int i = 0; i < index; i++)
{ {
if (index == 0) // move forward: current + nested tokens
{ currentToken += 1 + getNestedTokenCounts(currentToken);
return buffer + tokens[i].start;
}
// skip nested objects
i += getNestedTokenCounts(i);
index--;
} }
return NULL; return json + tokens[currentToken].start;
} }
int JsonParserBase::getArraySize()
{
int size = 0;
for (int i = 1; i < parser.toknext; i++)
{
// skip nested objects
i += getNestedTokenCounts(i);
size++;
}
return size;
}
*/

View File

@ -10,6 +10,8 @@
#include <Arduino.h> #include <Arduino.h>
#include "utility/jsmn.h" #include "utility/jsmn.h"
class JsonArray;
class JsonObjectBase class JsonObjectBase
{ {
public: public:
@ -51,6 +53,7 @@ public:
} }
char* getString(char* key); char* getString(char* key);
JsonArray getArray(char* key);
private: private:
@ -61,10 +64,47 @@ private:
} }
}; };
class JsonArray : public JsonObjectBase
{
friend class JsonParserBase;
public:
public:
JsonArray()
{
}
char* getString(int index);
int getLength()
{
return tokens != NULL ? tokens[0].size : 0;
}
private:
JsonArray(char* json, jsmntok_t* tokens)
: JsonObjectBase(json, tokens)
{
}
};
class JsonParserBase class JsonParserBase
{ {
public: public:
JsonArray parseArray(char* json)
{
if (!parse(json) || tokens[0].type != JSMN_ARRAY)
return JsonArray();
return JsonArray(json, tokens);
}
JsonHashTable parseHashTable(char* json) JsonHashTable parseHashTable(char* json)
{ {
if (!parse(json) || tokens[0].type != JSMN_OBJECT) if (!parse(json) || tokens[0].type != JSMN_OBJECT)
@ -72,7 +112,7 @@ public:
return JsonHashTable(json, tokens); return JsonHashTable(json, tokens);
} }
protected: protected:
JsonParserBase(jsmntok_t* tokens, int maxTokenCount) JsonParserBase(jsmntok_t* tokens, int maxTokenCount)