Explorar el Código

Added JsonArray::getBool() and JsonHashTable::getBool()

Benoit Blanchon hace 12 años
padre
commit
9c7ff38bbf
Se han modificado 6 ficheros con 30 adiciones y 0 borrados
  1. 5 0
      JsonArray.cpp
  2. 1 0
      JsonArray.h
  3. 5 0
      JsonHashTable.cpp
  4. 1 0
      JsonHashTable.h
  5. 17 0
      JsonObjectBase.cpp
  6. 1 0
      JsonObjectBase.h

+ 5 - 0
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));

+ 1 - 0
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);

+ 5 - 0
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));

+ 1 - 0
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);

+ 17 - 0
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;

+ 1 - 0
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);