6 #if !defined(JSON_IS_AMALGAMATION)
9 #endif // if !defined(JSON_IS_AMALGAMATION)
18 #if __cplusplus >= 201103L
23 #define isnan std::isnan
26 #if !defined(isfinite)
27 #define isfinite std::isfinite
40 #if !defined(isfinite)
42 #define isfinite _finite
45 #if !defined(_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES)
46 #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
47 #endif //_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES
51 #if defined(__sun) && defined(__SVR4) // Solaris
52 #if !defined(isfinite)
54 #define isfinite finite
59 #if !defined(isfinite)
60 #if defined(__ia64) && !defined(finite)
62 ((sizeof(x) == sizeof(float) ? _Isfinitef(x) : _IsFinite(x)))
69 #define isnan(x) (x != x)
72 #if !defined(__APPLE__)
73 #if !defined(isfinite)
74 #define isfinite finite
81 #pragma warning(disable : 4996)
86 #if __cplusplus >= 201103L || (defined(_CPPLIB_VER) && _CPPLIB_VER >= 520)
94 char* current = buffer +
sizeof(buffer);
98 }
else if (value < 0) {
104 assert(current >= buffer);
110 char* current = buffer +
sizeof(buffer);
112 assert(current >= buffer);
116 #if defined(JSON_HAS_INT64)
122 #endif // # if defined(JSON_HAS_INT64)
131 static const char*
const reps[2][3] = {{
"NaN",
"-Infinity",
"Infinity"},
132 {
"null",
"-1e+9999",
"1e+9999"}};
133 return reps[useSpecialFloats ? 0 : 1]
134 [
isnan(value) ? 0 : (value < 0) ? 1 : 2];
137 String buffer(
size_t(36),
'\0');
140 &*buffer.begin(), buffer.size(),
144 auto wouldPrint = static_cast<size_t>(len);
145 if (wouldPrint >= buffer.size()) {
146 buffer.resize(wouldPrint + 1);
149 buffer.resize(wouldPrint);
162 if (buffer.find(
'.') == buffer.npos && buffer.find(
'e') == buffer.npos) {
171 return valueToString(value,
false, precision, precisionType);
179 char const*
const end = s + n;
180 for (
char const* cur = s; cur < end; ++cur) {
181 if (*cur ==
'\\' || *cur ==
'\"' || *cur <
' ' ||
182 static_cast<unsigned char>(*cur) < 0x80)
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;
239 static 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 bool emitUTF8 =
false) {
269 if (value ==
nullptr)
273 return String(
"\"") + value +
"\"";
277 String::size_type maxsize = length * 2 + 3;
279 result.reserve(maxsize);
281 char const* end = value + length;
282 for (
const char* c = value; c != end; ++c) {
318 const unsigned int FIRST_NON_CONTROL_CODEPOINT = 0x20;
319 const unsigned int LAST_NON_CONTROL_CODEPOINT = 0x7F;
320 const unsigned int FIRST_SURROGATE_PAIR_CODEPOINT = 0x10000;
323 if (FIRST_NON_CONTROL_CODEPOINT <= codepoint &&
324 codepoint <= LAST_NON_CONTROL_CODEPOINT) {
325 result += static_cast<char>(codepoint);
326 }
else if (codepoint <
327 FIRST_SURROGATE_PAIR_CODEPOINT) {
333 codepoint -= FIRST_SURROGATE_PAIR_CODEPOINT;
335 result +=
toHex16Bit((codepoint >> 10) + 0xD800);
337 result +=
toHex16Bit((codepoint & 0x3FF) + 0xDC00);
353 Writer::~Writer() =
default;
358 FastWriter::FastWriter()
362 void FastWriter::enableYAMLCompatibility() { yamlCompatibilityEnabled_ =
true; }
364 void FastWriter::dropNullPlaceholders() { dropNullPlaceholders_ =
true; }
366 void FastWriter::omitEndingLineFeed() { omitEndingLineFeed_ =
true; }
368 String FastWriter::write(
const Value& root) {
371 if (!omitEndingLineFeed_)
376 void FastWriter::writeValue(
const Value& value) {
377 switch (value.type()) {
379 if (!dropNullPlaceholders_)
395 bool ok = value.getString(&str, &end);
406 for (
ArrayIndex index = 0; index < size; ++index) {
409 writeValue(value[index]);
416 for (
auto it = members.begin(); it != members.end(); ++it) {
418 if (it != members.begin())
421 static_cast<unsigned>(name.length()));
422 document_ += yamlCompatibilityEnabled_ ?
": " :
":";
423 writeValue(value[name]);
433 StyledWriter::StyledWriter() =
default;
435 String StyledWriter::write(
const Value& root) {
437 addChildValues_ =
false;
438 indentString_.clear();
439 writeCommentBeforeValue(root);
441 writeCommentAfterValueOnSameLine(root);
446 void StyledWriter::writeValue(
const Value& value) {
447 switch (value.type()) {
464 bool ok = value.getString(&str, &end);
475 writeArrayValue(value);
482 writeWithIndent(
"{");
484 auto it = members.begin();
487 const Value& childValue = value[name];
488 writeCommentBeforeValue(childValue);
491 writeValue(childValue);
492 if (++it == members.end()) {
493 writeCommentAfterValueOnSameLine(childValue);
497 writeCommentAfterValueOnSameLine(childValue);
500 writeWithIndent(
"}");
506 void StyledWriter::writeArrayValue(
const Value& value) {
507 unsigned size = value.size();
511 bool isArrayMultiLine = isMultilineArray(value);
512 if (isArrayMultiLine) {
513 writeWithIndent(
"[");
515 bool hasChildValue = !childValues_.empty();
518 const Value& childValue = value[index];
519 writeCommentBeforeValue(childValue);
521 writeWithIndent(childValues_[index]);
524 writeValue(childValue);
526 if (++index == size) {
527 writeCommentAfterValueOnSameLine(childValue);
531 writeCommentAfterValueOnSameLine(childValue);
534 writeWithIndent(
"]");
537 assert(childValues_.size() == size);
539 for (
unsigned index = 0; index < size; ++index) {
542 document_ += childValues_[index];
549 bool StyledWriter::isMultilineArray(
const Value& value) {
551 bool isMultiLine = size * 3 >= rightMargin_;
552 childValues_.clear();
553 for (
ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
554 const Value& childValue = value[index];
555 isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
556 !childValue.empty());
560 childValues_.reserve(size);
561 addChildValues_ =
true;
563 for (
ArrayIndex index = 0; index < size; ++index) {
564 if (hasCommentForValue(value[index])) {
567 writeValue(value[index]);
568 lineLength += static_cast<ArrayIndex>(childValues_[index].length());
570 addChildValues_ =
false;
571 isMultiLine = isMultiLine || lineLength >= rightMargin_;
576 void StyledWriter::pushValue(
const String& value) {
578 childValues_.push_back(value);
583 void StyledWriter::writeIndent() {
584 if (!document_.empty()) {
585 char last = document_[document_.length() - 1];
591 document_ += indentString_;
594 void StyledWriter::writeWithIndent(
const String& value) {
599 void StyledWriter::indent() { indentString_ +=
String(indentSize_,
' '); }
601 void StyledWriter::unindent() {
602 assert(indentString_.size() >= indentSize_);
603 indentString_.resize(indentString_.size() - indentSize_);
606 void StyledWriter::writeCommentBeforeValue(
const Value& root) {
613 String::const_iterator iter = comment.begin();
614 while (iter != comment.end()) {
616 if (*iter ==
'\n' && ((iter + 1) != comment.end() && *(iter + 1) ==
'/'))
625 void StyledWriter::writeCommentAfterValueOnSameLine(
const Value& root) {
636 bool StyledWriter::hasCommentForValue(
const Value& value) {
645 StyledStreamWriter::StyledStreamWriter(
String indentation)
646 : document_(nullptr), indentation_(std::move(indentation)),
647 addChildValues_(), indented_(false) {}
649 void StyledStreamWriter::write(
OStream& out,
const Value& root) {
651 addChildValues_ =
false;
652 indentString_.clear();
654 writeCommentBeforeValue(root);
659 writeCommentAfterValueOnSameLine(root);
664 void StyledStreamWriter::writeValue(
const Value& value) {
665 switch (value.type()) {
682 bool ok = value.getString(&str, &end);
693 writeArrayValue(value);
696 Value::Members members(value.getMemberNames());
700 writeWithIndent(
"{");
702 auto it = members.begin();
705 const Value& childValue = value[name];
706 writeCommentBeforeValue(childValue);
709 writeValue(childValue);
710 if (++it == members.end()) {
711 writeCommentAfterValueOnSameLine(childValue);
715 writeCommentAfterValueOnSameLine(childValue);
718 writeWithIndent(
"}");
724 void StyledStreamWriter::writeArrayValue(
const Value& value) {
725 unsigned size = value.size();
729 bool isArrayMultiLine = isMultilineArray(value);
730 if (isArrayMultiLine) {
731 writeWithIndent(
"[");
733 bool hasChildValue = !childValues_.empty();
736 const Value& childValue = value[index];
737 writeCommentBeforeValue(childValue);
739 writeWithIndent(childValues_[index]);
744 writeValue(childValue);
747 if (++index == size) {
748 writeCommentAfterValueOnSameLine(childValue);
752 writeCommentAfterValueOnSameLine(childValue);
755 writeWithIndent(
"]");
758 assert(childValues_.size() == size);
760 for (
unsigned index = 0; index < size; ++index) {
763 *document_ << childValues_[index];
770 bool StyledStreamWriter::isMultilineArray(
const Value& value) {
772 bool isMultiLine = size * 3 >= rightMargin_;
773 childValues_.clear();
774 for (
ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
775 const Value& childValue = value[index];
776 isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
777 !childValue.empty());
781 childValues_.reserve(size);
782 addChildValues_ =
true;
784 for (
ArrayIndex index = 0; index < size; ++index) {
785 if (hasCommentForValue(value[index])) {
788 writeValue(value[index]);
789 lineLength += static_cast<ArrayIndex>(childValues_[index].length());
791 addChildValues_ =
false;
792 isMultiLine = isMultiLine || lineLength >= rightMargin_;
797 void StyledStreamWriter::pushValue(
const String& value) {
799 childValues_.push_back(value);
804 void StyledStreamWriter::writeIndent() {
809 *document_ <<
'\n' << indentString_;
812 void StyledStreamWriter::writeWithIndent(
const String& value) {
819 void StyledStreamWriter::indent() { indentString_ += indentation_; }
821 void StyledStreamWriter::unindent() {
822 assert(indentString_.size() >= indentation_.size());
823 indentString_.resize(indentString_.size() - indentation_.size());
826 void StyledStreamWriter::writeCommentBeforeValue(
const Value& root) {
833 String::const_iterator iter = comment.begin();
834 while (iter != comment.end()) {
836 if (*iter ==
'\n' && ((iter + 1) != comment.end() && *(iter + 1) ==
'/'))
838 *document_ << indentString_;
844 void StyledStreamWriter::writeCommentAfterValueOnSameLine(
const Value& root) {
855 bool StyledStreamWriter::hasCommentForValue(
const Value& value) {
865 struct CommentStyle {
874 struct BuiltStyledStreamWriter :
public StreamWriter {
875 BuiltStyledStreamWriter(
String indentation, CommentStyle::Enum cs,
877 String endingLineFeedSymbol,
bool useSpecialFloats,
878 bool emitUTF8,
unsigned int precision,
880 int write(Value
const& root,
OStream* sout)
override;
883 void writeValue(Value
const& value);
884 void writeArrayValue(Value
const& value);
885 bool isMultilineArray(Value
const& value);
886 void pushValue(
String const& value);
888 void writeWithIndent(
String const& value);
891 void writeCommentBeforeValue(Value
const& root);
892 void writeCommentAfterValueOnSameLine(Value
const& root);
893 static bool hasCommentForValue(
const Value& value);
895 using ChildValues = std::vector<String>;
897 ChildValues childValues_;
899 unsigned int rightMargin_;
901 CommentStyle::Enum cs_;
904 String endingLineFeedSymbol_;
905 bool addChildValues_ : 1;
907 bool useSpecialFloats_ : 1;
909 unsigned int precision_;
912 BuiltStyledStreamWriter::BuiltStyledStreamWriter(
913 String indentation, CommentStyle::Enum cs,
String colonSymbol,
914 String nullSymbol,
String endingLineFeedSymbol,
bool useSpecialFloats,
915 bool emitUTF8,
unsigned int precision,
PrecisionType precisionType)
916 : rightMargin_(74), indentation_(std::move(indentation)), cs_(cs),
917 colonSymbol_(std::move(colonSymbol)), nullSymbol_(std::move(nullSymbol)),
918 endingLineFeedSymbol_(std::move(endingLineFeedSymbol)),
919 addChildValues_(false), indented_(false),
920 useSpecialFloats_(useSpecialFloats), emitUTF8_(emitUTF8),
921 precision_(precision), precisionType_(precisionType) {}
922 int BuiltStyledStreamWriter::write(Value
const& root,
OStream* sout) {
924 addChildValues_ =
false;
926 indentString_.clear();
927 writeCommentBeforeValue(root);
932 writeCommentAfterValueOnSameLine(root);
933 *sout_ << endingLineFeedSymbol_;
937 void BuiltStyledStreamWriter::writeValue(Value
const& value) {
938 switch (value.type()) {
940 pushValue(nullSymbol_);
949 pushValue(
valueToString(value.asDouble(), useSpecialFloats_, precision_,
956 bool ok = value.getString(&str, &end);
968 writeArrayValue(value);
971 Value::Members members(value.getMemberNames());
975 writeWithIndent(
"{");
977 auto it = members.begin();
980 Value
const& childValue = value[name];
981 writeCommentBeforeValue(childValue);
983 name.data(), static_cast<unsigned>(name.length()), emitUTF8_));
984 *sout_ << colonSymbol_;
985 writeValue(childValue);
986 if (++it == members.end()) {
987 writeCommentAfterValueOnSameLine(childValue);
991 writeCommentAfterValueOnSameLine(childValue);
994 writeWithIndent(
"}");
1000 void BuiltStyledStreamWriter::writeArrayValue(Value
const& value) {
1001 unsigned size = value.size();
1005 bool isMultiLine = (cs_ == CommentStyle::All) || isMultilineArray(value);
1007 writeWithIndent(
"[");
1009 bool hasChildValue = !childValues_.empty();
1012 Value
const& childValue = value[index];
1013 writeCommentBeforeValue(childValue);
1015 writeWithIndent(childValues_[index]);
1020 writeValue(childValue);
1023 if (++index == size) {
1024 writeCommentAfterValueOnSameLine(childValue);
1028 writeCommentAfterValueOnSameLine(childValue);
1031 writeWithIndent(
"]");
1034 assert(childValues_.size() == size);
1036 if (!indentation_.empty())
1038 for (
unsigned index = 0; index < size; ++index) {
1040 *sout_ << ((!indentation_.empty()) ?
", " :
",");
1041 *sout_ << childValues_[index];
1043 if (!indentation_.empty())
1050 bool BuiltStyledStreamWriter::isMultilineArray(Value
const& value) {
1052 bool isMultiLine = size * 3 >= rightMargin_;
1053 childValues_.clear();
1054 for (
ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
1055 Value
const& childValue = value[index];
1056 isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
1057 !childValue.empty());
1061 childValues_.reserve(size);
1062 addChildValues_ =
true;
1064 for (
ArrayIndex index = 0; index < size; ++index) {
1065 if (hasCommentForValue(value[index])) {
1068 writeValue(value[index]);
1069 lineLength += static_cast<ArrayIndex>(childValues_[index].length());
1071 addChildValues_ =
false;
1072 isMultiLine = isMultiLine || lineLength >= rightMargin_;
1077 void BuiltStyledStreamWriter::pushValue(
String const& value) {
1078 if (addChildValues_)
1079 childValues_.push_back(value);
1084 void BuiltStyledStreamWriter::writeIndent() {
1090 if (!indentation_.empty()) {
1092 *sout_ <<
'\n' << indentString_;
1096 void BuiltStyledStreamWriter::writeWithIndent(
String const& value) {
1103 void BuiltStyledStreamWriter::indent() { indentString_ += indentation_; }
1105 void BuiltStyledStreamWriter::unindent() {
1106 assert(indentString_.size() >= indentation_.size());
1107 indentString_.resize(indentString_.size() - indentation_.size());
1110 void BuiltStyledStreamWriter::writeCommentBeforeValue(Value
const& root) {
1111 if (cs_ == CommentStyle::None)
1119 String::const_iterator iter = comment.begin();
1120 while (iter != comment.end()) {
1122 if (*iter ==
'\n' && ((iter + 1) != comment.end() && *(iter + 1) ==
'/'))
1124 *sout_ << indentString_;
1130 void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(
1131 Value
const& root) {
1132 if (cs_ == CommentStyle::None)
1144 bool BuiltStyledStreamWriter::hasCommentForValue(
const Value& value) {
1153 StreamWriter::StreamWriter() : sout_(nullptr) {}
1167 CommentStyle::Enum cs = CommentStyle::All;
1168 if (cs_str ==
"All") {
1169 cs = CommentStyle::All;
1170 }
else if (cs_str ==
"None") {
1171 cs = CommentStyle::None;
1173 throwRuntimeError(
"commentStyle must be 'All' or 'None'");
1176 if (pt_str ==
"significant") {
1178 }
else if (pt_str ==
"decimal") {
1181 throwRuntimeError(
"precisionType must be 'significant' or 'decimal'");
1183 String colonSymbol =
" : ";
1186 }
else if (indentation.empty()) {
1189 String nullSymbol =
"null";
1195 String endingLineFeedSymbol;
1196 return new BuiltStyledStreamWriter(indentation, cs, colonSymbol, nullSymbol,
1197 endingLineFeedSymbol, usf, emitUTF8, pre,
1201 valid_keys->clear();
1202 valid_keys->insert(
"indentation");
1203 valid_keys->insert(
"commentStyle");
1204 valid_keys->insert(
"enableYAMLCompatibility");
1205 valid_keys->insert(
"dropNullPlaceholders");
1206 valid_keys->insert(
"useSpecialFloats");
1207 valid_keys->insert(
"emitUTF8");
1208 valid_keys->insert(
"precision");
1209 valid_keys->insert(
"precisionType");
1214 invalid = &my_invalid;
1216 std::set<String> valid_keys;
1219 size_t n = keys.size();
1220 for (
size_t i = 0; i < n; ++i) {
1221 String const& key = keys[i];
1222 if (valid_keys.find(key) == valid_keys.end()) {
1234 (*settings)[
"commentStyle"] =
"All";
1235 (*settings)[
"indentation"] =
"\t";
1236 (*settings)[
"enableYAMLCompatibility"] =
false;
1237 (*settings)[
"dropNullPlaceholders"] =
false;
1238 (*settings)[
"useSpecialFloats"] =
false;
1239 (*settings)[
"emitUTF8"] =
false;
1240 (*settings)[
"precision"] = 17;
1241 (*settings)[
"precisionType"] =
"significant";
1248 writer->write(root, &sout);
1255 writer->write(root, &sout);