diff --git a/src/JSONValue.cpp b/src/JSONValue.cpp index cbf8648..bb4e192 100644 --- a/src/JSONValue.cpp +++ b/src/JSONValue.cpp @@ -410,25 +410,50 @@ JSONValue::JSONValue(const JSONObject &m_object_value) */ JSONValue::JSONValue(const JSONValue &m_source) { - type = m_source.type; + DeepAssignment (m_source); +} + +/** + * Assignment operator to perform a deep copy of array / object values + * + * @access public + * + * @param JSONValue m_source The source JSONValue that is being copied + */ +JSONValue& JSONValue::operator= (const JSONValue &m_source) +{ + if (this == &m_source) + { + // Self-assignment, return without doing anything + return *this; + } + + DeepAssignment (m_source); + + return *this; +} + +void JSONValue::DeepAssignment (const JSONValue &source) +{ + type = source.type; switch (type) { case JSONType_String: - string_value = m_source.string_value; + string_value = source.string_value; break; case JSONType_Bool: - bool_value = m_source.bool_value; + bool_value = source.bool_value; break; case JSONType_Number: - number_value = m_source.number_value; + number_value = source.number_value; break; case JSONType_Array: { - JSONArray source_array = m_source.array_value; + JSONArray source_array = source.array_value; JSONArray::iterator iter; for (iter = source_array.begin(); iter != source_array.end(); iter++) array_value.push_back(new JSONValue(**iter)); @@ -437,7 +462,7 @@ JSONValue::JSONValue(const JSONValue &m_source) case JSONType_Object: { - JSONObject source_object = m_source.object_value; + JSONObject source_object = source.object_value; JSONObject::iterator iter; for (iter = source_object.begin(); iter != source_object.end(); iter++) { diff --git a/src/JSONValue.h b/src/JSONValue.h index 02d0fab..c49f94f 100644 --- a/src/JSONValue.h +++ b/src/JSONValue.h @@ -47,6 +47,7 @@ class JSONValue JSONValue(const JSONArray &m_array_value); JSONValue(const JSONObject &m_object_value); JSONValue(const JSONValue &m_source); + JSONValue& operator= (const JSONValue &m_source); ~JSONValue(); bool IsNull() const; @@ -74,6 +75,7 @@ class JSONValue static JSONValue *Parse(const wchar_t **data); private: + void DeepAssignment (const JSONValue &source); static std::wstring StringifyString(const std::wstring &str); std::wstring StringifyImpl(size_t const indentDepth) const; static std::wstring Indent(size_t depth);