diff options
author | Mike Anchor <git@mjpa.co.uk> | 2015-06-30 22:34:26 +0300 |
---|---|---|
committer | Mike Anchor <git@mjpa.co.uk> | 2015-06-30 22:34:31 +0300 |
commit | 3260b36bb1fc7975151f9e27696927071c22015f (patch) | |
tree | 399539d91f551be918fc2d1540920bf26e0f8c75 | |
parent | f94cdfa5733a03b9fea91184a6980db7a40755ec (diff) |
Adding copy constructor for deep copying of array/object values. This closes #24
-rw-r--r-- | src/JSONValue.cpp | 52 | ||||
-rwxr-xr-x | src/JSONValue.h | 1 | ||||
-rw-r--r-- | src/demo/testcases.cpp | 21 |
3 files changed, 73 insertions, 1 deletions
diff --git a/src/JSONValue.cpp b/src/JSONValue.cpp index 44ff337..c15f686 100644 --- a/src/JSONValue.cpp +++ b/src/JSONValue.cpp @@ -390,6 +390,58 @@ JSONValue::JSONValue(const JSONObject &m_object_value) } /** + * Copy constructor to perform a deep copy of array / object values + * + * @access public + * + * @param JSONValue m_source The source JSONValue that is being copied + */ +JSONValue::JSONValue(const JSONValue &m_source) +{ + type = m_source.type; + + switch (type) + { + case JSONType_String: + string_value = m_source.string_value; + break; + + case JSONType_Bool: + bool_value = m_source.bool_value; + break; + + case JSONType_Number: + number_value = m_source.number_value; + break; + + case JSONType_Array: + { + JSONArray source_array = m_source.array_value; + JSONArray::iterator iter; + for (iter = source_array.begin(); iter != source_array.end(); iter++) + array_value.push_back(new JSONValue(**iter)); + break; + } + + case JSONType_Object: + { + JSONObject source_object = m_source.object_value; + JSONObject::iterator iter; + for (iter = source_object.begin(); iter != source_object.end(); iter++) + { + std::wstring name = (*iter).first; + object_value[name] = new JSONValue(*((*iter).second)); + } + break; + } + + case JSONType_Null: + // Nothing to do. + break; + } +} + +/** * The destructor for the JSON Value object * Handles deleting the objects in the array or the object value * diff --git a/src/JSONValue.h b/src/JSONValue.h index 9a239bb..02d0fab 100755 --- a/src/JSONValue.h +++ b/src/JSONValue.h @@ -46,6 +46,7 @@ class JSONValue JSONValue(double m_number_value); JSONValue(const JSONArray &m_array_value); JSONValue(const JSONObject &m_object_value); + JSONValue(const JSONValue &m_source); ~JSONValue(); bool IsNull() const; diff --git a/src/demo/testcases.cpp b/src/demo/testcases.cpp index db168fc..3624811 100644 --- a/src/demo/testcases.cpp +++ b/src/demo/testcases.cpp @@ -39,6 +39,9 @@ using namespace std; +// Defined in testcases.cpp +extern const wchar_t* EXAMPLE; + // Helper to do a quick parse check bool parse_check(wstring str) { @@ -157,6 +160,22 @@ void run_tests() test_output += wstring(L"failed |\r\n"); } print_out(test_output.c_str()); - + + // Test case for issue #24. + test_output = wstring(L"| Testing JSONValue passing as value") + wstring(DESC_LENGTH - 34, L' ') + wstring(L" | "); + JSONValue *value = JSON::Parse(EXAMPLE); + wstring json_check = value->Stringify(); + JSONValue new_value(*value); + delete value; + if (new_value.Stringify() == json_check) + { + test_output += wstring(L"passed |\r\n"); + } + else + { + test_output += wstring(L"failed |\r\n"); + } + print_out(test_output.c_str()); + print_out(vert_sep.c_str()); } |