Added verification of the type of token

This commit is contained in:
Benoit Blanchon
2014-01-11 16:41:18 +01:00
parent da37162a94
commit 4713e90f12
7 changed files with 74 additions and 74 deletions

View File

@ -1,20 +1,32 @@
/* /*
* malloc-free JSON parser for Arduino * malloc-free JSON parser for Arduino
* Benoit Blanchon 2014 * Benoit Blanchon 2014 - MIT License
* MIT License
*/ */
#include "JsonArray.h" #include "JsonArray.h"
#include "JsonHashTable.h" #include "JsonHashTable.h"
JsonArray::JsonArray(char* json, jsmntok_t* tokens)
: JsonObjectBase(json, tokens)
{
if (tokens[0].type != JSMN_ARRAY)
makeInvalid();
}
/*
* Returns the token for the value at the specified index
*/
jsmntok_t* JsonArray::getToken(int index) jsmntok_t* JsonArray::getToken(int index)
{ {
// sanity check
if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size) if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size)
return 0; return 0;
// skip first token, it's the whole object // skip first token, it's the whole object
int currentToken = 1; int currentToken = 1;
// skip all tokens before the specified index
for (int i = 0; i < index; i++) for (int i = 0; i < index; i++)
{ {
// move forward: current + nested tokens // move forward: current + nested tokens
@ -26,14 +38,12 @@ jsmntok_t* JsonArray::getToken(int index)
JsonArray JsonArray::getArray(int index) JsonArray JsonArray::getArray(int index)
{ {
jsmntok_t* token = getToken(index); return JsonArray(json, getToken(index));
return JsonArray(json, token);
} }
JsonHashTable JsonArray::getHashTable(int index) JsonHashTable JsonArray::getHashTable(int index)
{ {
jsmntok_t* token = getToken(index); return JsonHashTable(json, getToken(index));
return JsonHashTable(json, token);
} }
char* JsonArray::getString(int index) char* JsonArray::getString(int index)

View File

@ -1,7 +1,6 @@
/* /*
* malloc-free JSON parser for Arduino * malloc-free JSON parser for Arduino
* Benoit Blanchon 2014 * Benoit Blanchon 2014 - MIT License
* MIT License
*/ */
#ifndef __JSONARRAY_H #ifndef __JSONARRAY_H
@ -31,13 +30,8 @@ public:
private: private:
JsonArray(char* json, jsmntok_t* tokens) JsonArray(char* json, jsmntok_t* tokens);
: JsonObjectBase(json, tokens)
{
}
jsmntok_t* getToken(int index); jsmntok_t* getToken(int index);
}; };
#endif #endif

View File

@ -1,7 +1,6 @@
/* /*
* malloc-free JSON parser for Arduino * malloc-free JSON parser for Arduino
* Benoit Blanchon 2014 * Benoit Blanchon 2014 - MIT License
* MIT License
*/ */
#include "JsonArray.h" #include "JsonArray.h"
@ -9,10 +8,20 @@
#include <string.h> // for strcmp() #include <string.h> // for strcmp()
jsmntok_t* JsonHashTable::getToken(char* name) JsonHashTable::JsonHashTable(char* json, jsmntok_t* tokens)
: JsonObjectBase(json, tokens)
{
if (tokens[0].type != JSMN_OBJECT)
makeInvalid();
}
/*
* Returns the token for the value associated with the specified key
*/
jsmntok_t* JsonHashTable::getToken(char* desiredKey)
{ {
// sanity check // sanity check
if (json == 0 || tokens == 0 || name == 0) if (json == 0 || tokens == 0 || desiredKey == 0)
return 0; return 0;
// skip first token, it's the whole object // skip first token, it's the whole object
@ -21,11 +30,11 @@ jsmntok_t* JsonHashTable::getToken(char* name)
// scan each keys // scan each keys
for (int i = 0; i < tokens[0].size / 2 ; i++) for (int i = 0; i < tokens[0].size / 2 ; i++)
{ {
// Get key token string // get key token string
char* key = json + tokens[currentToken].start; char* key = json + tokens[currentToken].start;
// Compare with desired name // Compare with desired name
if (strcmp(name, key) == 0) if (strcmp(desiredKey, key) == 0)
{ {
return &tokens[currentToken + 1]; return &tokens[currentToken + 1];
} }
@ -40,14 +49,12 @@ jsmntok_t* JsonHashTable::getToken(char* name)
JsonArray JsonHashTable::getArray(char* key) JsonArray JsonHashTable::getArray(char* key)
{ {
jsmntok_t* token = getToken(key); return JsonArray(json, getToken(key));
return JsonArray(json, token);
} }
JsonHashTable JsonHashTable::getHashTable(char* key) JsonHashTable JsonHashTable::getHashTable(char* key)
{ {
jsmntok_t* token = getToken(key); return JsonHashTable(json, getToken(key));
return JsonHashTable(json, token);
} }
char* JsonHashTable::getString(char* key) char* JsonHashTable::getString(char* key)

View File

@ -1,7 +1,6 @@
/* /*
* malloc-free JSON parser for Arduino * malloc-free JSON parser for Arduino
* Benoit Blanchon 2014 * Benoit Blanchon 2014 - MIT License
* MIT License
*/ */
#ifndef __JSONHASHTABLE_H #ifndef __JSONHASHTABLE_H
@ -26,13 +25,8 @@ public:
private: private:
JsonHashTable(char* json, jsmntok_t* tokens) JsonHashTable(char* json, jsmntok_t* tokens);
: JsonObjectBase(json, tokens)
{
}
jsmntok_t* getToken(char* key); jsmntok_t* getToken(char* key);
}; };
#endif #endif

View File

@ -15,8 +15,7 @@ public:
JsonObjectBase() JsonObjectBase()
{ {
json = 0; makeInvalid();
tokens = 0;
} }
bool success() bool success()
@ -32,6 +31,12 @@ protected:
this->tokens = tokens; this->tokens = tokens;
} }
void makeInvalid()
{
json = 0;
tokens = 0;
}
int getNestedTokenCount(int tokenIndex); int getNestedTokenCount(int tokenIndex);
char* json; char* json;

