diff options
author | Clodéric Mars <cms@masagroup.net> | 2012-05-24 11:25:40 +0400 |
---|---|---|
committer | Clodéric Mars <cms@masagroup.net> | 2012-05-24 11:27:45 +0400 |
commit | 4f55ca1c3cf7e458ecce2425225cbd72fd215e2c (patch) | |
tree | dab201ba97905237d296bde523ac63c4a25837e0 /src | |
parent | 8fb7fd07a918166e1d2ba0baaf75ab32f2061ba2 (diff) |
Adding methods to directly retrieve from a JSONValue its children
Diffstat (limited to 'src')
-rw-r--r-- | src/JSONValue.cpp | 95 | ||||
-rwxr-xr-x[-rw-r--r--] | src/JSONValue.h | 12 |
2 files changed, 104 insertions, 3 deletions
diff --git a/src/JSONValue.cpp b/src/JSONValue.cpp index 258fe65..dd4f49b 100644 --- a/src/JSONValue.cpp +++ b/src/JSONValue.cpp @@ -551,6 +551,101 @@ const JSONObject &JSONValue::AsObject() const } /** + * Retrieves the number of children of this JSONValue. + * This number will be 0 or the actual number of children + * if IsArray() or IsObject(). + * + * @access public + * + * @return The number of children. +*/ +std::size_t JSONValue::CountChildren() const +{ + switch (type) + { + case JSONType_Array: + return array_value.size(); + case JSONType_Object: + return object_value.size(); + default: + return 0; + } +} + +/** + * Checks if this JSONValue has a child at the given index. + * Use IsArray() before using this method. + * + * @access public +*/ +bool JSONValue::HasChild(std::size_t index) const +{ + if (type == JSONType_Array) + { + return index < array_value.size(); + } + else + { + return false; + } +} + +/** +* Retrieves the child of this JSONValue at the given index. +* Use IsArray() before using this method. +* +* @access public +*/ +JSONValue* JSONValue::Child(std::size_t index) +{ + if (index < array_value.size()) + { + return array_value[index]; + } + else + { + return NULL; + } +} + +/** +* Checks if this JSONValue as a child at the given key. +* Use IsObject() before using this method. +* +* @access public +*/ +bool JSONValue::HasChild(const wchar_t* name) const +{ + if (type == JSONType_Object) + { + return object_value.find(name) != object_value.end(); + } + else + { + return false; + } +} + +/** +* Retrieves the child of this JSONValue at the given key. +* Use IsObject() before using this method. +* +* @access public +*/ +JSONValue* JSONValue::Child(const wchar_t* name) +{ + JSONObject::const_iterator it = object_value.find(name); + if (it != object_value.end()) + { + return it->second; + } + else + { + return NULL; + } +} + +/** * Creates a JSON encoded string for the value with all necessary characters escaped * * @access public diff --git a/src/JSONValue.h b/src/JSONValue.h index 6215efd..71f6343 100644..100755 --- a/src/JSONValue.h +++ b/src/JSONValue.h @@ -47,7 +47,7 @@ class JSONValue JSONValue(const JSONArray &m_array_value); JSONValue(const JSONObject &m_object_value); ~JSONValue(); - + bool IsNull() const; bool IsString() const; bool IsBool() const; @@ -60,9 +60,15 @@ class JSONValue double AsNumber() const; const JSONArray &AsArray() const; const JSONObject &AsObject() const; - + + std::size_t CountChildren() const; + bool HasChild(std::size_t index) const; + JSONValue* Child(std::size_t index); + bool HasChild(const wchar_t* name) const; + JSONValue* Child(const wchar_t* name); + std::wstring Stringify() const; - + protected: static JSONValue *Parse(const wchar_t **data); |