diff --git a/JsonArray.cpp b/JsonArray.cpp index f8838df5..f2e1bfae 100644 --- a/JsonArray.cpp +++ b/JsonArray.cpp @@ -41,6 +41,11 @@ JsonArray JsonArray::getArray(int index) return JsonArray(json, getToken(index)); } +bool JsonArray::getBool(int index) +{ + return getBoolFromToken(getToken(index)); +} + double JsonArray::getDouble(int index) { return getDoubleFromToken(getToken(index)); diff --git a/JsonArray.h b/JsonArray.h index e7f29c10..b9cdfd07 100644 --- a/JsonArray.h +++ b/JsonArray.h @@ -27,6 +27,7 @@ public: } JsonArray getArray(int index); + bool getBool(int index); double getDouble(int index); JsonHashTable getHashTable(int index); long getLong(int index); diff --git a/JsonHashTable.cpp b/JsonHashTable.cpp index 111f5faf..a44768f0 100644 --- a/JsonHashTable.cpp +++ b/JsonHashTable.cpp @@ -53,6 +53,11 @@ JsonArray JsonHashTable::getArray(char* key) return JsonArray(json, getToken(key)); } +bool JsonHashTable::getBool(char* key) +{ + return getBoolFromToken(getToken(key)); +} + double JsonHashTable::getDouble(char* key) { return getDoubleFromToken(getToken(key)); diff --git a/JsonHashTable.h b/JsonHashTable.h index 560faace..57cae390 100644 --- a/JsonHashTable.h +++ b/JsonHashTable.h @@ -22,6 +22,7 @@ public: JsonHashTable() {} JsonArray getArray(char* key); + bool getBool(char* key); double getDouble(char* key); JsonHashTable getHashTable(char* key); long getLong(char* key); diff --git a/JsonObjectBase.cpp b/JsonObjectBase.cpp index cee98bab..1e158678 100644 --- a/JsonObjectBase.cpp +++ b/JsonObjectBase.cpp @@ -20,6 +20,23 @@ int JsonObjectBase::getNestedTokenCount(jsmntok_t* token) return count; } +bool JsonObjectBase::getBoolFromToken(jsmntok_t* token) +{ + if (token->type != JSMN_PRIMITIVE) return 0; + + // "true" + if (json[token->start] == 't') return true; + + // "false" + if (json[token->start] == 'f') return false; + + // "null" + if (json[token->start] == 'n') return false; + + // number + return strtol(json + token->start, 0, 0) != 0; +} + double JsonObjectBase::getDoubleFromToken(jsmntok_t* token) { if (token->type != JSMN_PRIMITIVE) return 0; diff --git a/JsonObjectBase.h b/JsonObjectBase.h index 9ca779da..091a968d 100644 --- a/JsonObjectBase.h +++ b/JsonObjectBase.h @@ -39,6 +39,7 @@ protected: static int getNestedTokenCount(jsmntok_t* token); + bool getBoolFromToken(jsmntok_t* token); double getDoubleFromToken(jsmntok_t* token); long getLongFromToken(jsmntok_t* token); char* getStringFromToken(jsmntok_t* token);