use SwapPayload() to retain comments

All tests pass, but we might be missing coverage.

issue #47
This commit is contained in:
Christopher Dunn 2015-01-20 11:02:22 -06:00
parent 94b0297dc5
commit 66eb72f121
3 changed files with 14 additions and 10 deletions

View File

@ -235,23 +235,26 @@ Json::Value obj_value(Json::objectValue); // {}
Value(const CppTL::ConstString& value); Value(const CppTL::ConstString& value);
#endif #endif
Value(bool value); Value(bool value);
/// Deep copy.
Value(const Value& other); Value(const Value& other);
~Value(); ~Value();
// Deep copy, then swap(other).
Value& operator=(Value other); Value& operator=(Value other);
/// Swap values. /// Swap everything.
void swap(Value& other); void swap(Value& other);
/// Swap values but leave comments and source offsets in place.
void swapPayload(Value& other);
ValueType type() const; 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;
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; int compare(const Value& other) const;
const char* asCString() const; const char* asCString() const;
@ -442,9 +445,6 @@ private:
Value& resolveReference(const char* key, bool isStatic); 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 #ifdef JSON_VALUE_USE_INTERNAL_MAP
inline bool isItemAvailable() const { return itemIsUsed_ == 0; } inline bool isItemAvailable() const { return itemIsUsed_ == 0; }

View File

@ -173,9 +173,12 @@ bool Reader::readValue() {
currentValue().setOffsetLimit(token.end_ - begin_); currentValue().setOffsetLimit(token.end_ - begin_);
break; break;
case tokenNull: case tokenNull:
currentValue() = Value(); {
Value v;
currentValue().swapPayload(v);
currentValue().setOffsetStart(token.start_ - begin_); currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_); currentValue().setOffsetLimit(token.end_ - begin_);
}
break; break;
case tokenArraySeparator: case tokenArraySeparator:
if (features_.allowDroppedNullPlaceholders_) { if (features_.allowDroppedNullPlaceholders_) {
@ -393,7 +396,8 @@ bool Reader::readString() {
bool Reader::readObject(Token& tokenStart) { bool Reader::readObject(Token& tokenStart) {
Token tokenName; Token tokenName;
std::string name; std::string name;
currentValue() = Value(objectValue); Value init(objectValue);
currentValue().swapPayload(init);
currentValue().setOffsetStart(tokenStart.start_ - begin_); currentValue().setOffsetStart(tokenStart.start_ - begin_);
while (readToken(tokenName)) { while (readToken(tokenName)) {
bool initialTokenOk = true; bool initialTokenOk = true;
@ -486,7 +490,7 @@ bool Reader::decodeNumber(Token& token) {
Value decoded; Value decoded;
if (!decodeNumber(token, decoded)) if (!decodeNumber(token, decoded))
return false; return false;
currentValue() = decoded; currentValue().swapPayload(decoded);
currentValue().setOffsetStart(token.start_ - begin_); currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_); currentValue().setOffsetLimit(token.end_ - begin_);
return true; return true;

View File

@ -406,7 +406,7 @@ Value::~Value() {
} }
Value& Value::operator=(Value other) { Value& Value::operator=(Value other) {
swapPayload(other); swap(other);
return *this; return *this;
} }