Skip to content

Commit 66eb72f

Browse files
committed
use SwapPayload() to retain comments
All tests pass, but we might be missing coverage. issue #47
1 parent 94b0297 commit 66eb72f

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

include/json/value.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,23 +235,26 @@ Json::Value obj_value(Json::objectValue); // {}
235235
Value(const CppTL::ConstString& value);
236236
#endif
237237
Value(bool value);
238+
/// Deep copy.
238239
Value(const Value& other);
239240
~Value();
240241

242+
// Deep copy, then swap(other).
241243
Value& operator=(Value other);
242-
/// Swap values.
244+
/// Swap everything.
243245
void swap(Value& other);
246+
/// Swap values but leave comments and source offsets in place.
247+
void swapPayload(Value& other);
244248

245249
ValueType type() const;
246250

251+
/// Compare payload only, not comments etc.
247252
bool operator<(const Value& other) const;
248253
bool operator<=(const Value& other) const;
249254
bool operator>=(const Value& other) const;
250255
bool operator>(const Value& other) const;
251-
252256
bool operator==(const Value& other) const;
253257
bool operator!=(const Value& other) const;
254-
255258
int compare(const Value& other) const;
256259

257260
const char* asCString() const;
@@ -442,9 +445,6 @@ Json::Value obj_value(Json::objectValue); // {}
442445

443446
Value& resolveReference(const char* key, bool isStatic);
444447

445-
/// Swap values but leave comments and source offsets in place.
446-
void swapPayload(Value& other);
447-
448448
#ifdef JSON_VALUE_USE_INTERNAL_MAP
449449
inline bool isItemAvailable() const { return itemIsUsed_ == 0; }
450450

src/lib_json/json_reader.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,12 @@ bool Reader::readValue() {
173173
currentValue().setOffsetLimit(token.end_ - begin_);
174174
break;
175175
case tokenNull:
176-
currentValue() = Value();
176+
{
177+
Value v;
178+
currentValue().swapPayload(v);
177179
currentValue().setOffsetStart(token.start_ - begin_);
178180
currentValue().setOffsetLimit(token.end_ - begin_);
181+
}
179182
break;
180183
case tokenArraySeparator:
181184
if (features_.allowDroppedNullPlaceholders_) {
@@ -393,7 +396,8 @@ bool Reader::readString() {
393396
bool Reader::readObject(Token& tokenStart) {
394397
Token tokenName;
395398
std::string name;
396-
currentValue() = Value(objectValue);
399+
Value init(objectValue);
400+
currentValue().swapPayload(init);
397401
currentValue().setOffsetStart(tokenStart.start_ - begin_);
398402
while (readToken(tokenName)) {
399403
bool initialTokenOk = true;
@@ -486,7 +490,7 @@ bool Reader::decodeNumber(Token& token) {
486490
Value decoded;
487491
if (!decodeNumber(token, decoded))
488492
return false;
489-
currentValue() = decoded;
493+
currentValue().swapPayload(decoded);
490494
currentValue().setOffsetStart(token.start_ - begin_);
491495
currentValue().setOffsetLimit(token.end_ - begin_);
492496
return true;

src/lib_json/json_value.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ Value::~Value() {
406406
}
407407

408408
Value& Value::operator=(Value other) {
409-
swapPayload(other);
409+
swap(other);
410410
return *this;
411411
}
412412

0 commit comments

Comments
 (0)