forked from bblanchon/ArduinoJson
Made JsonValue inherit from JsonObjectBase
This commit is contained in:
@ -8,21 +8,13 @@
|
|||||||
|
|
||||||
using namespace ArduinoJson::Parser;
|
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
|
* Returns the token for the value at the specified index
|
||||||
*/
|
*/
|
||||||
JsonValue JsonArray::operator[](int index)
|
JsonValue JsonArray::operator[](int index)
|
||||||
{
|
{
|
||||||
// sanity check
|
// sanity check
|
||||||
if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size)
|
if (!success() || index < 0 || index >= tokens[0].size)
|
||||||
return JsonValue();
|
return JsonValue();
|
||||||
|
|
||||||
// skip first token, it's the whole object
|
// skip first token, it's the whole object
|
||||||
|
@ -25,9 +25,14 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
JsonArray() {}
|
JsonArray() {}
|
||||||
|
|
||||||
|
bool success()
|
||||||
|
{
|
||||||
|
return JsonObjectBase::success() && tokens->type == JSMN_ARRAY;
|
||||||
|
}
|
||||||
|
|
||||||
int getLength()
|
int getLength()
|
||||||
{
|
{
|
||||||
return tokens != 0 ? tokens[0].size : 0;
|
return success() ? tokens[0].size : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue operator[](int index);
|
JsonValue operator[](int index);
|
||||||
@ -61,7 +66,11 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
JsonArray(char* json, jsmntok_t* tokens);
|
JsonArray(char* json, jsmntok_t* tokens)
|
||||||
|
: JsonObjectBase(json, tokens)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,20 +10,13 @@
|
|||||||
|
|
||||||
using namespace ArduinoJson::Parser;
|
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
|
* Returns the token for the value associated with the specified key
|
||||||
*/
|
*/
|
||||||
JsonValue JsonHashTable::operator [](const char* desiredKey)
|
JsonValue JsonHashTable::operator [](const char* desiredKey)
|
||||||
{
|
{
|
||||||
// sanity check
|
// sanity check
|
||||||
if (json == 0 || tokens == 0 || desiredKey == 0)
|
if (!success() || desiredKey == 0)
|
||||||
return JsonValue();
|
return JsonValue();
|
||||||
|
|
||||||
// skip first token, it's the whole object
|
// skip first token, it's the whole object
|
||||||
|
@ -25,6 +25,11 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
JsonHashTable() {}
|
JsonHashTable() {}
|
||||||
|
|
||||||
|
bool success()
|
||||||
|
{
|
||||||
|
return JsonObjectBase::success() && tokens->type == JSMN_OBJECT;
|
||||||
|
}
|
||||||
|
|
||||||
JsonValue operator[](const char* key);
|
JsonValue operator[](const char* key);
|
||||||
|
|
||||||
bool containsKey(const char* key)
|
bool containsKey(const char* key)
|
||||||
@ -61,7 +66,11 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
JsonHashTable(char* json, jsmntok_t* tokens);
|
JsonHashTable(char* json, jsmntok_t* tokens)
|
||||||
|
: JsonObjectBase(json, tokens)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,8 +16,9 @@ namespace ArduinoJson
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
JsonObjectBase()
|
JsonObjectBase()
|
||||||
|
: json(0), tokens(0)
|
||||||
{
|
{
|
||||||
makeInvalid();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool success()
|
bool success()
|
||||||
@ -28,15 +29,9 @@ namespace ArduinoJson
|
|||||||
protected:
|
protected:
|
||||||
|
|
||||||
JsonObjectBase(char* json, jsmntok_t* tokens)
|
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);
|
static int getNestedTokenCount(jsmntok_t* token);
|
||||||
|
@ -12,52 +12,52 @@ using namespace ArduinoJson::Parser;
|
|||||||
|
|
||||||
JsonValue::operator bool()
|
JsonValue::operator bool()
|
||||||
{
|
{
|
||||||
if (token == 0 || token->type != JSMN_PRIMITIVE) return 0;
|
if (tokens == 0 || tokens->type != JSMN_PRIMITIVE) return 0;
|
||||||
|
|
||||||
// "true"
|
// "true"
|
||||||
if (json[token->start] == 't') return true;
|
if (json[tokens->start] == 't') return true;
|
||||||
|
|
||||||
// "false"
|
// "false"
|
||||||
if (json[token->start] == 'f') return false;
|
if (json[tokens->start] == 'f') return false;
|
||||||
|
|
||||||
// "null"
|
// "null"
|
||||||
if (json[token->start] == 'n') return false;
|
if (json[tokens->start] == 'n') return false;
|
||||||
|
|
||||||
// number
|
// number
|
||||||
return strtol(json + token->start, 0, 0) != 0;
|
return strtol(json + tokens->start, 0, 0) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue::operator double()
|
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()
|
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*()
|
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;
|
return 0;
|
||||||
|
|
||||||
// add null terminator to the string
|
// 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()
|
JsonValue::operator JsonArray()
|
||||||
{
|
{
|
||||||
return JsonArray(json, token);
|
return JsonArray(json, tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue::operator JsonHashTable()
|
JsonValue::operator JsonHashTable()
|
||||||
{
|
{
|
||||||
return JsonHashTable(json, token);
|
return JsonHashTable(json, tokens);
|
||||||
}
|
}
|
@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
#include "jsmn.h"
|
#include "jsmn.h"
|
||||||
|
|
||||||
|
#include "JsonObjectBase.h"
|
||||||
|
|
||||||
namespace ArduinoJson
|
namespace ArduinoJson
|
||||||
{
|
{
|
||||||
namespace Parser
|
namespace Parser
|
||||||
@ -14,15 +16,19 @@ namespace ArduinoJson
|
|||||||
class JsonArray;
|
class JsonArray;
|
||||||
class JsonHashTable;
|
class JsonHashTable;
|
||||||
|
|
||||||
class JsonValue
|
class JsonValue : public JsonObjectBase
|
||||||
{
|
{
|
||||||
friend JsonArray;
|
friend JsonArray;
|
||||||
friend JsonHashTable;
|
friend JsonHashTable;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool success()
|
|
||||||
|
JsonValue() {}
|
||||||
|
|
||||||
|
JsonValue(char* json, jsmntok_t* tokens)
|
||||||
|
: JsonObjectBase(json, tokens)
|
||||||
{
|
{
|
||||||
return token != 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
operator bool();
|
operator bool();
|
||||||
@ -31,22 +37,6 @@ namespace ArduinoJson
|
|||||||
operator char*();
|
operator char*();
|
||||||
operator JsonArray();
|
operator JsonArray();
|
||||||
operator JsonHashTable();
|
operator JsonHashTable();
|
||||||
|
|
||||||
private:
|
|
||||||
JsonValue()
|
|
||||||
: json(0), token(0)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonValue(char* json, jsmntok_t* token)
|
|
||||||
: json(json), token(token)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
char* json;
|
|
||||||
jsmntok_t* token;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user