Просмотр исходного кода

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

Benoit Blanchon 12 лет назад
Родитель
Сommit
80c2cf3567
6 измененных файлов с 28 добавлено и 5 удалено
  1. 6 1
      JsonArray.cpp
  2. 1 0
      JsonArray.h
  3. 7 2
      JsonHashTable.cpp
  4. 1 0
      JsonHashTable.h
  5. 11 1
      JsonObjectBase.cpp
  6. 2 1
      JsonObjectBase.h

+ 6 - 1
JsonArray.cpp

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

+ 1 - 0
JsonArray.h

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

+ 7 - 2
JsonHashTable.cpp

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

+ 1 - 0
JsonHashTable.h

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

+ 11 - 1
JsonObjectBase.cpp

@@ -6,6 +6,8 @@
 
 #include "JsonObjectBase.h"
 
+#include <stdlib.h> // for strtol
+
 int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
 {
 	int count = 0;
@@ -18,7 +20,15 @@ int JsonObjectBase::getNestedTokenCount(jsmntok_t* token)
 	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)
 		return 0;

+ 2 - 1
JsonObjectBase.h

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