View File

@ -1,23 +1,28 @@
/* /*
* malloc-free JSON parser for Arduino * malloc-free JSON parser for Arduino
* Benoit Blanchon 2014 * Benoit Blanchon 2014 - MIT License
* MIT License
*/ */
#include "JsonParser.h" #include "JsonParser.h"
bool JsonParserBase::parse(char* jsonString) JsonParserBase::JsonParserBase(jsmntok_t* tokens, int maxTokenCount)
{ {
buffer = jsonString; this->maxTokenCount = maxTokenCount;
this->tokens = tokens;
jsmn_init(&parser);
}
jsmntok_t* JsonParserBase::parse(char* jsonString)
{
if (JSMN_SUCCESS != jsmn_parse(&parser, jsonString, tokens, maxTokenCount)) if (JSMN_SUCCESS != jsmn_parse(&parser, jsonString, tokens, maxTokenCount))
return false; return 0;
// Add null termination to each token // Add null termination to each token
for (int i = 1; i < parser.toknext; i++) for (int i = 1; i < parser.toknext; i++)
{ {
buffer[tokens[i].end] = 0; jsonString[tokens[i].end] = 0;
} }
return true; return tokens;
} }

View File

@ -1,7 +1,6 @@
/* /*
* malloc-free JSON parser for Arduino * malloc-free JSON parser for Arduino
* Benoit Blanchon 2014 * Benoit Blanchon 2014 - MIT License
* MIT License
*/ */
#ifndef __JSONPARSER_H #ifndef __JSONPARSER_H
@ -16,35 +15,22 @@ public:
JsonArray parseArray(char* json) JsonArray parseArray(char* json)
{ {
if (!parse(json) || tokens[0].type != JSMN_ARRAY) return JsonArray(json, parse(json));
return JsonArray();
return JsonArray(json, tokens);
} }
JsonHashTable parseHashTable(char* json) JsonHashTable parseHashTable(char* json)
{ {
if (!parse(json) || tokens[0].type != JSMN_OBJECT) return JsonHashTable(json, parse(json));
return JsonHashTable();
return JsonHashTable(json, tokens);
} }
protected: protected:
JsonParserBase(jsmntok_t* tokens, int maxTokenCount) JsonParserBase(jsmntok_t* tokens, int maxTokenCount);
{
this->maxTokenCount = maxTokenCount;
this->tokens = tokens;
jsmn_init(&parser);
}
bool parse(char* json);
private: private:
char* buffer; jsmntok_t* parse(char* json);
jsmn_parser parser; jsmn_parser parser;
int maxTokenCount; int maxTokenCount;
jsmntok_t* tokens; jsmntok_t* tokens;
@ -58,7 +44,6 @@ public:
JsonParser() JsonParser()
: JsonParserBase(tokens, N) : JsonParserBase(tokens, N)
{ {
} }
private: private: