Added JsonArray::getLong() and JsonHashTable::getLong()

This commit is contained in:
Benoit Blanchon
2014-01-12 12:57:44 +01:00
parent b3647a7d91
commit 80c2cf3567
6 changed files with 28 additions and 5 deletions

View File

@ -46,7 +46,12 @@ JsonHashTable JsonArray::getHashTable(int index)
return JsonHashTable(json, getToken(index)); return JsonHashTable(json, getToken(index));
} }
long JsonArray::getLong(int index)
{
return getLongFromToken(getToken(index));
}
char* JsonArray::getString(int index) char* JsonArray::getString(int index)
{ {
return getTokenString(getToken(index)); return getStringFromToken(getToken(index));
} }

View File

@ -28,6 +28,7 @@ public:
JsonArray getArray(int index); JsonArray getArray(int index);
JsonHashTable getHashTable(int index); JsonHashTable getHashTable(int index);
long getLong(int index);
char* getString(int index); char* getString(int index);
private: private:

View File

@ -31,7 +31,7 @@ jsmntok_t* JsonHashTable::getToken(char* desiredKey)
for (int i = 0; i < tokens[0].size / 2 ; i++) for (int i = 0; i < tokens[0].size / 2 ; i++)
{ {
// get key token string // get key token string
char* key = getTokenString(currentToken); char* key = getStringFromToken(currentToken);
// compare with desired name // compare with desired name
if (strcmp(desiredKey, key) == 0) if (strcmp(desiredKey, key) == 0)
@ -58,7 +58,12 @@ JsonHashTable JsonHashTable::getHashTable(char* key)
return JsonHashTable(json, getToken(key)); return JsonHashTable(json, getToken(key));
} }
long JsonHashTable::getLong(char* key)
{
return getLongFromToken(getToken(key));
}
char* JsonHashTable::getString(char* key) char* JsonHashTable::getString(char* key)
{ {
return getTokenString(getToken(key)); return getStringFromToken(getToken(key));
} }

View File

@ -23,6 +23,7 @@ public:
JsonArray getArray(char* key); JsonArray getArray(char* key);
JsonHashTable getHashTable(char* key); JsonHashTable getHashTable(char* key);
long getLong(char* key);
char* getString(char* key); char* getString(char* key);
private: private:

View File

@ -6,6 +6,8 @@
#include "JsonObjectBase.h" #include "JsonObjectBase.h"
#include <stdlib.h> // for strtol
int JsonObjectBase::getNestedTokenCount(jsmntok_t* token) int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
{ {
int count = 0; int count = 0;
@ -18,7 +20,15 @@ int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
return count; return count;
} }
char* JsonObjectBase::getTokenString(jsmntok_t* token) long JsonObjectBase::getLongFromToken(jsmntok_t* token)
{
if (token->type != JSMN_PRIMITIVE)
return 0;
return strtol(json + token->start, 0, 0);
}
char* JsonObjectBase::getStringFromToken(jsmntok_t* token)
{ {
if (token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING) if (token->type != JSMN_PRIMITIVE && token->type != JSMN_STRING)
return 0; return 0;

View File

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