summaryrefslogtreecommitdiff
path: root/lib/jsoncpp/jsoncpp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/jsoncpp/jsoncpp.cpp')
-rw-r--r--lib/jsoncpp/jsoncpp.cpp163
1 files changed, 118 insertions, 45 deletions
diff --git a/lib/jsoncpp/jsoncpp.cpp b/lib/jsoncpp/jsoncpp.cpp
index c373405f0..a85e280ed 100644
--- a/lib/jsoncpp/jsoncpp.cpp
+++ b/lib/jsoncpp/jsoncpp.cpp
@@ -10,13 +10,13 @@ The JsonCpp library's source code, including accompanying documentation,
tests and demonstration applications, are licensed under the following
conditions...
-The author (Baptiste Lepilleur) explicitly disclaims copyright in all
+Baptiste Lepilleur and The JsonCpp Authors explicitly disclaim copyright in all
jurisdictions which recognize such a disclaimer. In such jurisdictions,
this software is released into the Public Domain.
In jurisdictions which do not recognize Public Domain property (e.g. Germany as of
-2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur, and is
-released under the terms of the MIT License (see below).
+2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur and
+The JsonCpp Authors, and is released under the terms of the MIT License (see below).
In jurisdictions which recognize Public Domain property, the user of this
software may choose to accept it either as 1) Public Domain, 2) under the
@@ -31,7 +31,7 @@ described in clear, concise terms at:
The full text of the MIT License follows:
========================================================================
-Copyright (c) 2007-2010 Baptiste Lepilleur
+Copyright (c) 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
@@ -84,7 +84,7 @@ license you like.
// Beginning of content of file: src/lib_json/json_tool.h
// //////////////////////////////////////////////////////////////////////
-// Copyright 2007-2010 Baptiste Lepilleur
+// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
@@ -215,7 +215,7 @@ static inline void fixNumericLocaleInput(char* begin, char* end) {
// Beginning of content of file: src/lib_json/json_reader.cpp
// //////////////////////////////////////////////////////////////////////
-// Copyright 2007-2011 Baptiste Lepilleur
+// Copyright 2007-2011 Baptiste Lepilleur and The JsonCpp Authors
// Copyright (C) 2016 InfoTeCS JSC. All rights reserved.
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
@@ -298,7 +298,7 @@ Features Features::strictMode() {
// Implementation of class Reader
// ////////////////////////////////
-static bool containsNewLine(Reader::Location begin, Reader::Location end) {
+bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) {
for (; begin < end; ++begin)
if (*begin == '\n' || *begin == '\r')
return true;
@@ -320,8 +320,7 @@ Reader::Reader(const Features& features)
bool
Reader::parse(const std::string& document, Value& root, bool collectComments) {
- JSONCPP_STRING documentCopy(document.data(), document.data() + document.capacity());
- std::swap(documentCopy, document_);
+ document_.assign(document.begin(), document.end());
const char* begin = document_.c_str();
const char* end = begin + document_.length();
return parse(begin, end, root, collectComments);
@@ -354,7 +353,7 @@ bool Reader::parse(const char* beginDoc,
current_ = begin_;
lastValueEnd_ = 0;
lastValue_ = 0;
- commentsBefore_ = "";
+ commentsBefore_.clear();
errors_.clear();
while (!nodes_.empty())
nodes_.pop();
@@ -393,7 +392,7 @@ bool Reader::readValue() {
if (collectComments_ && !commentsBefore_.empty()) {
currentValue().setComment(commentsBefore_, commentBefore);
- commentsBefore_ = "";
+ commentsBefore_.clear();
}
switch (token.type_) {
@@ -587,7 +586,7 @@ bool Reader::readComment() {
return true;
}
-static JSONCPP_STRING normalizeEOL(Reader::Location begin, Reader::Location end) {
+JSONCPP_STRING Reader::normalizeEOL(Reader::Location begin, Reader::Location end) {
JSONCPP_STRING normalized;
normalized.reserve(static_cast<size_t>(end - begin));
Reader::Location current = begin;
@@ -691,7 +690,7 @@ bool Reader::readObject(Token& tokenStart) {
break;
if (tokenName.type_ == tokenObjectEnd && name.empty()) // empty object
return true;
- name = "";
+ name.clear();
if (tokenName.type_ == tokenString) {
if (!decodeString(tokenName, name))
return recoverFromError(tokenObjectEnd);
@@ -1236,6 +1235,9 @@ private:
void addComment(Location begin, Location end, CommentPlacement placement);
void skipCommentTokens(Token& token);
+ static JSONCPP_STRING normalizeEOL(Location begin, Location end);
+ static bool containsNewLine(Location begin, Location end);
+
typedef std::stack<Value*> Nodes;
Nodes nodes_;
Errors errors_;
@@ -1253,6 +1255,13 @@ private:
// complete copy of Read impl, for OurReader
+bool OurReader::containsNewLine(OurReader::Location begin, OurReader::Location end) {
+ for (; begin < end; ++begin)
+ if (*begin == '\n' || *begin == '\r')
+ return true;
+ return false;
+}
+
OurReader::OurReader(OurFeatures const& features)
: errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),
lastValue_(), commentsBefore_(),
@@ -1273,7 +1282,7 @@ bool OurReader::parse(const char* beginDoc,
current_ = begin_;
lastValueEnd_ = 0;
lastValue_ = 0;
- commentsBefore_ = "";
+ commentsBefore_.clear();
errors_.clear();
while (!nodes_.empty())
nodes_.pop();
@@ -1315,7 +1324,7 @@ bool OurReader::readValue() {
if (collectComments_ && !commentsBefore_.empty()) {
currentValue().setComment(commentsBefore_, commentBefore);
- commentsBefore_ = "";
+ commentsBefore_.clear();
}
switch (token.type_) {
@@ -1562,6 +1571,25 @@ bool OurReader::readComment() {
return true;
}
+JSONCPP_STRING OurReader::normalizeEOL(OurReader::Location begin, OurReader::Location end) {
+ JSONCPP_STRING normalized;
+ normalized.reserve(static_cast<size_t>(end - begin));
+ OurReader::Location current = begin;
+ while (current != end) {
+ char c = *current++;
+ if (c == '\r') {
+ if (current != end && *current == '\n')
+ // convert dos EOL
+ ++current;
+ // convert Mac EOL
+ normalized += '\n';
+ } else {
+ normalized += c;
+ }
+ }
+ return normalized;
+}
+
void
OurReader::addComment(Location begin, Location end, CommentPlacement placement) {
assert(collectComments_);
@@ -1664,7 +1692,7 @@ bool OurReader::readObject(Token& tokenStart) {
break;
if (tokenName.type_ == tokenObjectEnd && name.empty()) // empty object
return true;
- name = "";
+ name.clear();
if (tokenName.type_ == tokenString) {
if (!decodeString(tokenName, name))
return recoverFromError(tokenObjectEnd);
@@ -2265,7 +2293,7 @@ JSONCPP_ISTREAM& operator>>(JSONCPP_ISTREAM& sin, Value& root) {
// Beginning of content of file: src/lib_json/json_valueiterator.inl
// //////////////////////////////////////////////////////////////////////
-// Copyright 2007-2010 Baptiste Lepilleur
+// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
@@ -2446,7 +2474,7 @@ ValueIterator& ValueIterator::operator=(const SelfType& other) {
// Beginning of content of file: src/lib_json/json_value.cpp
// //////////////////////////////////////////////////////////////////////
-// Copyright 2011 Baptiste Lepilleur
+// Copyright 2011 Baptiste Lepilleur and The JsonCpp Authors
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
@@ -2740,10 +2768,20 @@ void Value::CZString::swap(CZString& other) {
std::swap(index_, other.index_);
}
-Value::CZString& Value::CZString::operator=(CZString other) {
- swap(other);
+Value::CZString& Value::CZString::operator=(const CZString& other) {
+ cstr_ = other.cstr_;
+ index_ = other.index_;
+ return *this;
+}
+
+#if JSON_HAS_RVALUE_REFERENCES
+Value::CZString& Value::CZString::operator=(CZString&& other) {
+ cstr_ = other.cstr_;
+ index_ = other.index_;
+ other.cstr_ = nullptr;
return *this;
}
+#endif
bool Value::CZString::operator<(const CZString& other) const {
if (!cstr_) return index_ < other.index_;
@@ -2751,7 +2789,7 @@ bool Value::CZString::operator<(const CZString& other) const {
// Assume both are strings.
unsigned this_len = this->storage_.length_;
unsigned other_len = other.storage_.length_;
- unsigned min_len = std::min(this_len, other_len);
+ unsigned min_len = std::min<unsigned>(this_len, other_len);
JSON_ASSERT(this->cstr_ && other.cstr_);
int comp = memcmp(this->cstr_, other.cstr_, min_len);
if (comp < 0) return true;
@@ -2846,6 +2884,7 @@ Value::Value(double value) {
Value::Value(const char* value) {
initBasic(stringValue, true);
+ JSON_ASSERT_MESSAGE(value != NULL, "Null Value Passed to Value Constructor");
value_.string_ = duplicateAndPrefixStringValue(value, static_cast<unsigned>(strlen(value)));
}
@@ -2970,6 +3009,12 @@ void Value::swapPayload(Value& other) {
other.allocated_ = temp2 & 0x1;
}
+void Value::copyPayload(const Value& other) {
+ type_ = other.type_;
+ value_ = other.value_;
+ allocated_ = other.allocated_;
+}
+
void Value::swap(Value& other) {
swapPayload(other);
std::swap(comments_, other.comments_);
@@ -2977,6 +3022,13 @@ void Value::swap(Value& other) {
std::swap(limit_, other.limit_);
}
+void Value::copy(const Value& other) {
+ copyPayload(other);
+ comments_ = other.comments_;
+ start_ = other.start_;
+ limit_ = other.limit_;
+}
+
ValueType Value::type() const { return type_; }
int Value::compare(const Value& other) const {
@@ -3014,7 +3066,7 @@ bool Value::operator<(const Value& other) const {
char const* other_str;
decodePrefixedString(this->allocated_, this->value_.string_, &this_len, &this_str);
decodePrefixedString(other.allocated_, other.value_.string_, &other_len, &other_str);
- unsigned min_len = std::min(this_len, other_len);
+ unsigned min_len = std::min<unsigned>(this_len, other_len);
JSON_ASSERT(this_str && other_str);
int comp = memcmp(this_str, other_str, min_len);
if (comp < 0) return true;
@@ -3327,7 +3379,7 @@ bool Value::isConvertibleTo(ValueType other) const {
case nullValue:
return (isNumeric() && asDouble() == 0.0) ||
(type_ == booleanValue && value_.bool_ == false) ||
- (type_ == stringValue && asString() == "") ||
+ (type_ == stringValue && asString().empty()) ||
(type_ == arrayValue && value_.map_->size() == 0) ||
(type_ == objectValue && value_.map_->size() == 0) ||
type_ == nullValue;
@@ -3571,6 +3623,10 @@ Value const& Value::operator[](CppTL::ConstString const& key) const
Value& Value::append(const Value& value) { return (*this)[size()] = value; }
+#if JSON_HAS_RVALUE_REFERENCES
+ Value& Value::append(Value&& value) { return (*this)[size()] = value; }
+#endif
+
Value Value::get(char const* key, char const* cend, Value const& defaultValue) const
{
Value const* found = find(key, cend);
@@ -3807,11 +3863,23 @@ bool Value::isUInt64() const {
}
bool Value::isIntegral() const {
+ switch (type_) {
+ case intValue:
+ case uintValue:
+ return true;
+ case realValue:
#if defined(JSON_HAS_INT64)
- return isInt64() || isUInt64();
+ // Note that maxUInt64 (= 2^64 - 1) is not exactly representable as a
+ // double, so double(maxUInt64) will be rounded up to 2^64. Therefore we
+ // require the value to be strictly less than the limit.
+ return value_.real_ >= double(minInt64) && value_.real_ < maxUInt64AsDouble && IsIntegral(value_.real_);
#else
- return isInt() || isUInt();
-#endif
+ return value_.real_ >= minInt && value_.real_ <= maxUInt && IsIntegral(value_.real_);
+#endif // JSON_HAS_INT64
+ default:
+ break;
+ }
+ return false;
}
bool Value::isDouble() const { return type_ == intValue || type_ == uintValue || type_ == realValue; }
@@ -3861,8 +3929,13 @@ ptrdiff_t Value::getOffsetStart() const { return start_; }
ptrdiff_t Value::getOffsetLimit() const { return limit_; }
JSONCPP_STRING Value::toStyledString() const {
- StyledWriter writer;
- return writer.write(*this);
+ StreamWriterBuilder builder;
+
+ JSONCPP_STRING out = this->hasComment(commentBefore) ? "\n" : "";
+ out += Json::writeString(builder, *this);
+ out += "\n";
+
+ return out;
}
Value::const_iterator Value::begin() const {
@@ -3941,6 +4014,7 @@ Path::Path(const JSONCPP_STRING& path,
const PathArgument& a4,
const PathArgument& a5) {
InArgs in;
+ in.reserve(5);
in.push_back(&a1);
in.push_back(&a2);
in.push_back(&a3);
@@ -4076,7 +4150,7 @@ Value& Path::make(Value& root) const {
// Beginning of content of file: src/lib_json/json_writer.cpp
// //////////////////////////////////////////////////////////////////////
-// Copyright 2011 Baptiste Lepilleur
+// Copyright 2011 Baptiste Lepilleur and The JsonCpp Authors
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
@@ -4220,17 +4294,18 @@ JSONCPP_STRING valueToString(double value, bool useSpecialFloats, unsigned int p
char buffer[36];
int len = -1;
- char formatString[6];
- sprintf(formatString, "%%.%dg", precision);
+ char formatString[15];
+ snprintf(formatString, sizeof(formatString), "%%.%dg", precision);
// Print into the buffer. We need not request the alternative representation
// that always has a decimal point because JSON doesn't distingish the
// concepts of reals and integers.
if (isfinite(value)) {
len = snprintf(buffer, sizeof(buffer), formatString, value);
-
+ fixNumericLocale(buffer, buffer + len);
+
// try to ensure we preserve the fact that this was given to us as a double on input
- if (!strstr(buffer, ".") && !strstr(buffer, "e")) {
+ if (!strchr(buffer, '.') && !strchr(buffer, 'e')) {
strcat(buffer, ".0");
}
@@ -4243,10 +4318,8 @@ JSONCPP_STRING valueToString(double value, bool useSpecialFloats, unsigned int p
} else {
len = snprintf(buffer, sizeof(buffer), useSpecialFloats ? "Infinity" : "1e+9999");
}
- // For those, we do not need to call fixNumLoc, but it is fast.
}
assert(len >= 0);
- fixNumericLocale(buffer, buffer + len);
return buffer;
}
}
@@ -4413,7 +4486,7 @@ void FastWriter::dropNullPlaceholders() { dropNullPlaceholders_ = true; }
void FastWriter::omitEndingLineFeed() { omitEndingLineFeed_ = true; }
JSONCPP_STRING FastWriter::write(const Value& root) {
- document_ = "";
+ document_.clear();
writeValue(root);
if (!omitEndingLineFeed_)
document_ += "\n";
@@ -4481,9 +4554,9 @@ StyledWriter::StyledWriter()
: rightMargin_(74), indentSize_(3), addChildValues_() {}
JSONCPP_STRING StyledWriter::write(const Value& root) {
- document_ = "";
+ document_.clear();
addChildValues_ = false;
- indentString_ = "";
+ indentString_.clear();
writeCommentBeforeValue(root);
writeValue(root);
writeCommentAfterValueOnSameLine(root);
@@ -4661,7 +4734,7 @@ void StyledWriter::writeCommentBeforeValue(const Value& root) {
while (iter != comment.end()) {
document_ += *iter;
if (*iter == '\n' &&
- (iter != comment.end() && *(iter + 1) == '/'))
+ ((iter+1) != comment.end() && *(iter + 1) == '/'))
writeIndent();
++iter;
}
@@ -4697,7 +4770,7 @@ StyledStreamWriter::StyledStreamWriter(JSONCPP_STRING indentation)
void StyledStreamWriter::write(JSONCPP_OSTREAM& out, const Value& root) {
document_ = &out;
addChildValues_ = false;
- indentString_ = "";
+ indentString_.clear();
indented_ = true;
writeCommentBeforeValue(root);
if (!indented_) writeIndent();
@@ -4877,7 +4950,7 @@ void StyledStreamWriter::writeCommentBeforeValue(const Value& root) {
while (iter != comment.end()) {
*document_ << *iter;
if (*iter == '\n' &&
- (iter != comment.end() && *(iter + 1) == '/'))
+ ((iter+1) != comment.end() && *(iter + 1) == '/'))
// writeIndent(); // would include newline
*document_ << indentString_;
++iter;
@@ -4979,7 +5052,7 @@ int BuiltStyledStreamWriter::write(Value const& root, JSONCPP_OSTREAM* sout)
sout_ = sout;
addChildValues_ = false;
indented_ = true;
- indentString_ = "";
+ indentString_.clear();
writeCommentBeforeValue(root);
if (!indented_) writeIndent();
indented_ = true;
@@ -5165,7 +5238,7 @@ void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
while (iter != comment.end()) {
*sout_ << *iter;
if (*iter == '\n' &&
- (iter != comment.end() && *(iter + 1) == '/'))
+ ((iter+1) != comment.end() && *(iter + 1) == '/'))
// writeIndent(); // would write extra newline
*sout_ << indentString_;
++iter;
@@ -5233,10 +5306,10 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
}
JSONCPP_STRING nullSymbol = "null";
if (dnp) {
- nullSymbol = "";
+ nullSymbol.clear();
}
if (pre > 17) pre = 17;
- JSONCPP_STRING endingLineFeedSymbol = "";
+ JSONCPP_STRING endingLineFeedSymbol;
return new BuiltStyledStreamWriter(
indentation, cs,
colonSymbol, nullSymbol, endingLineFeedSymbol, usf, pre);