6#if !defined(JSON_IS_AMALGAMATION)
20#if __cplusplus >= 201103L
25#define isnan std::isnan
29#define isfinite std::isfinite
44#define isfinite _finite
47#if !defined(_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES)
48#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
53#if defined(__sun) && defined(__SVR4)
56#define isfinite finite
62#if defined(__ia64) && !defined(finite)
64 ((sizeof(x) == sizeof(float) ? _Isfinitef(x) : _IsFinite(x)))
71#define isnan(x) ((x) != (x))
74#if !defined(__APPLE__)
76#define isfinite finite
83#pragma warning(disable : 4996)
88#if __cplusplus >= 201103L || (defined(_CPPLIB_VER) && _CPPLIB_VER >= 520)
96 char* current = buffer +
sizeof(buffer);
100 }
else if (value < 0) {
106 assert(current >= buffer);
112 char* current = buffer +
sizeof(buffer);
114 assert(current >= buffer);
118#if defined(JSON_HAS_INT64)
128 unsigned int precision, PrecisionType precisionType) {
133 static const char*
const reps[2][3] = {{
"NaN",
"-Infinity",
"Infinity"},
134 {
"null",
"-1e+9999",
"1e+9999"}};
135 return reps[useSpecialFloats ? 0 : 1]
136 [
isnan(value) ? 0 : (value < 0) ? 1 : 2];
139 String buffer(
size_t(36),
'\0');
142 &*buffer.begin(), buffer.size(),
146 auto wouldPrint =
static_cast<size_t>(len);
147 if (wouldPrint >= buffer.size()) {
148 buffer.resize(wouldPrint + 1);
151 buffer.resize(wouldPrint);
159 if (buffer.find(
'.') == buffer.npos && buffer.find(
'e') == buffer.npos) {
175 return valueToString(value,
false, precision, precisionType);
183 return std::any_of(s, s + n, [](
unsigned char c) {
184 return c ==
'\\' || c ==
'"' || c < 0x20 || c > 0x7F;
189 const unsigned int REPLACEMENT_CHARACTER = 0xFFFD;
191 unsigned int firstByte =
static_cast<unsigned char>(*s);
193 if (firstByte < 0x80)
196 if (firstByte < 0xE0) {
198 return REPLACEMENT_CHARACTER;
200 unsigned int calculated =
201 ((firstByte & 0x1F) << 6) | (
static_cast<unsigned int>(s[1]) & 0x3F);
204 return calculated < 0x80 ? REPLACEMENT_CHARACTER : calculated;
207 if (firstByte < 0xF0) {
209 return REPLACEMENT_CHARACTER;
211 unsigned int calculated = ((firstByte & 0x0F) << 12) |
212 ((
static_cast<unsigned int>(s[1]) & 0x3F) << 6) |
213 (
static_cast<unsigned int>(s[2]) & 0x3F);
217 if (calculated >= 0xD800 && calculated <= 0xDFFF)
218 return REPLACEMENT_CHARACTER;
220 return calculated < 0x800 ? REPLACEMENT_CHARACTER : calculated;
223 if (firstByte < 0xF8) {
225 return REPLACEMENT_CHARACTER;
227 unsigned int calculated = ((firstByte & 0x07) << 18) |
228 ((
static_cast<unsigned int>(s[1]) & 0x3F) << 12) |
229 ((
static_cast<unsigned int>(s[2]) & 0x3F) << 6) |
230 (
static_cast<unsigned int>(s[3]) & 0x3F);
233 return calculated < 0x10000 ? REPLACEMENT_CHARACTER : calculated;
236 return REPLACEMENT_CHARACTER;
239static const char hex2[] =
"000102030405060708090a0b0c0d0e0f"
240 "101112131415161718191a1b1c1d1e1f"
241 "202122232425262728292a2b2c2d2e2f"
242 "303132333435363738393a3b3c3d3e3f"
243 "404142434445464748494a4b4c4d4e4f"
244 "505152535455565758595a5b5c5d5e5f"
245 "606162636465666768696a6b6c6d6e6f"
246 "707172737475767778797a7b7c7d7e7f"
247 "808182838485868788898a8b8c8d8e8f"
248 "909192939495969798999a9b9c9d9e9f"
249 "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
250 "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
251 "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
252 "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
253 "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
254 "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
257 const unsigned int hi = (x >> 8) & 0xff;
258 const unsigned int lo = x & 0xff;
260 result[0] =
hex2[2 * hi];
261 result[1] =
hex2[2 * hi + 1];
262 result[2] =
hex2[2 * lo];
263 result[3] =
hex2[2 * lo + 1];
268 result +=
static_cast<char>(ch);
276 bool emitUTF8 =
false) {
277 if (value ==
nullptr)
281 return String(
"\"") + value +
"\"";
285 String::size_type maxsize = length * 2 + 3;
287 result.reserve(maxsize);
289 char const* end = value + length;
290 for (
const char* c = value; c != end; ++c) {
377 if (!omitEndingLineFeed_)
382void FastWriter::writeValue(
const Value& value) {
383 switch (value.
type()) {
385 if (!dropNullPlaceholders_)
412 for (
ArrayIndex index = 0; index < size; ++index) {
415 writeValue(value[index]);
422 for (
auto it = members.begin(); it != members.end(); ++it) {
424 if (it != members.begin())
427 document_ += yamlCompatibilityEnabled_ ?
": " :
":";
428 writeValue(value[name]);
442 addChildValues_ =
false;
443 indentString_.clear();
444 writeCommentBeforeValue(root);
446 writeCommentAfterValueOnSameLine(root);
451void StyledWriter::writeValue(
const Value& value) {
452 switch (value.
type()) {
480 writeArrayValue(value);
487 writeWithIndent(
"{");
489 auto it = members.begin();
492 const Value& childValue = value[name];
493 writeCommentBeforeValue(childValue);
496 writeValue(childValue);
497 if (++it == members.end()) {
498 writeCommentAfterValueOnSameLine(childValue);
502 writeCommentAfterValueOnSameLine(childValue);
505 writeWithIndent(
"}");
511void StyledWriter::writeArrayValue(
const Value& value) {
512 size_t size = value.size();
516 bool isArrayMultiLine = isMultilineArray(value);
517 if (isArrayMultiLine) {
518 writeWithIndent(
"[");
520 bool hasChildValue = !childValues_.empty();
523 const Value& childValue = value[index];
524 writeCommentBeforeValue(childValue);
526 writeWithIndent(childValues_[index]);
529 writeValue(childValue);
531 if (++index == size) {
532 writeCommentAfterValueOnSameLine(childValue);
536 writeCommentAfterValueOnSameLine(childValue);
539 writeWithIndent(
"]");
542 assert(childValues_.size() == size);
544 for (
size_t index = 0; index < size; ++index) {
547 document_ += childValues_[index];
554bool StyledWriter::isMultilineArray(
const Value& value) {
556 bool isMultiLine = size * 3 >= rightMargin_;
557 childValues_.clear();
558 for (
ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
559 const Value& childValue = value[index];
560 isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
561 !childValue.empty());
565 childValues_.reserve(size);
566 addChildValues_ =
true;
568 for (
ArrayIndex index = 0; index < size; ++index) {
569 if (hasCommentForValue(value[index])) {
572 writeValue(value[index]);
573 lineLength +=
static_cast<ArrayIndex>(childValues_[index].length());
575 addChildValues_ =
false;
576 isMultiLine = isMultiLine || lineLength >= rightMargin_;
581void StyledWriter::pushValue(
const String& value) {
583 childValues_.push_back(value);
588void StyledWriter::writeIndent() {
589 if (!document_.empty()) {
590 char last = document_[document_.length() - 1];
596 document_ += indentString_;
599void StyledWriter::writeWithIndent(
const String& value) {
604void StyledWriter::indent() { indentString_ +=
String(indentSize_,
' '); }
606void StyledWriter::unindent() {
607 assert(indentString_.size() >= indentSize_);
608 indentString_.resize(indentString_.size() - indentSize_);
611void StyledWriter::writeCommentBeforeValue(
const Value& root) {
618 String::const_iterator iter = comment.begin();
619 while (iter != comment.end()) {
621 if (*iter ==
'\n' && ((iter + 1) != comment.end() && *(iter + 1) ==
'/'))
630void StyledWriter::writeCommentAfterValueOnSameLine(
const Value& root) {
641bool StyledWriter::hasCommentForValue(
const Value& value) {
651 : document_(nullptr), indentation_(std::move(indentation)),
652 addChildValues_(), indented_(false) {}
656 addChildValues_ =
false;
657 indentString_.clear();
659 writeCommentBeforeValue(root);
664 writeCommentAfterValueOnSameLine(root);
669void StyledStreamWriter::writeValue(
const Value& value) {
670 switch (value.
type()) {
698 writeArrayValue(value);
705 writeWithIndent(
"{");
707 auto it = members.begin();
710 const Value& childValue = value[name];
711 writeCommentBeforeValue(childValue);
714 writeValue(childValue);
715 if (++it == members.end()) {
716 writeCommentAfterValueOnSameLine(childValue);
720 writeCommentAfterValueOnSameLine(childValue);
723 writeWithIndent(
"}");
729void StyledStreamWriter::writeArrayValue(
const Value& value) {
730 unsigned size = value.size();
734 bool isArrayMultiLine = isMultilineArray(value);
735 if (isArrayMultiLine) {
736 writeWithIndent(
"[");
738 bool hasChildValue = !childValues_.empty();
741 const Value& childValue = value[index];
742 writeCommentBeforeValue(childValue);
744 writeWithIndent(childValues_[index]);
749 writeValue(childValue);
752 if (++index == size) {
753 writeCommentAfterValueOnSameLine(childValue);
757 writeCommentAfterValueOnSameLine(childValue);
760 writeWithIndent(
"]");
763 assert(childValues_.size() == size);
765 for (
unsigned index = 0; index < size; ++index) {
768 *document_ << childValues_[index];
775bool StyledStreamWriter::isMultilineArray(
const Value& value) {
777 bool isMultiLine = size * 3 >= rightMargin_;
778 childValues_.clear();
779 for (
ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
780 const Value& childValue = value[index];
781 isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
782 !childValue.empty());
786 childValues_.reserve(size);
787 addChildValues_ =
true;
789 for (
ArrayIndex index = 0; index < size; ++index) {
790 if (hasCommentForValue(value[index])) {
793 writeValue(value[index]);
794 lineLength +=
static_cast<ArrayIndex>(childValues_[index].length());
796 addChildValues_ =
false;
797 isMultiLine = isMultiLine || lineLength >= rightMargin_;
802void StyledStreamWriter::pushValue(
const String& value) {
804 childValues_.push_back(value);
809void StyledStreamWriter::writeIndent() {
814 *document_ <<
'\n' << indentString_;
817void StyledStreamWriter::writeWithIndent(
const String& value) {
824void StyledStreamWriter::indent() { indentString_ += indentation_; }
826void StyledStreamWriter::unindent() {
827 assert(indentString_.size() >= indentation_.size());
828 indentString_.resize(indentString_.size() - indentation_.size());
831void StyledStreamWriter::writeCommentBeforeValue(
const Value& root) {
838 String::const_iterator iter = comment.begin();
839 while (iter != comment.end()) {
841 if (*iter ==
'\n' && ((iter + 1) != comment.end() && *(iter + 1) ==
'/'))
843 *document_ << indentString_;
849void StyledStreamWriter::writeCommentAfterValueOnSameLine(
const Value& root) {
860bool StyledStreamWriter::hasCommentForValue(
const Value& value) {
879struct BuiltStyledStreamWriter :
public StreamWriter {
880 BuiltStyledStreamWriter(
String indentation, CommentStyle::Enum cs,
882 String endingLineFeedSymbol,
bool useSpecialFloats,
883 bool emitUTF8,
unsigned int precision,
884 PrecisionType precisionType);
885 int write(Value
const& root,
OStream* sout)
override;
888 void writeValue(Value
const& value);
889 void writeArrayValue(Value
const& value);
890 bool isMultilineArray(Value
const& value);
891 void pushValue(
String const& value);
893 void writeWithIndent(
String const& value);
896 void writeCommentBeforeValue(Value
const& root);
897 void writeCommentAfterValueOnSameLine(Value
const& root);
898 static bool hasCommentForValue(
const Value& value);
900 using ChildValues = std::vector<String>;
902 ChildValues childValues_;
904 unsigned int rightMargin_;
906 CommentStyle::Enum cs_;
909 String endingLineFeedSymbol_;
910 bool addChildValues_ : 1;
912 bool useSpecialFloats_ : 1;
914 unsigned int precision_;
915 PrecisionType precisionType_;
917BuiltStyledStreamWriter::BuiltStyledStreamWriter(
918 String indentation, CommentStyle::Enum cs,
String colonSymbol,
919 String nullSymbol,
String endingLineFeedSymbol,
bool useSpecialFloats,
920 bool emitUTF8,
unsigned int precision, PrecisionType precisionType)
921 : rightMargin_(74), indentation_(std::move(indentation)), cs_(cs),
922 colonSymbol_(std::move(colonSymbol)), nullSymbol_(std::move(nullSymbol)),
923 endingLineFeedSymbol_(std::move(endingLineFeedSymbol)),
924 addChildValues_(false), indented_(false),
925 useSpecialFloats_(useSpecialFloats), emitUTF8_(emitUTF8),
926 precision_(precision), precisionType_(precisionType) {}
927int BuiltStyledStreamWriter::write(Value
const& root,
OStream* sout) {
929 addChildValues_ =
false;
931 indentString_.clear();
932 writeCommentBeforeValue(root);
937 writeCommentAfterValueOnSameLine(root);
938 *
sout_ << endingLineFeedSymbol_;
942void BuiltStyledStreamWriter::writeValue(Value
const& value) {
943 switch (value.type()) {
945 pushValue(nullSymbol_);
954 pushValue(
valueToString(value.asDouble(), useSpecialFloats_, precision_,
961 bool ok = value.getString(&str, &end);
973 writeArrayValue(value);
980 writeWithIndent(
"{");
982 auto it = members.begin();
985 Value
const& childValue = value[name];
986 writeCommentBeforeValue(childValue);
989 *
sout_ << colonSymbol_;
990 writeValue(childValue);
991 if (++it == members.end()) {
992 writeCommentAfterValueOnSameLine(childValue);
996 writeCommentAfterValueOnSameLine(childValue);
999 writeWithIndent(
"}");
1005void BuiltStyledStreamWriter::writeArrayValue(Value
const& value) {
1006 unsigned size = value.size();
1010 bool isMultiLine = (cs_ == CommentStyle::All) || isMultilineArray(value);
1012 writeWithIndent(
"[");
1014 bool hasChildValue = !childValues_.empty();
1017 Value
const& childValue = value[index];
1018 writeCommentBeforeValue(childValue);
1020 writeWithIndent(childValues_[index]);
1025 writeValue(childValue);
1028 if (++index == size) {
1029 writeCommentAfterValueOnSameLine(childValue);
1033 writeCommentAfterValueOnSameLine(childValue);
1036 writeWithIndent(
"]");
1039 assert(childValues_.size() == size);
1041 if (!indentation_.empty())
1043 for (
unsigned index = 0; index < size; ++index) {
1045 *
sout_ << ((!indentation_.empty()) ?
", " :
",");
1046 *
sout_ << childValues_[index];
1048 if (!indentation_.empty())
1055bool BuiltStyledStreamWriter::isMultilineArray(Value
const& value) {
1057 bool isMultiLine = size * 3 >= rightMargin_;
1058 childValues_.clear();
1059 for (
ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
1060 Value
const& childValue = value[index];
1061 isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
1062 !childValue.empty());
1066 childValues_.reserve(size);
1067 addChildValues_ =
true;
1069 for (
ArrayIndex index = 0; index < size; ++index) {
1070 if (hasCommentForValue(value[index])) {
1073 writeValue(value[index]);
1074 lineLength +=
static_cast<ArrayIndex>(childValues_[index].length());
1076 addChildValues_ =
false;
1077 isMultiLine = isMultiLine || lineLength >= rightMargin_;
1082void BuiltStyledStreamWriter::pushValue(
String const& value) {
1083 if (addChildValues_)
1084 childValues_.push_back(value);
1089void BuiltStyledStreamWriter::writeIndent() {
1095 if (!indentation_.empty()) {
1097 *
sout_ <<
'\n' << indentString_;
1101void BuiltStyledStreamWriter::writeWithIndent(
String const& value) {
1108void BuiltStyledStreamWriter::indent() { indentString_ += indentation_; }
1110void BuiltStyledStreamWriter::unindent() {
1111 assert(indentString_.size() >= indentation_.size());
1112 indentString_.resize(indentString_.size() - indentation_.size());
1115void BuiltStyledStreamWriter::writeCommentBeforeValue(Value
const& root) {
1116 if (cs_ == CommentStyle::None)
1124 String::const_iterator iter = comment.begin();
1125 while (iter != comment.end()) {
1127 if (*iter ==
'\n' && ((iter + 1) != comment.end() && *(iter + 1) ==
'/'))
1129 *
sout_ << indentString_;
1135void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(
1136 Value
const& root) {
1137 if (cs_ == CommentStyle::None)
1149bool BuiltStyledStreamWriter::hasCommentForValue(
const Value& value) {
1172 CommentStyle::Enum cs = CommentStyle::All;
1173 if (cs_str ==
"All") {
1174 cs = CommentStyle::All;
1175 }
else if (cs_str ==
"None") {
1176 cs = CommentStyle::None;
1178 throwRuntimeError(
"commentStyle must be 'All' or 'None'");
1181 if (pt_str ==
"significant") {
1183 }
else if (pt_str ==
"decimal") {
1186 throwRuntimeError(
"precisionType must be 'significant' or 'decimal'");
1188 String colonSymbol =
" : ";
1191 }
else if (indentation.empty()) {
1194 String nullSymbol =
"null";
1200 String endingLineFeedSymbol;
1201 return new BuiltStyledStreamWriter(indentation, cs, colonSymbol, nullSymbol,
1202 endingLineFeedSymbol, usf, emitUTF8, pre,
1207 static const auto& valid_keys = *
new std::set<String>{
1210 "enableYAMLCompatibility",
1211 "dropNullPlaceholders",
1218 auto key = si.name();
1219 if (valid_keys.count(key))
1222 (*invalid)[key] = *si;
1226 return invalid ? invalid->
empty() :
true;
1235 (*settings)[
"commentStyle"] =
"All";
1236 (*settings)[
"indentation"] =
"\t";
1237 (*settings)[
"enableYAMLCompatibility"] =
false;
1238 (*settings)[
"dropNullPlaceholders"] =
false;
1239 (*settings)[
"useSpecialFloats"] =
false;
1240 (*settings)[
"emitUTF8"] =
false;
1241 (*settings)[
"precision"] = 17;
1242 (*settings)[
"precisionType"] =
"significant";
1249 writer->write(root, &sout);
1256 writer->write(root, &sout);
String write(const Value &root) override
void dropNullPlaceholders()
Drop the "null" string from the writer's output for nullValues.
void enableYAMLCompatibility()
void omitEndingLineFeed()
A simple abstract factory.
virtual StreamWriter * newStreamWriter() const =0
Allocate a CharReader via operator new().
Build a StreamWriter implementation.
StreamWriter * newStreamWriter() const override
bool validate(Json::Value *invalid) const
static void setDefaults(Json::Value *settings)
Called by ctor, but you can use this to reset settings_.
Json::Value settings_
Configuration of this builder.
~StreamWriterBuilder() override
Value & operator[](const String &key)
A simple way to update a specific setting.
void write(OStream &out, const Value &root)
Serialize a Value in JSON format.
StyledStreamWriter(String indentation="\t")
String write(const Value &root) override
Serialize a Value in JSON format.
const_iterator begin() const
bool empty() const
Return true if empty array, empty object, or null; otherwise, false.
static constexpr LargestInt maxLargestInt
Maximum signed integer value that can be stored in a Json::Value.
ArrayIndex size() const
Number of values in array or object.
bool getString(char const **begin, char const **end) const
Get raw char* of string-value.
std::vector< String > Members
const_iterator end() const
String asString() const
Embedded zeroes are possible.
Members getMemberNames() const
Return a list of the member names.
LargestInt asLargestInt() const
LargestUInt asLargestUInt() const
static constexpr LargestInt minLargestInt
Minimum signed integer value that can be stored in a Json::Value.
JSON (JavaScript Object Notation).
std::basic_ostringstream< String::value_type, String::traits_type, String::allocator_type > OStringStream
static unsigned int utf8ToCodepoint(const char *&s, const char *e)
static void appendRaw(String &result, unsigned ch)
String writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience.
Iter fixNumericLocale(Iter begin, Iter end)
Change ',' to '.
@ commentAfterOnSameLine
a comment just after a value on the same line
@ commentBefore
a comment placed on the line before a value
@ commentAfter
a comment on the line after a value (only make sense for
static String toHex16Bit(unsigned int x)
static bool doesAnyCharRequireEscaping(char const *s, size_t n)
String valueToQuotedString(const char *value)
static String valueToQuotedStringN(const char *value, size_t length, bool emitUTF8=false)
String valueToString(Int value)
@ stringValue
UTF-8 string value.
@ arrayValue
array value (ordered list)
@ intValue
signed integer value
@ objectValue
object value (collection of name/value pairs).
@ uintValue
unsigned integer value
OStream & operator<<(OStream &, const Value &root)
Output using the StyledStreamWriter.
std::auto_ptr< StreamWriter > StreamWriterPtr
static void appendHex(String &result, unsigned ch)
Iter fixZerosInTheEnd(Iter begin, Iter end, unsigned int precision)
Return iterator that would be the new end of the range [begin,end), if we were to delete zeros in the...
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
char[uintToStringBufferSize] UIntToStringBuffer
static void uintToString(LargestUInt value, char *¤t)
Converts an unsigned integer to string.
PrecisionType
Type of precision for formatting of real values.
@ decimalPlaces
we set max number of digits after "." in string
@ significantDigits
we set max number of significant digits in string