From b9a75a320fabd2e991c297233ac90c0c1ec8bc6e Mon Sep 17 00:00:00 2001 From: Thulinma Date: Thu, 12 Jul 2012 01:03:40 +0200 Subject: [PATCH] Added missing JSON "operator bool()" and size() methods. --- AUTHORS | 4 +--- lib/json.cpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 915355b6..a74a17f1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,3 +1 @@ -All code was written by DDVTECH employees specifically for this project, with the exception of: - -* tinythread++ library taken verbatim from: http://tinythread.sourceforge.net +All code so far was written by DDVTECH employees. diff --git a/lib/json.cpp b/lib/json.cpp index 6fe60c84..14dd789f 100644 --- a/lib/json.cpp +++ b/lib/json.cpp @@ -283,6 +283,18 @@ JSON::Value::operator std::string(){ } } +/// Automatic conversion to bool. +/// Returns true if there is anything meaningful stored into this value. +JSON::Value::operator bool(){ + if (myType == STRING){return strVal == "";} + if (myType == INTEGER){return intVal == 0;} + if (myType == BOOL){return intVal == 0;} + if (myType == OBJECT){return size() > 0;} + if (myType == ARRAY){return size() > 0;} + if (myType == EMPTY){return false;} + return false;//unimplemented? should never happen... +} + /// Retrieves or sets the JSON::Value at this position in the object. /// Converts destructively to object if not already an object. JSON::Value & JSON::Value::operator[](const std::string i){ @@ -433,6 +445,11 @@ JSON::ArrIter JSON::Value::ArrEnd(){ return arrVal.end(); } +/// Returns the total of the objects and array size combined. +unsigned int JSON::Value::size(){ + return objVal.size() + arrVal.size(); +} + /// Completely clears the contents of this value, /// changing its type to NULL in the process. void JSON::Value::null(){