Made JsonValue inherit from JsonObjectBase

This commit is contained in:
Benoît Blanchon
2014-07-16 13:41:00 +02:00
parent d189bd7140
commit 6a868e46bd
7 changed files with 61 additions and 73 deletions

View File

@ -8,21 +8,13 @@
using namespace ArduinoJson::Parser;
JsonArray::JsonArray(char* json, jsmntok_t* tokens)
: JsonObjectBase(json, tokens)
{
if (tokens == 0 || tokens[0].type != JSMN_ARRAY)
makeInvalid();
}
/*
* Returns the token for the value at the specified index
*/
JsonValue JsonArray::operator[](int index)
{
// sanity check
if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size)
if (!success() || index < 0 || index >= tokens[0].size)
return JsonValue();
// skip first token, it's the whole object

View File

@ -25,9 +25,14 @@ namespace ArduinoJson
JsonArray() {}
bool success()
{
return JsonObjectBase::success() && tokens->type == JSMN_ARRAY;
}
int getLength()
{
return tokens != 0 ? tokens[0].size : 0;
return success() ? tokens[0].size : 0;
}
JsonValue operator[](int index);
@ -61,7 +66,11 @@ namespace ArduinoJson
private:
JsonArray(char* json, jsmntok_t* tokens);
JsonArray(char* json, jsmntok_t* tokens)
: JsonObjectBase(json, tokens)
{
}
};
}
}

View File

@ -10,20 +10,13 @@
using namespace ArduinoJson::Parser;
JsonHashTable::JsonHashTable(char* json, jsmntok_t* tokens)
: JsonObjectBase(json, tokens)
{
if (tokens == 0 || tokens[0].type != JSMN_OBJECT)
makeInvalid();
}
/*
* Returns the token for the value associated with the specified key
*/
JsonValue JsonHashTable::operator [](const char* desiredKey)
{
// sanity check
if (json == 0 || tokens == 0 || desiredKey == 0)
if (!success() || desiredKey == 0)
return JsonValue();
// skip first token, it's the whole object

View File

@ -25,6 +25,11 @@ namespace ArduinoJson
JsonHashTable() {}
bool success()
{
return JsonObjectBase::success() && tokens->type == JSMN_OBJECT;
}
JsonValue operator[](const char* key);
bool containsKey(const char* key)
@ -61,7 +66,11 @@ namespace ArduinoJson
private:
JsonHashTable(char* json, jsmntok_t* tokens);
JsonHashTable(char* json, jsmntok_t* tokens)
: JsonObjectBase(json, tokens)
{
}
};
}
}

View File

@ -16,8 +16,9 @@ namespace ArduinoJson
public:
JsonObjectBase()
: json(0), tokens(0)
{
makeInvalid();
}
bool success()
@ -28,15 +29,9 @@ namespace ArduinoJson
protected:
JsonObjectBase(char* json, jsmntok_t* tokens)
: json(json), tokens(tokens)
{
this->json = json;
this->tokens = tokens;
}
void makeInvalid()
{
json = 0;
tokens = 0;
}
static int getNestedTokenCount(jsmntok_t* token);

View File

@ -12,52 +12,52 @@ using namespace ArduinoJson::Parser;
JsonValue::operator bool()
{
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
if (tokens == 0 || tokens->type != JSMN_PRIMITIVE) return 0;
// "true"
if (json[token->start] == 't') return true;
if (json[tokens->start] == 't') return true;
// "false"
if (json[token->start] == 'f') return false;
if (json[tokens->start] == 'f') return false;
// "null"
if (json[token->start] == 'n') return false;
if (json[tokens->start] == 'n') return false;
// number
return strtol(json + token->start, 0, 0) != 0;
return strtol(json + tokens->start, 0, 0) != 0;
}
JsonValue::operator double()
{
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
if (tokens == 0 || tokens->type != JSMN_PRIMITIVE) return 0;
return strtod(json + token->start, 0);
return strtod(json + tokens->start, 0);
}
JsonValue::operator long()
{
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
if (tokens == 0 || tokens->type != JSMN_PRIMITIVE) return 0;
return strtol(json + token->start, 0, 0);
return strtol(json + tokens->start, 0, 0);
}
JsonValue::operator char*()
{
if (token == 0 || token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING)
if (tokens == 0 || tokens->type != JSMN_PRIMITIVE && tokens->type != JSMN_STRING)
return 0;
// add null terminator to the string
json[token->end] = 0;
json[tokens->end] = 0;
return json + token->start;
return json + tokens->start;
}
JsonValue::operator JsonArray()
{
return JsonArray(json, token);
return JsonArray(json, tokens);
}
JsonValue::operator JsonHashTable()
{
return JsonHashTable(json, token);
return JsonHashTable(json, tokens);
}

View File

@ -7,6 +7,8 @@
#include "jsmn.h"
#include "JsonObjectBase.h"
namespace ArduinoJson
{
namespace Parser
@ -14,15 +16,19 @@ namespace ArduinoJson
class JsonArray;
class JsonHashTable;
class JsonValue
class JsonValue : public JsonObjectBase
{
friend JsonArray;
friend JsonHashTable;
public:
bool success()
JsonValue() {}
JsonValue(char* json, jsmntok_t* tokens)
: JsonObjectBase(json, tokens)
{
return token != 0;
}
operator bool();
@ -31,22 +37,6 @@ namespace ArduinoJson
operator char*();
operator JsonArray();
operator JsonHashTable();
private:
JsonValue()
: json(0), token(0)
{
}
JsonValue(char* json, jsmntok_t* token)
: json(json), token(token)
{
}
char* json;
jsmntok_t* token;
};
}
}