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

View File

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

View File

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

View File

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

View File

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

View File

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