46 #if __cplusplus>=201103L
52 # elif defined(__INTEL_COMPILER)
60 #ifndef PICOJSON_USE_RVALUE_REFERENCE
61 # if (defined(__cpp_rvalue_references) && __cpp_rvalue_references >= 200610) || (defined(_MSC_VER) && _MSC_VER >= 1600)
62 # define PICOJSON_USE_RVALUE_REFERENCE 1
64 # define PICOJSON_USE_RVALUE_REFERENCE 0
70 #ifdef PICOJSON_USE_INT64
71 # define __STDC_FORMAT_MACROS
73 # include <inttypes.h>
77 #ifndef PICOJSON_USE_LOCALE
78 # define PICOJSON_USE_LOCALE 1
80 #if PICOJSON_USE_LOCALE
86 #ifndef PICOJSON_ASSERT
87 # define PICOJSON_ASSERT(e) do { if (! (e)) throw std::runtime_error(#e); } while (0)
91 #define SNPRINTF _snprintf_s
93 #pragma warning(disable : 4244)
94 #pragma warning(disable : 4127)
95 #pragma warning(disable : 4702)
97 #define SNPRINTF snprintf
109 #ifdef PICOJSON_USE_INT64
122 typedef std::vector<value> array;
123 typedef std::map<std::string, value> object;
127 #ifdef PICOJSON_USE_INT64
130 std::string* string_;
139 value(
int type,
bool);
140 explicit value(
bool b);
141 #ifdef PICOJSON_USE_INT64
142 explicit value(int64_t i);
144 explicit value(
double n);
145 explicit value(
const std::string& s);
146 explicit value(
const array& a);
147 explicit value(
const object& o);
148 #if PICOJSON_USE_RVALUE_REFERENCE
149 explicit value(std::string&& s);
150 explicit value(array&& a);
151 explicit value(
object&& o);
153 explicit value(
const char* s);
154 value(
const char* s,
size_t len);
158 #if PICOJSON_USE_RVALUE_REFERENCE
162 void swap(
value& x)
throw();
163 template <
typename T>
bool is()
const;
164 template <
typename T>
const T& get()
const;
165 template <
typename T> T& get();
166 template <
typename T>
void set(
const T &);
167 #if PICOJSON_USE_RVALUE_REFERENCE
168 template <
typename T>
void set(T &&);
170 bool evaluate_as_boolean()
const;
171 const value& get(
size_t idx)
const;
172 const value& get(
const std::string& key)
const;
173 value& get(
size_t idx);
174 value& get(
const std::string& key);
176 bool contains(
size_t idx)
const;
177 bool contains(
const std::string& key)
const;
178 std::string to_str()
const;
179 template <
typename Iter>
void serialize(Iter os,
bool prettify =
false)
const;
180 std::string serialize(
bool prettify =
false)
const;
182 template <
typename T>
value(
const T*);
183 template <
typename Iter>
static void _indent(Iter os,
int indent);
184 template <
typename Iter>
void _serialize(Iter os,
int indent)
const;
185 std::string _serialize(
int indent)
const;
189 typedef value::array array;
190 typedef value::object object;
192 inline value::value() : type_(null_type) {}
194 inline value::value(
int type,
bool) : type_(type) {
196 #define INIT(p, v) case p##type: u_.p = v; break
197 INIT(boolean_,
false);
199 #ifdef PICOJSON_USE_INT64
202 INIT(string_,
new std::string());
203 INIT(array_,
new array());
204 INIT(object_,
new object());
210 inline value::value(
bool b) : type_(boolean_type) {
214 #ifdef PICOJSON_USE_INT64
215 inline value::value(int64_t i) : type_(int64_type) {
220 inline value::value(
double n) : type_(number_type) {
224 #elif __cplusplus>=201103L || !(defined(isnan) && defined(isinf))
225 std::isnan(n) || std::isinf(n)
230 throw std::overflow_error(
"");
235 inline value::value(
const std::string& s) : type_(string_type) {
236 u_.string_ =
new std::string(s);
239 inline value::value(
const array& a) : type_(array_type) {
240 u_.array_ =
new array(a);
243 inline value::value(
const object& o) : type_(object_type) {
244 u_.object_ =
new object(o);
247 #if PICOJSON_USE_RVALUE_REFERENCE
248 inline value::value(std::string&& s) : type_(string_type) {
249 u_.string_ =
new std::string(std::move(s));
252 inline value::value(array&& a) : type_(array_type) {
253 u_.array_ =
new array(std::move(a));
256 inline value::value(
object&& o) : type_(object_type) {
257 u_.object_ =
new object(std::move(o));
261 inline value::value(
const char* s) : type_(string_type) {
262 u_.string_ =
new std::string(s);
265 inline value::value(
const char* s,
size_t len) : type_(string_type) {
266 u_.string_ =
new std::string(s, len);
269 inline void value::clear() {
271 #define DEINIT(p) case p##type: delete u_.p; break
280 inline value::~value() {
284 inline value::value(
const value& x) : type_(x.type_) {
286 #define INIT(p, v) case p##type: u_.p = v; break
287 INIT(string_,
new std::string(*x.u_.string_));
288 INIT(array_,
new array(*x.u_.array_));
289 INIT(object_,
new object(*x.u_.object_));
297 inline value& value::operator=(
const value& x) {
305 #if PICOJSON_USE_RVALUE_REFERENCE
306 inline value::value(value&& x)
throw() : type_(null_type) {
309 inline value& value::operator=(value&& x)
throw() {
314 inline void value::swap(value& x)
throw() {
315 std::swap(type_, x.type_);
319 #define IS(ctype, jtype) \
320 template <> inline bool value::is<ctype>() const { \
321 return type_ == jtype##_type; \
325 #ifdef PICOJSON_USE_INT64
328 IS(std::string,
string)
332 template <>
inline bool value::is<double>()
const {
333 return type_ == number_type
334 #ifdef PICOJSON_USE_INT64
335 || type_ == int64_type
340 #define GET(ctype, var) \
341 template <> inline const ctype& value::get<ctype>() const { \
342 PICOJSON_ASSERT("type mismatch! call is<type>() before get<type>()" \
346 template <> inline ctype& value::get<ctype>() { \
347 PICOJSON_ASSERT("type mismatch! call is<type>() before get<type>()" \
351 GET(
bool, u_.boolean_)
352 GET(std::
string, *u_.string_)
353 GET(array, *u_.array_)
354 GET(
object, *u_.object_)
355 #ifdef PICOJSON_USE_INT64
356 GET(
double, (type_ == int64_type && (
const_cast<value*
>(
this)->type_ = number_type,
const_cast<value*
>(
this)->u_.number_ = u_.int64_), u_.number_))
357 GET(int64_t, u_.int64_)
359 GET(
double, u_.number_)
363 #define SET(ctype, jtype, setter) \
364 template <> inline void value::set<ctype>(const ctype &_val) { \
366 type_ = jtype##_type; \
369 SET(
bool,
boolean, u_.boolean_ = _val;)
370 SET(std::string,
string, u_.string_ =
new std::string(_val);)
371 SET(array, array, u_.array_ =
new array(_val);)
372 SET(
object,
object, u_.object_ =
new object(_val);)
373 SET(
double, number, u_.number_ = _val;)
374 #ifdef PICOJSON_USE_INT64
375 SET(int64_t, int64, u_.int64_ = _val;)
379 #if PICOJSON_USE_RVALUE_REFERENCE
380 #define MOVESET(ctype, jtype, setter) \
381 template <> inline void value::set<ctype>(ctype &&_val) { \
383 type_ = jtype##_type; \
386 MOVESET(std::string,
string, u_.string_ =
new std::string(std::move(_val));)
387 MOVESET(array, array, u_.array_ =
new array(std::move(_val));)
388 MOVESET(
object,
object, u_.object_ =
new object(std::move(_val));)
392 inline bool value::evaluate_as_boolean()
const {
399 return u_.number_ != 0;
400 #ifdef PICOJSON_USE_INT64
402 return u_.int64_ != 0;
405 return ! u_.string_->empty();
411 inline const value& value::get(
size_t idx)
const {
413 PICOJSON_ASSERT(is<array>());
414 return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null;
417 inline value& value::get(
size_t idx) {
419 PICOJSON_ASSERT(is<array>());
420 return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null;
423 inline const value& value::get(
const std::string& key)
const {
425 PICOJSON_ASSERT(is<object>());
426 object::const_iterator i = u_.object_->find(key);
427 return i != u_.object_->end() ? i->second : s_null;
430 inline value& value::get(
const std::string& key) {
432 PICOJSON_ASSERT(is<object>());
433 object::iterator i = u_.object_->find(key);
434 return i != u_.object_->end() ? i->second : s_null;
437 inline bool value::contains(
size_t idx)
const {
438 PICOJSON_ASSERT(is<array>());
439 return idx < u_.array_->size();
442 inline bool value::contains(
const std::string& key)
const {
443 PICOJSON_ASSERT(is<object>());
444 object::const_iterator i = u_.object_->find(key);
445 return i != u_.object_->end();
448 inline std::string value::to_str()
const {
450 case null_type:
return "null";
451 case boolean_type:
return u_.boolean_ ?
"true" :
"false";
452 #ifdef PICOJSON_USE_INT64
454 char buf[
sizeof(
"-9223372036854775808")];
455 SNPRINTF(buf,
sizeof(buf),
"%" PRId64, u_.int64_);
462 SNPRINTF(buf,
sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ?
"%.f" :
"%.17g", u_.number_);
463 #if PICOJSON_USE_LOCALE
464 char *decimal_point = localeconv()->decimal_point;
465 if (strcmp(decimal_point,
".") != 0) {
466 size_t decimal_point_len = strlen(decimal_point);
467 for (
char *p = buf; *p !=
'\0'; ++p) {
468 if (strncmp(p, decimal_point, decimal_point_len) == 0) {
469 return std::string(buf, p) +
"." + (p + decimal_point_len);
476 case string_type:
return *u_.string_;
477 case array_type:
return "array";
478 case object_type:
return "object";
479 default: PICOJSON_ASSERT(0);
484 return std::string();
487 template <
typename Iter>
void copy(
const std::string& s, Iter oi) {
488 std::copy(s.begin(), s.end(), oi);
491 template <
typename Iter>
494 void operator()(
char c) {
496 #define MAP(val, sym) case val: copy(sym, oi); break
507 if (
static_cast<unsigned char>(c) < 0x20 || c == 0x7f) {
509 SNPRINTF(buf,
sizeof(buf),
"\\u%04x", c & 0xff);
510 copy(buf, buf + 6, oi);
519 template <
typename Iter>
void serialize_str(
const std::string& s, Iter oi) {
522 std::for_each(s.begin(), s.end(), process_char);
526 template <
typename Iter>
void value::serialize(Iter oi,
bool prettify)
const {
527 return _serialize(oi, prettify ? 0 : -1);
530 inline std::string value::serialize(
bool prettify)
const {
531 return _serialize(prettify ? 0 : -1);
534 template <
typename Iter>
void value::_indent(Iter oi,
int indent) {
536 for (
int i = 0; i < indent * INDENT_WIDTH; ++i) {
541 template <
typename Iter>
void value::_serialize(Iter oi,
int indent)
const {
544 serialize_str(*u_.string_, oi);
551 for (array::const_iterator i = u_.array_->begin();
552 i != u_.array_->end();
554 if (i != u_.array_->begin()) {
560 i->_serialize(oi, indent);
564 if (! u_.array_->empty()) {
576 for (object::const_iterator i = u_.object_->begin();
577 i != u_.object_->end();
579 if (i != u_.object_->begin()) {
585 serialize_str(i->first, oi);
590 i->second._serialize(oi, indent);
594 if (! u_.object_->empty()) {
610 inline std::string value::_serialize(
int indent)
const {
612 _serialize(std::back_inserter(s), indent);
616 template <
typename Iter>
class input {
622 input(
const Iter& first,
const Iter& last) : cur_(first), end_(last), consumed_(
false), line_(1) {}
643 self->consumed_ =
false;
648 int line()
const {
return line_; }
652 if (! (ch ==
' ' || ch ==
'\t' || ch ==
'\n' || ch ==
'\r')) {
658 bool expect(
int expect) {
660 if (getc() != expect) {
666 bool match(
const std::string& pattern) {
667 for (std::string::const_iterator pi(pattern.begin());
679 template<
typename Iter>
inline int _parse_quadhex(
input<Iter> &in) {
681 for (
int i = 0; i < 4; i++) {
682 if ((hex = in.getc()) == -1) {
685 if (
'0' <= hex && hex <=
'9') {
687 }
else if (
'A' <= hex && hex <=
'F') {
689 }
else if (
'a' <= hex && hex <=
'f') {
695 uni_ch = uni_ch * 16 + hex;
700 template<
typename String,
typename Iter>
inline bool _parse_codepoint(String& out, input<Iter>& in) {
702 if ((uni_ch = _parse_quadhex(in)) == -1) {
705 if (0xd800 <= uni_ch && uni_ch <= 0xdfff) {
706 if (0xdc00 <= uni_ch) {
711 if (in.getc() !=
'\\' || in.getc() !=
'u') {
715 int second = _parse_quadhex(in);
716 if (! (0xdc00 <= second && second <= 0xdfff)) {
719 uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff);
723 out.push_back(uni_ch);
725 if (uni_ch < 0x800) {
726 out.push_back(0xc0 | (uni_ch >> 6));
728 if (uni_ch < 0x10000) {
729 out.push_back(0xe0 | (uni_ch >> 12));
731 out.push_back(0xf0 | (uni_ch >> 18));
732 out.push_back(0x80 | ((uni_ch >> 12) & 0x3f));
734 out.push_back(0x80 | ((uni_ch >> 6) & 0x3f));
736 out.push_back(0x80 | (uni_ch & 0x3f));
741 template<
typename String,
typename Iter>
inline bool _parse_string(String& out, input<Iter>& in) {
747 }
else if (ch ==
'"') {
749 }
else if (ch ==
'\\') {
750 if ((ch = in.getc()) == -1) {
754 #define MAP(sym, val) case sym: out.push_back(val); break
765 if (! _parse_codepoint(out, in)) {
779 template <
typename Context,
typename Iter>
inline bool _parse_array(Context& ctx, input<Iter>& in) {
780 if (! ctx.parse_array_start()) {
784 if (in.expect(
']')) {
785 return ctx.parse_array_stop(idx);
788 if (! ctx.parse_array_item(in, idx)) {
792 }
while (in.expect(
','));
793 return in.expect(
']') && ctx.parse_array_stop(idx);
796 template <
typename Context,
typename Iter>
inline bool _parse_object(Context& ctx, input<Iter>& in) {
797 if (! ctx.parse_object_start()) {
800 if (in.expect(
'}')) {
806 || ! _parse_string(key, in)
807 || ! in.expect(
':')) {
810 if (! ctx.parse_object_item(in, key)) {
813 }
while (in.expect(
','));
814 return in.expect(
'}');
817 template <
typename Iter>
inline std::string _parse_number(input<Iter>& in) {
821 if ((
'0' <= ch && ch <=
'9') || ch ==
'+' || ch ==
'-'
822 || ch ==
'e' || ch ==
'E') {
823 num_str.push_back(ch);
824 }
else if (ch ==
'.') {
825 #if PICOJSON_USE_LOCALE
826 num_str += localeconv()->decimal_point;
828 num_str.push_back(
'.');
838 template <
typename Context,
typename Iter>
inline bool _parse(Context& ctx, input<Iter>& in) {
842 #define IS(ch, text, op) case ch: \
843 if (in.match(text) && op) { \
848 IS(
'n',
"ull", ctx.set_null());
849 IS(
'f',
"alse", ctx.set_bool(
false));
850 IS(
't',
"rue", ctx.set_bool(
true));
853 return ctx.parse_string(in);
855 return _parse_array(ctx, in);
857 return _parse_object(ctx, in);
859 if ((
'0' <= ch && ch <=
'9') || ch ==
'-') {
863 std::string num_str = _parse_number(in);
864 if (num_str.empty()) {
867 #ifdef PICOJSON_USE_INT64
870 intmax_t ival = strtoimax(num_str.c_str(), &endp, 10);
872 && std::numeric_limits<int64_t>::min() <= ival
873 && ival <= std::numeric_limits<int64_t>::max()
874 && endp == num_str.c_str() + num_str.size()) {
880 f = strtod(num_str.c_str(), &endp);
881 if (endp == num_str.c_str() + num_str.size()) {
895 bool set_null() {
return false; }
896 bool set_bool(
bool) {
return false; }
897 #ifdef PICOJSON_USE_INT64
898 bool set_int64(int64_t) {
return false; }
900 bool set_number(
double) {
return false; }
901 template <
typename Iter>
bool parse_string(
input<Iter>&) {
return false; }
902 bool parse_array_start() {
return false; }
903 template <
typename Iter>
bool parse_array_item(
input<Iter>&,
size_t) {
906 bool parse_array_stop(
size_t) {
return false; }
907 bool parse_object_start() {
return false; }
908 template <
typename Iter>
bool parse_object_item(
input<Iter>&,
const std::string&) {
922 bool set_bool(
bool b) {
926 #ifdef PICOJSON_USE_INT64
927 bool set_int64(int64_t i) {
932 bool set_number(
double f) {
936 template<
typename Iter>
bool parse_string(
input<Iter>& in) {
937 *out_ =
value(string_type,
false);
938 return _parse_string(out_->get<std::string>(), in);
940 bool parse_array_start() {
941 *out_ =
value(array_type,
false);
944 template <
typename Iter>
bool parse_array_item(
input<Iter>& in,
size_t) {
945 array& a = out_->get<array>();
946 a.push_back(
value());
948 return _parse(ctx, in);
950 bool parse_array_stop(
size_t) {
return true; }
951 bool parse_object_start() {
952 *out_ =
value(object_type,
false);
955 template <
typename Iter>
bool parse_object_item(
input<Iter>& in,
const std::string& key) {
956 object& o = out_->get<
object>();
958 return _parse(ctx, in);
968 void push_back(
int) {}
972 bool set_null() {
return true; }
973 bool set_bool(
bool) {
return true; }
974 #ifdef PICOJSON_USE_INT64
975 bool set_int64(int64_t) {
return true; }
977 bool set_number(
double) {
return true; }
978 template <
typename Iter>
bool parse_string(input<Iter>& in) {
980 return _parse_string(s, in);
982 bool parse_array_start() {
return true; }
983 template <
typename Iter>
bool parse_array_item(input<Iter>& in,
size_t) {
984 return _parse(*
this, in);
986 bool parse_array_stop(
size_t) {
return true; }
987 bool parse_object_start() {
return true; }
988 template <
typename Iter>
bool parse_object_item(input<Iter>& in,
const std::string&) {
989 return _parse(*
this, in);
992 null_parse_context(
const null_parse_context&);
993 null_parse_context& operator=(
const null_parse_context&);
997 template <
typename Iter>
inline std::string parse(value& out, Iter& pos,
const Iter& last) {
999 pos = parse(out, pos, last, &err);
1003 template <
typename Context,
typename Iter>
inline Iter _parse(Context& ctx,
const Iter& first,
const Iter& last, std::string* err) {
1004 input<Iter> in(first, last);
1005 if (! _parse(ctx, in) && err != NULL) {
1007 SNPRINTF(buf,
sizeof(buf),
"syntax error at line %d near: ", in.line());
1011 if (ch == -1 || ch ==
'\n') {
1013 }
else if (ch >=
' ') {
1021 template <
typename Iter>
inline Iter parse(value& out,
const Iter& first,
const Iter& last, std::string* err) {
1022 default_parse_context ctx(&out);
1023 return _parse(ctx, first, last, err);
1026 inline std::string parse(value& out,
const std::string& s) {
1028 parse(out, s.begin(), s.end(), &err);
1032 inline std::string parse(value& out, std::istream& is) {
1034 parse(out, std::istreambuf_iterator<char>(is.rdbuf()),
1035 std::istreambuf_iterator<char>(), &err);
1040 static std::string s;
1044 inline void set_last_error(
const std::string& s) {
1048 inline const std::string& get_last_error() {
1049 return last_error_t<bool>::s;
1052 inline bool operator==(
const value& x,
const value& y) {
1054 return y.is<
null>();
1055 #define PICOJSON_CMP(type) \
1057 return y.is<type>() && x.get<type>() == y.get<type>()
1059 PICOJSON_CMP(
double);
1060 PICOJSON_CMP(std::string);
1061 PICOJSON_CMP(array);
1062 PICOJSON_CMP(
object);
1071 inline bool operator!=(
const value& x,
const value& y) {
1076 #if !PICOJSON_USE_RVALUE_REFERENCE
1087 picojson::set_last_error(std::string());
1088 std::string err = picojson::parse(x, is);
1089 if (! err.empty()) {
1090 picojson::set_last_error(err);
1091 is.setstate(std::ios::failbit);
1096 inline std::ostream& operator<<(std::ostream& os,
const picojson::value& x)
1098 x.serialize(std::ostream_iterator<char>(os));
1102 #pragma warning(pop)
Definition: picojson.h:913
Definition: picojson.h:893
Definition: picojson.h:965
Definition: picojson.h:120
Definition: picojson.h:1039
Definition: picojson.h:967
Definition: picojson.h:118
Definition: picojson.h:492
Definition: picojson.h:124