From 66eb72f121e689c17186f050093e6c6eff3688d8 Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Tue, 20 Jan 2015 11:02:22 -0600 Subject: [PATCH] use SwapPayload() to retain comments All tests pass, but we might be missing coverage. issue #47 --- include/json/value.h | 12 ++++++------ src/lib_json/json_reader.cpp | 10 +++++++--- src/lib_json/json_value.cpp | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index 3473c7e..93112d1 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -235,23 +235,26 @@ Json::Value obj_value(Json::objectValue); // {} Value(const CppTL::ConstString& value); #endif Value(bool value); + /// Deep copy. Value(const Value& other); ~Value(); + // Deep copy, then swap(other). Value& operator=(Value other); - /// Swap values. + /// Swap everything. void swap(Value& other); + /// Swap values but leave comments and source offsets in place. + void swapPayload(Value& other); ValueType type() const; + /// Compare payload only, not comments etc. bool operator<(const Value& other) const; bool operator<=(const Value& other) const; bool operator>=(const Value& other) const; bool operator>(const Value& other) const; - bool operator==(const Value& other) const; bool operator!=(const Value& other) const; - int compare(const Value& other) const; const char* asCString() const; @@ -442,9 +445,6 @@ private: Value& resolveReference(const char* key, bool isStatic); - /// Swap values but leave comments and source offsets in place. - void swapPayload(Value& other); - #ifdef JSON_VALUE_USE_INTERNAL_MAP inline bool isItemAvailable() const { return itemIsUsed_ == 0; } diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 2e587ab..be22343 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -173,9 +173,12 @@ bool Reader::readValue() { currentValue().setOffsetLimit(token.end_ - begin_); break; case tokenNull: - currentValue() = Value(); + { + Value v; + currentValue().swapPayload(v); currentValue().setOffsetStart(token.start_ - begin_); currentValue().setOffsetLimit(token.end_ - begin_); + } break; case tokenArraySeparator: if (features_.allowDroppedNullPlaceholders_) { @@ -393,7 +396,8 @@ bool Reader::readString() { bool Reader::readObject(Token& tokenStart) { Token tokenName; std::string name; - currentValue() = Value(objectValue); + Value init(objectValue); + currentValue().swapPayload(init); currentValue().setOffsetStart(tokenStart.start_ - begin_); while (readToken(tokenName)) { bool initialTokenOk = true; @@ -486,7 +490,7 @@ bool Reader::decodeNumber(Token& token) { Value decoded; if (!decodeNumber(token, decoded)) return false; - currentValue() = decoded; + currentValue().swapPayload(decoded); currentValue().setOffsetStart(token.start_ - begin_); currentValue().setOffsetLimit(token.end_ - begin_); return true; diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 85b26ce..0a7fb85 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -406,7 +406,7 @@ Value::~Value() { } Value& Value::operator=(Value other) { - swapPayload(other); + swap(other); return *this; }