mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-10-24 03:53:18 +08:00
Apply the formatting specified in .clang-format file.
$ clang-format --version clang-format version 7.0.0 (tags/google/stable/2018-01-11) $ clang-format -i --style=file $(find . -name '*.cpp' -o -name '*.h')
This commit is contained in:
@@ -4,76 +4,77 @@
|
||||
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
|
||||
|
||||
#if !defined(JSON_IS_AMALGAMATION)
|
||||
#include <json/writer.h>
|
||||
#include "json_tool.h"
|
||||
#include <json/writer.h>
|
||||
#endif // if !defined(JSON_IS_AMALGAMATION)
|
||||
#include <iomanip>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
#include <set>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
|
||||
#if __cplusplus >= 201103L
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
|
||||
#if !defined(isnan)
|
||||
#define isnan std::isnan
|
||||
#endif
|
||||
#if !defined(isnan)
|
||||
#define isnan std::isnan
|
||||
#endif
|
||||
|
||||
#if !defined(isfinite)
|
||||
#define isfinite std::isfinite
|
||||
#endif
|
||||
#if !defined(isfinite)
|
||||
#define isfinite std::isfinite
|
||||
#endif
|
||||
|
||||
#if !defined(snprintf)
|
||||
#define snprintf std::snprintf
|
||||
#endif
|
||||
#if !defined(snprintf)
|
||||
#define snprintf std::snprintf
|
||||
#endif
|
||||
#else
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#if !defined(isnan)
|
||||
#include <float.h>
|
||||
#define isnan _isnan
|
||||
#endif
|
||||
#if defined(_MSC_VER)
|
||||
#if !defined(isnan)
|
||||
#include <float.h>
|
||||
#define isnan _isnan
|
||||
#endif
|
||||
|
||||
#if !defined(isfinite)
|
||||
#include <float.h>
|
||||
#define isfinite _finite
|
||||
#endif
|
||||
#if !defined(isfinite)
|
||||
#include <float.h>
|
||||
#define isfinite _finite
|
||||
#endif
|
||||
|
||||
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
|
||||
#if !defined(snprintf)
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
#endif
|
||||
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
|
||||
#if !defined(snprintf)
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__sun) && defined(__SVR4) //Solaris
|
||||
#if !defined(isfinite)
|
||||
#include <ieeefp.h>
|
||||
#define isfinite finite
|
||||
#endif
|
||||
#endif
|
||||
#if defined(__sun) && defined(__SVR4) // Solaris
|
||||
#if !defined(isfinite)
|
||||
#include <ieeefp.h>
|
||||
#define isfinite finite
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__hpux)
|
||||
#if !defined(isfinite)
|
||||
#if defined(__ia64) && !defined(finite)
|
||||
#define isfinite(x) ((sizeof(x) == sizeof(float) ? _Isfinitef(x) : _IsFinite(x)))
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#if defined(__hpux)
|
||||
#if !defined(isfinite)
|
||||
#if defined(__ia64) && !defined(finite)
|
||||
#define isfinite(x) \
|
||||
((sizeof(x) == sizeof(float) ? _Isfinitef(x) : _IsFinite(x)))
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if !defined(isnan)
|
||||
// IEEE standard states that NaN values will not compare to themselves
|
||||
#define isnan(x) (x!=x)
|
||||
#endif
|
||||
#if !defined(isnan)
|
||||
// IEEE standard states that NaN values will not compare to themselves
|
||||
#define isnan(x) (x != x)
|
||||
#endif
|
||||
|
||||
#if !defined(isfinite)
|
||||
#define isfinite finite
|
||||
#endif
|
||||
#if !defined(isfinite)
|
||||
#define isfinite finite
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1400 // VC++ 8.0
|
||||
@@ -86,7 +87,7 @@ namespace Json {
|
||||
#if __cplusplus >= 201103L || (defined(_CPPLIB_VER) && _CPPLIB_VER >= 520)
|
||||
typedef std::unique_ptr<StreamWriter> StreamWriterPtr;
|
||||
#else
|
||||
typedef std::auto_ptr<StreamWriter> StreamWriterPtr;
|
||||
typedef std::auto_ptr<StreamWriter> StreamWriterPtr;
|
||||
#endif
|
||||
|
||||
JSONCPP_STRING valueToString(LargestInt value) {
|
||||
@@ -126,30 +127,34 @@ JSONCPP_STRING valueToString(UInt value) {
|
||||
#endif // # if defined(JSON_HAS_INT64)
|
||||
|
||||
namespace {
|
||||
JSONCPP_STRING valueToString(double value, bool useSpecialFloats, unsigned int precision, PrecisionType precisionType) {
|
||||
JSONCPP_STRING valueToString(double value,
|
||||
bool useSpecialFloats,
|
||||
unsigned int precision,
|
||||
PrecisionType precisionType) {
|
||||
// Print into the buffer. We need not request the alternative representation
|
||||
// that always has a decimal point because JSON doesn't distinguish the
|
||||
// concepts of reals and integers.
|
||||
if (!isfinite(value)) {
|
||||
static const char* const reps[2][3] = {
|
||||
{"NaN", "-Infinity", "Infinity"},
|
||||
{"null", "-1e+9999", "1e+9999"}};
|
||||
return reps[useSpecialFloats ? 0 : 1][isnan(value) ? 0 : (value < 0) ? 1 : 2];
|
||||
static const char* const reps[2][3] = { { "NaN", "-Infinity", "Infinity" },
|
||||
{ "null", "-1e+9999", "1e+9999" } };
|
||||
return reps[useSpecialFloats ? 0 : 1]
|
||||
[isnan(value) ? 0 : (value < 0) ? 1 : 2];
|
||||
}
|
||||
|
||||
JSONCPP_STRING buffer(size_t(36), '\0');
|
||||
while (true) {
|
||||
int len = snprintf(&*buffer.begin(), buffer.size(),
|
||||
(precisionType == PrecisionType::significantDigits) ? "%.*g" : "%.*f",
|
||||
precision, value);
|
||||
assert(len >= 0);
|
||||
size_t wouldPrint = static_cast<size_t>(len);
|
||||
if (wouldPrint >= buffer.size()) {
|
||||
buffer.resize(wouldPrint + 1);
|
||||
continue;
|
||||
}
|
||||
buffer.resize(wouldPrint);
|
||||
break;
|
||||
int len = snprintf(
|
||||
&*buffer.begin(), buffer.size(),
|
||||
(precisionType == PrecisionType::significantDigits) ? "%.*g" : "%.*f",
|
||||
precision, value);
|
||||
assert(len >= 0);
|
||||
size_t wouldPrint = static_cast<size_t>(len);
|
||||
if (wouldPrint >= buffer.size()) {
|
||||
buffer.resize(wouldPrint + 1);
|
||||
continue;
|
||||
}
|
||||
buffer.resize(wouldPrint);
|
||||
break;
|
||||
}
|
||||
|
||||
buffer.erase(fixNumericLocale(buffer.begin(), buffer.end()), buffer.end());
|
||||
@@ -159,15 +164,18 @@ JSONCPP_STRING valueToString(double value, bool useSpecialFloats, unsigned int p
|
||||
buffer.erase(fixZerosInTheEnd(buffer.begin(), buffer.end()), buffer.end());
|
||||
}
|
||||
|
||||
// try to ensure we preserve the fact that this was given to us as a double on input
|
||||
// try to ensure we preserve the fact that this was given to us as a double on
|
||||
// input
|
||||
if (buffer.find('.') == buffer.npos && buffer.find('e') == buffer.npos) {
|
||||
buffer += ".0";
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
JSONCPP_STRING valueToString(double value, unsigned int precision, PrecisionType precisionType) {
|
||||
JSONCPP_STRING valueToString(double value,
|
||||
unsigned int precision,
|
||||
PrecisionType precisionType) {
|
||||
return valueToString(value, false, precision, precisionType);
|
||||
}
|
||||
|
||||
@@ -178,8 +186,8 @@ static bool isAnyCharRequiredQuoting(char const* s, size_t n) {
|
||||
|
||||
char const* const end = s + n;
|
||||
for (char const* cur = s; cur < end; ++cur) {
|
||||
if (*cur == '\\' || *cur == '\"' || *cur < ' '
|
||||
|| static_cast<unsigned char>(*cur) < 0x80)
|
||||
if (*cur == '\\' || *cur == '\"' || *cur < ' ' ||
|
||||
static_cast<unsigned char>(*cur) < 0x80)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -197,8 +205,8 @@ static unsigned int utf8ToCodepoint(const char*& s, const char* e) {
|
||||
if (e - s < 2)
|
||||
return REPLACEMENT_CHARACTER;
|
||||
|
||||
unsigned int calculated = ((firstByte & 0x1F) << 6)
|
||||
| (static_cast<unsigned int>(s[1]) & 0x3F);
|
||||
unsigned int calculated =
|
||||
((firstByte & 0x1F) << 6) | (static_cast<unsigned int>(s[1]) & 0x3F);
|
||||
s += 1;
|
||||
// oversized encoded characters are invalid
|
||||
return calculated < 0x80 ? REPLACEMENT_CHARACTER : calculated;
|
||||
@@ -208,9 +216,9 @@ static unsigned int utf8ToCodepoint(const char*& s, const char* e) {
|
||||
if (e - s < 3)
|
||||
return REPLACEMENT_CHARACTER;
|
||||
|
||||
unsigned int calculated = ((firstByte & 0x0F) << 12)
|
||||
| ((static_cast<unsigned int>(s[1]) & 0x3F) << 6)
|
||||
| (static_cast<unsigned int>(s[2]) & 0x3F);
|
||||
unsigned int calculated = ((firstByte & 0x0F) << 12) |
|
||||
((static_cast<unsigned int>(s[1]) & 0x3F) << 6) |
|
||||
(static_cast<unsigned int>(s[2]) & 0x3F);
|
||||
s += 2;
|
||||
// surrogates aren't valid codepoints itself
|
||||
// shouldn't be UTF-8 encoded
|
||||
@@ -224,10 +232,10 @@ static unsigned int utf8ToCodepoint(const char*& s, const char* e) {
|
||||
if (e - s < 4)
|
||||
return REPLACEMENT_CHARACTER;
|
||||
|
||||
unsigned int calculated = ((firstByte & 0x07) << 18)
|
||||
| ((static_cast<unsigned int>(s[1]) & 0x3F) << 12)
|
||||
| ((static_cast<unsigned int>(s[2]) & 0x3F) << 6)
|
||||
| (static_cast<unsigned int>(s[3]) & 0x3F);
|
||||
unsigned int calculated = ((firstByte & 0x07) << 18) |
|
||||
((static_cast<unsigned int>(s[1]) & 0x3F) << 12) |
|
||||
((static_cast<unsigned int>(s[2]) & 0x3F) << 6) |
|
||||
(static_cast<unsigned int>(s[3]) & 0x3F);
|
||||
s += 3;
|
||||
// oversized encoded characters are invalid
|
||||
return calculated < 0x10000 ? REPLACEMENT_CHARACTER : calculated;
|
||||
@@ -236,23 +244,22 @@ static unsigned int utf8ToCodepoint(const char*& s, const char* e) {
|
||||
return REPLACEMENT_CHARACTER;
|
||||
}
|
||||
|
||||
static const char hex2[] =
|
||||
"000102030405060708090a0b0c0d0e0f"
|
||||
"101112131415161718191a1b1c1d1e1f"
|
||||
"202122232425262728292a2b2c2d2e2f"
|
||||
"303132333435363738393a3b3c3d3e3f"
|
||||
"404142434445464748494a4b4c4d4e4f"
|
||||
"505152535455565758595a5b5c5d5e5f"
|
||||
"606162636465666768696a6b6c6d6e6f"
|
||||
"707172737475767778797a7b7c7d7e7f"
|
||||
"808182838485868788898a8b8c8d8e8f"
|
||||
"909192939495969798999a9b9c9d9e9f"
|
||||
"a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
|
||||
"b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
|
||||
"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
|
||||
"d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
|
||||
"e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
|
||||
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
|
||||
static const char hex2[] = "000102030405060708090a0b0c0d0e0f"
|
||||
"101112131415161718191a1b1c1d1e1f"
|
||||
"202122232425262728292a2b2c2d2e2f"
|
||||
"303132333435363738393a3b3c3d3e3f"
|
||||
"404142434445464748494a4b4c4d4e4f"
|
||||
"505152535455565758595a5b5c5d5e5f"
|
||||
"606162636465666768696a6b6c6d6e6f"
|
||||
"707172737475767778797a7b7c7d7e7f"
|
||||
"808182838485868788898a8b8c8d8e8f"
|
||||
"909192939495969798999a9b9c9d9e9f"
|
||||
"a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
|
||||
"b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
|
||||
"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
|
||||
"d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
|
||||
"e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
|
||||
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
|
||||
|
||||
static JSONCPP_STRING toHex16Bit(unsigned int x) {
|
||||
const unsigned int hi = (x >> 8) & 0xff;
|
||||
@@ -274,8 +281,7 @@ static JSONCPP_STRING valueToQuotedStringN(const char* value, unsigned length) {
|
||||
// We have to walk value and escape any special characters.
|
||||
// Appending to JSONCPP_STRING is not efficient, but this should be rare.
|
||||
// (Note: forward slashes are *not* rare, but I am not escaping them.)
|
||||
JSONCPP_STRING::size_type maxsize =
|
||||
length * 2 + 3; // allescaped+quotes+NULL
|
||||
JSONCPP_STRING::size_type maxsize = length * 2 + 3; // allescaped+quotes+NULL
|
||||
JSONCPP_STRING result;
|
||||
result.reserve(maxsize); // to avoid lots of mallocs
|
||||
result += "\"";
|
||||
@@ -312,25 +318,23 @@ static JSONCPP_STRING valueToQuotedStringN(const char* value, unsigned length) {
|
||||
// Should add a flag to allow this compatibility mode and prevent this
|
||||
// sequence from occurring.
|
||||
default: {
|
||||
unsigned int cp = utf8ToCodepoint(c, end);
|
||||
// don't escape non-control characters
|
||||
// (short escape sequence are applied above)
|
||||
if (cp < 0x80 && cp >= 0x20)
|
||||
result += static_cast<char>(cp);
|
||||
else if (cp < 0x10000) { // codepoint is in Basic Multilingual Plane
|
||||
result += "\\u";
|
||||
result += toHex16Bit(cp);
|
||||
}
|
||||
else { // codepoint is not in Basic Multilingual Plane
|
||||
unsigned int cp = utf8ToCodepoint(c, end);
|
||||
// don't escape non-control characters
|
||||
// (short escape sequence are applied above)
|
||||
if (cp < 0x80 && cp >= 0x20)
|
||||
result += static_cast<char>(cp);
|
||||
else if (cp < 0x10000) { // codepoint is in Basic Multilingual Plane
|
||||
result += "\\u";
|
||||
result += toHex16Bit(cp);
|
||||
} else { // codepoint is not in Basic Multilingual Plane
|
||||
// convert to surrogate pair first
|
||||
cp -= 0x10000;
|
||||
result += "\\u";
|
||||
result += toHex16Bit((cp >> 10) + 0xD800);
|
||||
result += "\\u";
|
||||
result += toHex16Bit((cp & 0x3FF) + 0xDC00);
|
||||
}
|
||||
cp -= 0x10000;
|
||||
result += "\\u";
|
||||
result += toHex16Bit((cp >> 10) + 0xD800);
|
||||
result += "\\u";
|
||||
result += toHex16Bit((cp & 0x3FF) + 0xDC00);
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
}
|
||||
}
|
||||
result += "\"";
|
||||
@@ -381,13 +385,13 @@ void FastWriter::writeValue(const Value& value) {
|
||||
case realValue:
|
||||
document_ += valueToString(value.asDouble());
|
||||
break;
|
||||
case stringValue:
|
||||
{
|
||||
case stringValue: {
|
||||
// Is NULL possible for value.string_? No.
|
||||
char const* str;
|
||||
char const* end;
|
||||
bool ok = value.getString(&str, &end);
|
||||
if (ok) document_ += valueToQuotedStringN(str, static_cast<unsigned>(end-str));
|
||||
if (ok)
|
||||
document_ += valueToQuotedStringN(str, static_cast<unsigned>(end - str));
|
||||
break;
|
||||
}
|
||||
case booleanValue:
|
||||
@@ -411,7 +415,8 @@ void FastWriter::writeValue(const Value& value) {
|
||||
const JSONCPP_STRING& name = *it;
|
||||
if (it != members.begin())
|
||||
document_ += ',';
|
||||
document_ += valueToQuotedStringN(name.data(), static_cast<unsigned>(name.length()));
|
||||
document_ += valueToQuotedStringN(name.data(),
|
||||
static_cast<unsigned>(name.length()));
|
||||
document_ += yamlCompatibilityEnabled_ ? ": " : ":";
|
||||
writeValue(value[name]);
|
||||
}
|
||||
@@ -451,14 +456,15 @@ void StyledWriter::writeValue(const Value& value) {
|
||||
case realValue:
|
||||
pushValue(valueToString(value.asDouble()));
|
||||
break;
|
||||
case stringValue:
|
||||
{
|
||||
case stringValue: {
|
||||
// Is NULL possible for value.string_? No.
|
||||
char const* str;
|
||||
char const* end;
|
||||
bool ok = value.getString(&str, &end);
|
||||
if (ok) pushValue(valueToQuotedStringN(str, static_cast<unsigned>(end-str)));
|
||||
else pushValue("");
|
||||
if (ok)
|
||||
pushValue(valueToQuotedStringN(str, static_cast<unsigned>(end - str)));
|
||||
else
|
||||
pushValue("");
|
||||
break;
|
||||
}
|
||||
case booleanValue:
|
||||
@@ -546,7 +552,7 @@ bool StyledWriter::isMultilineArray(const Value& value) {
|
||||
for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
|
||||
const Value& childValue = value[index];
|
||||
isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
|
||||
childValue.size() > 0);
|
||||
childValue.size() > 0);
|
||||
}
|
||||
if (!isMultiLine) // check if line length > max line length
|
||||
{
|
||||
@@ -589,7 +595,9 @@ void StyledWriter::writeWithIndent(const JSONCPP_STRING& value) {
|
||||
document_ += value;
|
||||
}
|
||||
|
||||
void StyledWriter::indent() { indentString_ += JSONCPP_STRING(indentSize_, ' '); }
|
||||
void StyledWriter::indent() {
|
||||
indentString_ += JSONCPP_STRING(indentSize_, ' ');
|
||||
}
|
||||
|
||||
void StyledWriter::unindent() {
|
||||
assert(indentString_.size() >= indentSize_);
|
||||
@@ -606,8 +614,7 @@ void StyledWriter::writeCommentBeforeValue(const Value& root) {
|
||||
JSONCPP_STRING::const_iterator iter = comment.begin();
|
||||
while (iter != comment.end()) {
|
||||
document_ += *iter;
|
||||
if (*iter == '\n' &&
|
||||
((iter+1) != comment.end() && *(iter + 1) == '/'))
|
||||
if (*iter == '\n' && ((iter + 1) != comment.end() && *(iter + 1) == '/'))
|
||||
writeIndent();
|
||||
++iter;
|
||||
}
|
||||
@@ -638,8 +645,7 @@ bool StyledWriter::hasCommentForValue(const Value& value) {
|
||||
|
||||
StyledStreamWriter::StyledStreamWriter(JSONCPP_STRING indentation)
|
||||
: document_(NULL), rightMargin_(74), indentation_(indentation),
|
||||
addChildValues_(), indented_(false)
|
||||
{}
|
||||
addChildValues_(), indented_(false) {}
|
||||
|
||||
void StyledStreamWriter::write(JSONCPP_OSTREAM& out, const Value& root) {
|
||||
document_ = &out;
|
||||
@@ -647,7 +653,8 @@ void StyledStreamWriter::write(JSONCPP_OSTREAM& out, const Value& root) {
|
||||
indentString_.clear();
|
||||
indented_ = true;
|
||||
writeCommentBeforeValue(root);
|
||||
if (!indented_) writeIndent();
|
||||
if (!indented_)
|
||||
writeIndent();
|
||||
indented_ = true;
|
||||
writeValue(root);
|
||||
writeCommentAfterValueOnSameLine(root);
|
||||
@@ -669,14 +676,15 @@ void StyledStreamWriter::writeValue(const Value& value) {
|
||||
case realValue:
|
||||
pushValue(valueToString(value.asDouble()));
|
||||
break;
|
||||
case stringValue:
|
||||
{
|
||||
case stringValue: {
|
||||
// Is NULL possible for value.string_? No.
|
||||
char const* str;
|
||||
char const* end;
|
||||
bool ok = value.getString(&str, &end);
|
||||
if (ok) pushValue(valueToQuotedStringN(str, static_cast<unsigned>(end-str)));
|
||||
else pushValue("");
|
||||
if (ok)
|
||||
pushValue(valueToQuotedStringN(str, static_cast<unsigned>(end - str)));
|
||||
else
|
||||
pushValue("");
|
||||
break;
|
||||
}
|
||||
case booleanValue:
|
||||
@@ -731,7 +739,8 @@ void StyledStreamWriter::writeArrayValue(const Value& value) {
|
||||
if (hasChildValue)
|
||||
writeWithIndent(childValues_[index]);
|
||||
else {
|
||||
if (!indented_) writeIndent();
|
||||
if (!indented_)
|
||||
writeIndent();
|
||||
indented_ = true;
|
||||
writeValue(childValue);
|
||||
indented_ = false;
|
||||
@@ -766,7 +775,7 @@ bool StyledStreamWriter::isMultilineArray(const Value& value) {
|
||||
for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
|
||||
const Value& childValue = value[index];
|
||||
isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
|
||||
childValue.size() > 0);
|
||||
childValue.size() > 0);
|
||||
}
|
||||
if (!isMultiLine) // check if line length > max line length
|
||||
{
|
||||
@@ -802,7 +811,8 @@ void StyledStreamWriter::writeIndent() {
|
||||
}
|
||||
|
||||
void StyledStreamWriter::writeWithIndent(const JSONCPP_STRING& value) {
|
||||
if (!indented_) writeIndent();
|
||||
if (!indented_)
|
||||
writeIndent();
|
||||
*document_ << value;
|
||||
indented_ = false;
|
||||
}
|
||||
@@ -818,13 +828,13 @@ void StyledStreamWriter::writeCommentBeforeValue(const Value& root) {
|
||||
if (!root.hasComment(commentBefore))
|
||||
return;
|
||||
|
||||
if (!indented_) writeIndent();
|
||||
if (!indented_)
|
||||
writeIndent();
|
||||
const JSONCPP_STRING& comment = root.getComment(commentBefore);
|
||||
JSONCPP_STRING::const_iterator iter = comment.begin();
|
||||
while (iter != comment.end()) {
|
||||
*document_ << *iter;
|
||||
if (*iter == '\n' &&
|
||||
((iter+1) != comment.end() && *(iter + 1) == '/'))
|
||||
if (*iter == '\n' && ((iter + 1) != comment.end() && *(iter + 1) == '/'))
|
||||
// writeIndent(); // would include newline
|
||||
*document_ << indentString_;
|
||||
++iter;
|
||||
@@ -856,24 +866,23 @@ bool StyledStreamWriter::hasCommentForValue(const Value& value) {
|
||||
struct CommentStyle {
|
||||
/// Decide whether to write comments.
|
||||
enum Enum {
|
||||
None, ///< Drop all comments.
|
||||
Most, ///< Recover odd behavior of previous versions (not implemented yet).
|
||||
All ///< Keep all comments.
|
||||
None, ///< Drop all comments.
|
||||
Most, ///< Recover odd behavior of previous versions (not implemented yet).
|
||||
All ///< Keep all comments.
|
||||
};
|
||||
};
|
||||
|
||||
struct BuiltStyledStreamWriter : public StreamWriter
|
||||
{
|
||||
BuiltStyledStreamWriter(
|
||||
JSONCPP_STRING const& indentation,
|
||||
CommentStyle::Enum cs,
|
||||
JSONCPP_STRING const& colonSymbol,
|
||||
JSONCPP_STRING const& nullSymbol,
|
||||
JSONCPP_STRING const& endingLineFeedSymbol,
|
||||
bool useSpecialFloats,
|
||||
unsigned int precision,
|
||||
PrecisionType precisionType);
|
||||
struct BuiltStyledStreamWriter : public StreamWriter {
|
||||
BuiltStyledStreamWriter(JSONCPP_STRING const& indentation,
|
||||
CommentStyle::Enum cs,
|
||||
JSONCPP_STRING const& colonSymbol,
|
||||
JSONCPP_STRING const& nullSymbol,
|
||||
JSONCPP_STRING const& endingLineFeedSymbol,
|
||||
bool useSpecialFloats,
|
||||
unsigned int precision,
|
||||
PrecisionType precisionType);
|
||||
int write(Value const& root, JSONCPP_OSTREAM* sout) JSONCPP_OVERRIDE;
|
||||
|
||||
private:
|
||||
void writeValue(Value const& value);
|
||||
void writeArrayValue(Value const& value);
|
||||
@@ -904,35 +913,27 @@ private:
|
||||
PrecisionType precisionType_;
|
||||
};
|
||||
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
|
||||
JSONCPP_STRING const& indentation,
|
||||
CommentStyle::Enum cs,
|
||||
JSONCPP_STRING const& colonSymbol,
|
||||
JSONCPP_STRING const& nullSymbol,
|
||||
JSONCPP_STRING const& endingLineFeedSymbol,
|
||||
bool useSpecialFloats,
|
||||
unsigned int precision,
|
||||
PrecisionType precisionType)
|
||||
: rightMargin_(74)
|
||||
, indentation_(indentation)
|
||||
, cs_(cs)
|
||||
, colonSymbol_(colonSymbol)
|
||||
, nullSymbol_(nullSymbol)
|
||||
, endingLineFeedSymbol_(endingLineFeedSymbol)
|
||||
, addChildValues_(false)
|
||||
, indented_(false)
|
||||
, useSpecialFloats_(useSpecialFloats)
|
||||
, precision_(precision)
|
||||
, precisionType_(precisionType)
|
||||
{
|
||||
}
|
||||
int BuiltStyledStreamWriter::write(Value const& root, JSONCPP_OSTREAM* sout)
|
||||
{
|
||||
JSONCPP_STRING const& indentation,
|
||||
CommentStyle::Enum cs,
|
||||
JSONCPP_STRING const& colonSymbol,
|
||||
JSONCPP_STRING const& nullSymbol,
|
||||
JSONCPP_STRING const& endingLineFeedSymbol,
|
||||
bool useSpecialFloats,
|
||||
unsigned int precision,
|
||||
PrecisionType precisionType)
|
||||
: rightMargin_(74), indentation_(indentation), cs_(cs),
|
||||
colonSymbol_(colonSymbol), nullSymbol_(nullSymbol),
|
||||
endingLineFeedSymbol_(endingLineFeedSymbol), addChildValues_(false),
|
||||
indented_(false), useSpecialFloats_(useSpecialFloats),
|
||||
precision_(precision), precisionType_(precisionType) {}
|
||||
int BuiltStyledStreamWriter::write(Value const& root, JSONCPP_OSTREAM* sout) {
|
||||
sout_ = sout;
|
||||
addChildValues_ = false;
|
||||
indented_ = true;
|
||||
indentString_.clear();
|
||||
writeCommentBeforeValue(root);
|
||||
if (!indented_) writeIndent();
|
||||
if (!indented_)
|
||||
writeIndent();
|
||||
indented_ = true;
|
||||
writeValue(root);
|
||||
writeCommentAfterValueOnSameLine(root);
|
||||
@@ -952,16 +953,18 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
|
||||
pushValue(valueToString(value.asLargestUInt()));
|
||||
break;
|
||||
case realValue:
|
||||
pushValue(valueToString(value.asDouble(), useSpecialFloats_, precision_, precisionType_));
|
||||
pushValue(valueToString(value.asDouble(), useSpecialFloats_, precision_,
|
||||
precisionType_));
|
||||
break;
|
||||
case stringValue:
|
||||
{
|
||||
case stringValue: {
|
||||
// Is NULL is possible for value.string_? No.
|
||||
char const* str;
|
||||
char const* end;
|
||||
bool ok = value.getString(&str, &end);
|
||||
if (ok) pushValue(valueToQuotedStringN(str, static_cast<unsigned>(end-str)));
|
||||
else pushValue("");
|
||||
if (ok)
|
||||
pushValue(valueToQuotedStringN(str, static_cast<unsigned>(end - str)));
|
||||
else
|
||||
pushValue("");
|
||||
break;
|
||||
}
|
||||
case booleanValue:
|
||||
@@ -982,7 +985,8 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
|
||||
JSONCPP_STRING const& name = *it;
|
||||
Value const& childValue = value[name];
|
||||
writeCommentBeforeValue(childValue);
|
||||
writeWithIndent(valueToQuotedStringN(name.data(), static_cast<unsigned>(name.length())));
|
||||
writeWithIndent(valueToQuotedStringN(
|
||||
name.data(), static_cast<unsigned>(name.length())));
|
||||
*sout_ << colonSymbol_;
|
||||
writeValue(childValue);
|
||||
if (++it == members.end()) {
|
||||
@@ -1016,7 +1020,8 @@ void BuiltStyledStreamWriter::writeArrayValue(Value const& value) {
|
||||
if (hasChildValue)
|
||||
writeWithIndent(childValues_[index]);
|
||||
else {
|
||||
if (!indented_) writeIndent();
|
||||
if (!indented_)
|
||||
writeIndent();
|
||||
indented_ = true;
|
||||
writeValue(childValue);
|
||||
indented_ = false;
|
||||
@@ -1034,13 +1039,15 @@ void BuiltStyledStreamWriter::writeArrayValue(Value const& value) {
|
||||
{
|
||||
assert(childValues_.size() == size);
|
||||
*sout_ << "[";
|
||||
if (!indentation_.empty()) *sout_ << " ";
|
||||
if (!indentation_.empty())
|
||||
*sout_ << " ";
|
||||
for (unsigned index = 0; index < size; ++index) {
|
||||
if (index > 0)
|
||||
*sout_ << ((!indentation_.empty()) ? ", " : ",");
|
||||
*sout_ << childValues_[index];
|
||||
}
|
||||
if (!indentation_.empty()) *sout_ << " ";
|
||||
if (!indentation_.empty())
|
||||
*sout_ << " ";
|
||||
*sout_ << "]";
|
||||
}
|
||||
}
|
||||
@@ -1053,7 +1060,7 @@ bool BuiltStyledStreamWriter::isMultilineArray(Value const& value) {
|
||||
for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
|
||||
Value const& childValue = value[index];
|
||||
isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
|
||||
childValue.size() > 0);
|
||||
childValue.size() > 0);
|
||||
}
|
||||
if (!isMultiLine) // check if line length > max line length
|
||||
{
|
||||
@@ -1093,7 +1100,8 @@ void BuiltStyledStreamWriter::writeIndent() {
|
||||
}
|
||||
|
||||
void BuiltStyledStreamWriter::writeWithIndent(JSONCPP_STRING const& value) {
|
||||
if (!indented_) writeIndent();
|
||||
if (!indented_)
|
||||
writeIndent();
|
||||
*sout_ << value;
|
||||
indented_ = false;
|
||||
}
|
||||
@@ -1106,17 +1114,18 @@ void BuiltStyledStreamWriter::unindent() {
|
||||
}
|
||||
|
||||
void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
|
||||
if (cs_ == CommentStyle::None) return;
|
||||
if (cs_ == CommentStyle::None)
|
||||
return;
|
||||
if (!root.hasComment(commentBefore))
|
||||
return;
|
||||
|
||||
if (!indented_) writeIndent();
|
||||
if (!indented_)
|
||||
writeIndent();
|
||||
const JSONCPP_STRING& comment = root.getComment(commentBefore);
|
||||
JSONCPP_STRING::const_iterator iter = comment.begin();
|
||||
while (iter != comment.end()) {
|
||||
*sout_ << *iter;
|
||||
if (*iter == '\n' &&
|
||||
((iter+1) != comment.end() && *(iter + 1) == '/'))
|
||||
if (*iter == '\n' && ((iter + 1) != comment.end() && *(iter + 1) == '/'))
|
||||
// writeIndent(); // would write extra newline
|
||||
*sout_ << indentString_;
|
||||
++iter;
|
||||
@@ -1124,8 +1133,10 @@ void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
|
||||
indented_ = false;
|
||||
}
|
||||
|
||||
void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(Value const& root) {
|
||||
if (cs_ == CommentStyle::None) return;
|
||||
void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(
|
||||
Value const& root) {
|
||||
if (cs_ == CommentStyle::None)
|
||||
return;
|
||||
if (root.hasComment(commentAfterOnSameLine))
|
||||
*sout_ << " " + root.getComment(commentAfterOnSameLine);
|
||||
|
||||
@@ -1145,29 +1156,18 @@ bool BuiltStyledStreamWriter::hasCommentForValue(const Value& value) {
|
||||
///////////////
|
||||
// StreamWriter
|
||||
|
||||
StreamWriter::StreamWriter()
|
||||
: sout_(NULL)
|
||||
{
|
||||
}
|
||||
StreamWriter::~StreamWriter()
|
||||
{
|
||||
}
|
||||
StreamWriter::Factory::~Factory()
|
||||
{}
|
||||
StreamWriterBuilder::StreamWriterBuilder()
|
||||
{
|
||||
setDefaults(&settings_);
|
||||
}
|
||||
StreamWriterBuilder::~StreamWriterBuilder()
|
||||
{}
|
||||
StreamWriter* StreamWriterBuilder::newStreamWriter() const
|
||||
{
|
||||
StreamWriter::StreamWriter() : sout_(NULL) {}
|
||||
StreamWriter::~StreamWriter() {}
|
||||
StreamWriter::Factory::~Factory() {}
|
||||
StreamWriterBuilder::StreamWriterBuilder() { setDefaults(&settings_); }
|
||||
StreamWriterBuilder::~StreamWriterBuilder() {}
|
||||
StreamWriter* StreamWriterBuilder::newStreamWriter() const {
|
||||
JSONCPP_STRING indentation = settings_["indentation"].asString();
|
||||
JSONCPP_STRING cs_str = settings_["commentStyle"].asString();
|
||||
JSONCPP_STRING pt_str = settings_["precisionType"].asString();
|
||||
bool eyc = settings_["enableYAMLCompatibility"].asBool();
|
||||
bool dnp = settings_["dropNullPlaceholders"].asBool();
|
||||
bool usf = settings_["useSpecialFloats"].asBool();
|
||||
bool usf = settings_["useSpecialFloats"].asBool();
|
||||
unsigned int pre = settings_["precision"].asUInt();
|
||||
CommentStyle::Enum cs = CommentStyle::All;
|
||||
if (cs_str == "All") {
|
||||
@@ -1195,14 +1195,14 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
|
||||
if (dnp) {
|
||||
nullSymbol.clear();
|
||||
}
|
||||
if (pre > 17) pre = 17;
|
||||
if (pre > 17)
|
||||
pre = 17;
|
||||
JSONCPP_STRING endingLineFeedSymbol;
|
||||
return new BuiltStyledStreamWriter(
|
||||
indentation, cs,
|
||||
colonSymbol, nullSymbol, endingLineFeedSymbol, usf, pre, precisionType);
|
||||
return new BuiltStyledStreamWriter(indentation, cs, colonSymbol, nullSymbol,
|
||||
endingLineFeedSymbol, usf, pre,
|
||||
precisionType);
|
||||
}
|
||||
static void getValidWriterKeys(std::set<JSONCPP_STRING>* valid_keys)
|
||||
{
|
||||
static void getValidWriterKeys(std::set<JSONCPP_STRING>* valid_keys) {
|
||||
valid_keys->clear();
|
||||
valid_keys->insert("indentation");
|
||||
valid_keys->insert("commentStyle");
|
||||
@@ -1212,10 +1212,10 @@ static void getValidWriterKeys(std::set<JSONCPP_STRING>* valid_keys)
|
||||
valid_keys->insert("precision");
|
||||
valid_keys->insert("precisionType");
|
||||
}
|
||||
bool StreamWriterBuilder::validate(Json::Value* invalid) const
|
||||
{
|
||||
bool StreamWriterBuilder::validate(Json::Value* invalid) const {
|
||||
Json::Value my_invalid;
|
||||
if (!invalid) invalid = &my_invalid; // so we do not need to test for NULL
|
||||
if (!invalid)
|
||||
invalid = &my_invalid; // so we do not need to test for NULL
|
||||
Json::Value& inv = *invalid;
|
||||
std::set<JSONCPP_STRING> valid_keys;
|
||||
getValidWriterKeys(&valid_keys);
|
||||
@@ -1229,13 +1229,11 @@ bool StreamWriterBuilder::validate(Json::Value* invalid) const
|
||||
}
|
||||
return 0u == inv.size();
|
||||
}
|
||||
Value& StreamWriterBuilder::operator[](JSONCPP_STRING key)
|
||||
{
|
||||
Value& StreamWriterBuilder::operator[](JSONCPP_STRING key) {
|
||||
return settings_[key];
|
||||
}
|
||||
// static
|
||||
void StreamWriterBuilder::setDefaults(Json::Value* settings)
|
||||
{
|
||||
void StreamWriterBuilder::setDefaults(Json::Value* settings) {
|
||||
//! [StreamWriterBuilderDefaults]
|
||||
(*settings)["commentStyle"] = "All";
|
||||
(*settings)["indentation"] = "\t";
|
||||
@@ -1247,7 +1245,8 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings)
|
||||
//! [StreamWriterBuilderDefaults]
|
||||
}
|
||||
|
||||
JSONCPP_STRING writeString(StreamWriter::Factory const& builder, Value const& root) {
|
||||
JSONCPP_STRING writeString(StreamWriter::Factory const& builder,
|
||||
Value const& root) {
|
||||
JSONCPP_OSTRINGSTREAM sout;
|
||||
StreamWriterPtr const writer(builder.newStreamWriter());
|
||||
writer->write(root, &sout);
|
||||
|
Reference in New Issue
Block a user