diff options
author | Mike Anchor <git@mjpa.co.uk> | 2012-05-26 18:40:13 +0400 |
---|---|---|
committer | Mike Anchor <git@mjpa.co.uk> | 2012-05-26 18:40:13 +0400 |
commit | 7e6e3d6629b12a747ccfc7d7da4c26f26d2ee7c3 (patch) | |
tree | a0a0e2779cc6ae4e33fa3911800c01605304db7c /src | |
parent | d25146e0ef9b26081079484f9a5dec8c4d25a55d (diff) | |
parent | 4f55ca1c3cf7e458ecce2425225cbd72fd215e2c (diff) |
Merge pull request #10 from cloderic/master
Addition of methods to JSONValue allowing the direct retrieval of the 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 52537eb..ef17abf 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); |