mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-18 04:52:22 +02:00
Made JsonValue inherit from JsonObjectBase
This commit is contained in:
@ -8,34 +8,26 @@
|
||||
|
||||
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)
|
||||
// sanity check
|
||||
if (!success() || index < 0 || index >= tokens[0].size)
|
||||
return JsonValue();
|
||||
|
||||
// skip first token, it's the whole object
|
||||
jsmntok_t* currentToken = tokens + 1;
|
||||
// skip first token, it's the whole object
|
||||
jsmntok_t* currentToken = tokens + 1;
|
||||
|
||||
// skip all tokens before the specified index
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
// move forward: current + nested tokens
|
||||
currentToken += 1 + getNestedTokenCount(currentToken);
|
||||
}
|
||||
// skip all tokens before the specified index
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
// move forward: current + nested tokens
|
||||
currentToken += 1 + getNestedTokenCount(currentToken);
|
||||
}
|
||||
|
||||
return JsonValue(json, currentToken);
|
||||
return JsonValue(json, currentToken);
|
||||
}
|
||||
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -23,8 +23,13 @@ namespace ArduinoJson
|
||||
|
||||
public:
|
||||
|
||||
JsonHashTable() {}
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -16,8 +16,9 @@ namespace ArduinoJson
|
||||
public:
|
||||
|
||||
JsonObjectBase()
|
||||
{
|
||||
makeInvalid();
|
||||
: json(0), tokens(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -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);
|
||||
}
|
@ -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;
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user