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.
This commit is contained in:
Christopher Dunn 2015-01-20 12:49:51 -06:00
commit 4ce4bb8404
9 changed files with 57 additions and 24 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

@ -4,10 +4,10 @@
#ifndef JSON_VERSION_H_INCLUDED #ifndef JSON_VERSION_H_INCLUDED
# define JSON_VERSION_H_INCLUDED # define JSON_VERSION_H_INCLUDED
# define JSONCPP_VERSION_STRING "1.1.1" # define JSONCPP_VERSION_STRING "1.2.0"
# define JSONCPP_VERSION_MAJOR 1 # define JSONCPP_VERSION_MAJOR 1
# define JSONCPP_VERSION_MINOR 1 # define JSONCPP_VERSION_MINOR 2
# define JSONCPP_VERSION_PATCH 1 # define JSONCPP_VERSION_PATCH 0
# define JSONCPP_VERSION_QUALIFIER # define JSONCPP_VERSION_QUALIFIER
# define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8)) # define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8))

View File

@ -163,26 +163,36 @@ bool Reader::readValue() {
successful = decodeString(token); successful = decodeString(token);
break; break;
case tokenTrue: case tokenTrue:
currentValue() = true; {
Value v(true);
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 tokenFalse: case tokenFalse:
currentValue() = false; {
Value v(false);
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 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_) {
// "Un-read" the current token and mark the current value as a null // "Un-read" the current token and mark the current value as a null
// token. // token.
current_--; current_--;
currentValue() = Value(); Value v;
currentValue().swapPayload(v);
currentValue().setOffsetStart(current_ - begin_ - 1); currentValue().setOffsetStart(current_ - begin_ - 1);
currentValue().setOffsetLimit(current_ - begin_); currentValue().setOffsetLimit(current_ - begin_);
break; break;
@ -393,7 +403,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;
@ -446,7 +457,8 @@ bool Reader::readObject(Token& tokenStart) {
} }
bool Reader::readArray(Token& tokenStart) { bool Reader::readArray(Token& tokenStart) {
currentValue() = Value(arrayValue); Value init(arrayValue);
currentValue().swapPayload(init);
currentValue().setOffsetStart(tokenStart.start_ - begin_); currentValue().setOffsetStart(tokenStart.start_ - begin_);
skipSpaces(); skipSpaces();
if (*current_ == ']') // empty array if (*current_ == ']') // empty array
@ -486,7 +498,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;
@ -536,7 +548,7 @@ bool Reader::decodeDouble(Token& token) {
Value decoded; Value decoded;
if (!decodeDouble(token, decoded)) if (!decodeDouble(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;
@ -579,10 +591,11 @@ bool Reader::decodeDouble(Token& token, Value& decoded) {
} }
bool Reader::decodeString(Token& token) { bool Reader::decodeString(Token& token) {
std::string decoded; std::string decoded_string;
if (!decodeString(token, decoded)) if (!decodeString(token, decoded_string))
return false; return false;
currentValue() = decoded; Value decoded(decoded_string);
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;
} }

View File

@ -1,4 +1,5 @@
.={} .={}
// Comment for array
.test=[] .test=[]
.test[0]={} .test[0]={}
.test[0].a="aaa" .test[0].a="aaa"

View File

@ -1,5 +1,6 @@
{ {
"test": "test":
// Comment for array
[ [
{ "a" : "aaa" }, // Comment for a { "a" : "aaa" }, // Comment for a
{ "b" : "bbb" }, // Comment for b { "b" : "bbb" }, // Comment for b

View File

@ -11,4 +11,13 @@
// Multiline comment cpp-style // Multiline comment cpp-style
// Second line // Second line
.cpp-test.c=3 .cpp-test.c=3
.cpp-test.d=4 // Comment before double
.cpp-test.d=4.1
// Comment before string
.cpp-test.e="e-string"
// Comment before true
.cpp-test.f=true
// Comment before false
.cpp-test.g=false
// Comment before null
.cpp-test.h=null

View File

@ -12,6 +12,15 @@
// Multiline comment cpp-style // Multiline comment cpp-style
// Second line // Second line
"c" : 3, "c" : 3,
"d" : 4 // Comment before double
"d" : 4.1,
// Comment before string
"e" : "e-string",
// Comment before true
"f" : true,
// Comment before false
"g" : false,
// Comment before null
"h" : null
} }
} }

View File

@ -1 +1 @@
1.1.1 1.2.0