Skip to content

Commit 4ce4bb8

Browse files
committed
Merge pull request #124 from cdunn2001/assign-with-comments
1.2.0 `operator=()` (which already performed a deep-copy) now includes comments. This change is probably harmless in all practical cases. But just in case, we bump the minor version. Address #47.
2 parents 94b0297 + 2cd0f4e commit 4ce4bb8

9 files changed

+57
-24
lines changed

Diff for: include/json/value.h

+6-6
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

Diff for: include/json/version.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#ifndef JSON_VERSION_H_INCLUDED
55
# define JSON_VERSION_H_INCLUDED
66

7-
# define JSONCPP_VERSION_STRING "1.1.1"
7+
# define JSONCPP_VERSION_STRING "1.2.0"
88
# define JSONCPP_VERSION_MAJOR 1
9-
# define JSONCPP_VERSION_MINOR 1
10-
# define JSONCPP_VERSION_PATCH 1
9+
# define JSONCPP_VERSION_MINOR 2
10+
# define JSONCPP_VERSION_PATCH 0
1111
# define JSONCPP_VERSION_QUALIFIER
1212
# define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8))
1313

Diff for: src/lib_json/json_reader.cpp

+24-11
Original file line numberDiff line numberDiff line change
@@ -163,26 +163,36 @@ bool Reader::readValue() {
163163
successful = decodeString(token);
164164
break;
165165
case tokenTrue:
166-
currentValue() = true;
166+
{
167+
Value v(true);
168+
currentValue().swapPayload(v);
167169
currentValue().setOffsetStart(token.start_ - begin_);
168170
currentValue().setOffsetLimit(token.end_ - begin_);
171+
}
169172
break;
170173
case tokenFalse:
171-
currentValue() = false;
174+
{
175+
Value v(false);
176+
currentValue().swapPayload(v);
172177
currentValue().setOffsetStart(token.start_ - begin_);
173178
currentValue().setOffsetLimit(token.end_ - begin_);
179+
}
174180
break;
175181
case tokenNull:
176-
currentValue() = Value();
182+
{
183+
Value v;
184+
currentValue().swapPayload(v);
177185
currentValue().setOffsetStart(token.start_ - begin_);
178186
currentValue().setOffsetLimit(token.end_ - begin_);
187+
}
179188
break;
180189
case tokenArraySeparator:
181190
if (features_.allowDroppedNullPlaceholders_) {
182191
// "Un-read" the current token and mark the current value as a null
183192
// token.
184193
current_--;
185-
currentValue() = Value();
194+
Value v;
195+
currentValue().swapPayload(v);
186196
currentValue().setOffsetStart(current_ - begin_ - 1);
187197
currentValue().setOffsetLimit(current_ - begin_);
188198
break;
@@ -393,7 +403,8 @@ bool Reader::readString() {
393403
bool Reader::readObject(Token& tokenStart) {
394404
Token tokenName;
395405
std::string name;
396-
currentValue() = Value(objectValue);
406+
Value init(objectValue);
407+
currentValue().swapPayload(init);
397408
currentValue().setOffsetStart(tokenStart.start_ - begin_);
398409
while (readToken(tokenName)) {
399410
bool initialTokenOk = true;
@@ -446,7 +457,8 @@ bool Reader::readObject(Token& tokenStart) {
446457
}
447458

448459
bool Reader::readArray(Token& tokenStart) {
449-
currentValue() = Value(arrayValue);
460+
Value init(arrayValue);
461+
currentValue().swapPayload(init);
450462
currentValue().setOffsetStart(tokenStart.start_ - begin_);
451463
skipSpaces();
452464
if (*current_ == ']') // empty array
@@ -486,7 +498,7 @@ bool Reader::decodeNumber(Token& token) {
486498
Value decoded;
487499
if (!decodeNumber(token, decoded))
488500
return false;
489-
currentValue() = decoded;
501+
currentValue().swapPayload(decoded);
490502
currentValue().setOffsetStart(token.start_ - begin_);
491503
currentValue().setOffsetLimit(token.end_ - begin_);
492504
return true;
@@ -536,7 +548,7 @@ bool Reader::decodeDouble(Token& token) {
536548
Value decoded;
537549
if (!decodeDouble(token, decoded))
538550
return false;
539-
currentValue() = decoded;
551+
currentValue().swapPayload(decoded);
540552
currentValue().setOffsetStart(token.start_ - begin_);
541553
currentValue().setOffsetLimit(token.end_ - begin_);
542554
return true;
@@ -579,10 +591,11 @@ bool Reader::decodeDouble(Token& token, Value& decoded) {
579591
}
580592

581593
bool Reader::decodeString(Token& token) {
582-
std::string decoded;
583-
if (!decodeString(token, decoded))
594+
std::string decoded_string;
595+
if (!decodeString(token, decoded_string))
584596
return false;
585-
currentValue() = decoded;
597+
Value decoded(decoded_string);
598+
currentValue().swapPayload(decoded);
586599
currentValue().setOffsetStart(token.start_ - begin_);
587600
currentValue().setOffsetLimit(token.end_ - begin_);
588601
return true;

Diff for: src/lib_json/json_value.cpp

+1-1
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

Diff for: test/data/test_comment_01.expected

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.={}
2+
// Comment for array
23
.test=[]
34
.test[0]={}
45
.test[0].a="aaa"

Diff for: test/data/test_comment_01.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"test":
3+
// Comment for array
34
[
45
{ "a" : "aaa" }, // Comment for a
56
{ "b" : "bbb" }, // Comment for b

Diff for: test/data/test_comment_02.expected

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,13 @@
1111
// Multiline comment cpp-style
1212
// Second line
1313
.cpp-test.c=3
14-
.cpp-test.d=4
14+
// Comment before double
15+
.cpp-test.d=4.1
16+
// Comment before string
17+
.cpp-test.e="e-string"
18+
// Comment before true
19+
.cpp-test.f=true
20+
// Comment before false
21+
.cpp-test.g=false
22+
// Comment before null
23+
.cpp-test.h=null

Diff for: test/data/test_comment_02.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
// Multiline comment cpp-style
1313
// Second line
1414
"c" : 3,
15-
"d" : 4
15+
// Comment before double
16+
"d" : 4.1,
17+
// Comment before string
18+
"e" : "e-string",
19+
// Comment before true
20+
"f" : true,
21+
// Comment before false
22+
"g" : false,
23+
// Comment before null
24+
"h" : null
1625
}
1726
}

Diff for: version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.1
1+
1.2.0

0 commit comments

Comments
 (0)