Added JsonArray::getDouble() and JsonHashTable::getDouble()

This commit is contained in:
Benoit Blanchon
2014-01-12 17:08:22 +01:00
parent 80c2cf3567
commit 9e91fb3f46
6 changed files with 25 additions and 5 deletions

View File

@ -41,6 +41,11 @@ JsonArray JsonArray::getArray(int index)
return JsonArray(json, getToken(index));
}
double JsonArray::getDouble(int index)
{
return getDoubleFromToken(getToken(index));
}
JsonHashTable JsonArray::getHashTable(int index)
{
return JsonHashTable(json, getToken(index));

View File

@ -26,7 +26,8 @@ public:
return tokens != 0 ? tokens[0].size : 0;
}
JsonArray getArray(int index);
JsonArray getArray(int index);
double getDouble(int index);
JsonHashTable getHashTable(int index);
long getLong(int index);
char* getString(int index);

View File

@ -53,6 +53,11 @@ JsonArray JsonHashTable::getArray(char* key)
return JsonArray(json, getToken(key));
}
double JsonHashTable::getDouble(char* key)
{
return getDoubleFromToken(getToken(key));
}
JsonHashTable JsonHashTable::getHashTable(char* key)
{
return JsonHashTable(json, getToken(key));

View File

@ -22,6 +22,7 @@ public:
JsonHashTable() {}
JsonArray getArray(char* key);
double getDouble(char* key);
JsonHashTable getHashTable(char* key);
long getLong(char* key);
char* getString(char* key);

View File

@ -6,7 +6,7 @@
#include "JsonObjectBase.h"
#include <stdlib.h> // for strtol
#include <stdlib.h> // for strtol, strtod
int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
{
@ -20,10 +20,16 @@ int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
return count;
}
double JsonObjectBase::getDoubleFromToken(jsmntok_t* token)
{
if (token->type != JSMN_PRIMITIVE) return 0;
return strtod(json + token->start, 0);
}
long JsonObjectBase::getLongFromToken(jsmntok_t* token)
{
if (token->type != JSMN_PRIMITIVE)
return 0;
if (token->type != JSMN_PRIMITIVE) return 0;
return strtol(json + token->start, 0, 0);
}

View File

@ -38,8 +38,10 @@ protected:
}
static int getNestedTokenCount(jsmntok_t* token);
char* getStringFromToken(jsmntok_t* token);
double getDoubleFromToken(jsmntok_t* token);
long getLongFromToken(jsmntok_t* token);
char* getStringFromToken(jsmntok_t* token);
char* json;
jsmntok_t* tokens;