Преглед изворни кода

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

Benoit Blanchon пре 12 година
родитељ
комит
9e91fb3f46
6 измењених фајлова са 25 додато и 5 уклоњено
  1. 5 0
      JsonArray.cpp
  2. 2 1
      JsonArray.h
  3. 5 0
      JsonHashTable.cpp
  4. 1 0
      JsonHashTable.h
  5. 9 3
      JsonObjectBase.cpp
  6. 3 1
      JsonObjectBase.h

+ 5 - 0
JsonArray.cpp

@@ -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));

+ 2 - 1
JsonArray.h

@@ -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);

+ 5 - 0
JsonHashTable.cpp

@@ -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));

+ 1 - 0
JsonHashTable.h

@@ -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);

+ 9 - 3
JsonObjectBase.cpp

@@ -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);
 }

+ 3 - 1
JsonObjectBase.h

@@ -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;