45#if __cplusplus>=201103L
51# 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#ifndef PICOJSON_USE_INT64
71#define PICOJSON_USE_INT64 1
73#ifdef PICOJSON_USE_INT64
74# define __STDC_FORMAT_MACROS
80#ifndef PICOJSON_USE_LOCALE
81# define PICOJSON_USE_LOCALE 0
83#if PICOJSON_USE_LOCALE
91#ifndef PICOJSON_USE_ASSERT
92# define PICOJSON_USE_ASSERT 0
94#if PICOJSON_USE_ASSERT
95# define PICOJSON_ASSERT(e) do { if (! (e)) throw std::runtime_error(#e); } while (0)
97# define PICOJSON_ASSERT(e)
101 #define SNPRINTF _snprintf_s
102 #pragma warning(push)
103 #pragma warning(disable : 4244)
104 #pragma warning(disable : 4127)
105 #pragma warning(disable : 4702)
106 #pragma warning(disable : 4706)
108 #define SNPRINTF snprintf
124#ifdef PICOJSON_USE_INT64
137 typedef std::vector<value> array;
138 typedef std::map<std::string, value> object;
142#ifdef PICOJSON_USE_INT64
145 std::string* string_;
154 value(
int type,
bool);
155 explicit value(
bool b);
156#ifdef PICOJSON_USE_INT64
157 explicit value(int64_t i);
159 explicit value(
double n);
160 explicit value(
const std::string& s);
161 explicit value(
const array& a);
162 explicit value(
const object& o);
163 explicit value(
const char* s);
164 value(
const char* s,
size_t len);
166 value(
const value& x);
167 value& operator=(
const value& x);
168#if PICOJSON_USE_RVALUE_REFERENCE
169 value(value&& x)
throw();
170 value& operator=(value&& x)
throw();
172 void swap(value& x)
throw();
173 template <
typename T>
bool is()
const;
174 template <
typename T>
const T& get()
const;
175 template <
typename T> T& get();
176 bool evaluate_as_boolean()
const;
177 const value& get(
size_t idx)
const;
178 const value& get(
const std::string& key)
const;
179 value& get(
size_t idx);
180 value& get(
const std::string& key);
182 bool contains(
size_t idx)
const;
183 bool contains(
const std::string& key)
const;
184 std::string to_str()
const;
185 template <
typename Iter>
void serialize(Iter os,
bool prettify =
false)
const;
186 std::string serialize(
bool prettify =
false)
const;
188 template <
typename T> value(
const T*);
189 template <
typename Iter>
static void _indent(Iter os,
int indent);
190 template <
typename Iter>
void _serialize(Iter os,
int indent)
const;
191 std::string _serialize(
int indent)
const;
194 typedef value::array array;
195 typedef value::object object;
197 inline value::value() : type_(null_type) {}
199 inline value::value(
int type,
bool) : type_(type) {
201#define INIT(p, v) case p##type: u_.p = v; break
202 INIT(boolean_,
false);
204#ifdef PICOJSON_USE_INT64
207 INIT(string_,
new std::string());
208 INIT(array_,
new array());
209 INIT(object_,
new object());
215 inline value::value(
bool b) : type_(boolean_type) {
219#ifdef PICOJSON_USE_INT64
220 inline value::value(int64_t i) : type_(int64_type) {
225 inline value::value(
double n) : type_(number_type) {
229#elif __cplusplus>=201103L || !(defined(isnan) && defined(isinf))
230 std::isnan(n) || std::isinf(n)
240 inline value::value(
const std::string& s) : type_(string_type) {
241 u_.string_ =
new std::string(s);
244 inline value::value(
const array& a) : type_(array_type) {
245 u_.array_ =
new array(a);
248 inline value::value(
const object& o) : type_(object_type) {
249 u_.object_ =
new object(o);
252 inline value::value(
const char* s) : type_(string_type) {
253 u_.string_ =
new std::string(s);
256 inline value::value(
const char* s,
size_t len) : type_(string_type) {
257 u_.string_ =
new std::string(s, len);
260 inline value::~value() {
262#define DEINIT(p) case p##type: delete u_.p; break
271 inline value::value(
const value& x) : type_(x.type_) {
273#define INIT(p, v) case p##type: u_.p = v; break
274 INIT(string_,
new std::string(*x.u_.string_));
275 INIT(array_,
new array(*x.u_.array_));
276 INIT(object_,
new object(*x.u_.object_));
284 inline value& value::operator=(
const value& x) {
292#if PICOJSON_USE_RVALUE_REFERENCE
293 inline value::value(value&& x)
throw() : type_(null_type) {
296 inline value& value::operator=(value&& x)
throw() {
301 inline void value::swap(value& x)
throw() {
302 std::swap(type_, x.type_);
306#define IS(ctype, jtype) \
307 template <> inline bool value::is<ctype>() const { \
308 return type_ == jtype##_type; \
312#ifdef PICOJSON_USE_INT64
315 IS(std::string,
string)
319 template <>
inline bool value::is<double>()
const {
320 return type_ == number_type
321#ifdef PICOJSON_USE_INT64
322 || type_ == int64_type
327#define GET(ctype, var) \
328 template <> inline const ctype& value::get<ctype>() const { \
329 PICOJSON_ASSERT("type mismatch! call is<type>() before get<type>()" \
333 template <> inline ctype& value::get<ctype>() { \
334 PICOJSON_ASSERT("type mismatch! call is<type>() before get<type>()" \
338 GET(
bool, u_.boolean_)
339 GET(std::
string, *u_.string_)
340 GET(array, *u_.array_)
341 GET(
object, *u_.object_)
342#ifdef PICOJSON_USE_INT64
343 GET(
double, (type_ == int64_type && (
const_cast<value*
>(
this)->type_ = number_type,
const_cast<value*
>(
this)->u_.number_ = u_.int64_), u_.number_))
344 GET(int64_t, u_.int64_)
346 GET(
double, u_.number_)
350 inline bool value::evaluate_as_boolean()
const {
357 return u_.number_ != 0;
358#ifdef PICOJSON_USE_INT64
360 return u_.int64_ != 0;
363 return ! u_.string_->empty();
369 inline const value& value::get(
size_t idx)
const {
371 PICOJSON_ASSERT(is<array>());
372 return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null;
375 inline value& value::get(
size_t idx) {
377 PICOJSON_ASSERT(is<array>());
378 return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null;
381 inline const value& value::get(
const std::string& key)
const {
383 PICOJSON_ASSERT(is<object>());
384 object::const_iterator i = u_.object_->find(key);
385 return i != u_.object_->end() ? i->second : s_null;
388 inline value& value::get(
const std::string& key) {
390 PICOJSON_ASSERT(is<object>());
391 object::iterator i = u_.object_->find(key);
392 return i != u_.object_->end() ? i->second : s_null;
395 inline bool value::contains(
size_t idx)
const {
396 PICOJSON_ASSERT(is<array>());
397 return idx < u_.array_->size();
400 inline bool value::contains(
const std::string& key)
const {
401 PICOJSON_ASSERT(is<object>());
402 object::const_iterator i = u_.object_->find(key);
403 return i != u_.object_->end();
408 inline std::string value::to_str()
const {
410 case null_type:
return "null";
411 case boolean_type:
return u_.boolean_ ?
"true" :
"false";
412#ifdef PICOJSON_USE_INT64
414 char buf[
sizeof(
"-9223372036854775808")];
415 SNPRINTF(buf,
sizeof(buf),
"%" PRId64, u_.int64_);
422 SNPRINTF(buf,
sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ?
"%.f" :
"%.17g", u_.number_);
423#if PICOJSON_USE_LOCALE
424 char *decimal_point = localeconv()->decimal_point;
425 if (strcmp(decimal_point,
".") != 0) {
426 size_t decimal_point_len = strlen(decimal_point);
427 for (
char *p = buf; *p !=
'\0'; ++p) {
428 if (strncmp(p, decimal_point, decimal_point_len) == 0) {
429 return std::string(buf, p) +
"." + (p + decimal_point_len);
436 case string_type:
return *u_.string_;
437 case array_type:
return "array";
438 case object_type:
return "object";
439 default: PICOJSON_ASSERT(0);
444 return std::string();
447 template <
typename Iter>
void copy(
const std::string& s, Iter oi) {
448 std::copy(s.begin(), s.end(), oi);
451 template <
typename Iter>
452 struct serialize_str_char {
454 void operator()(
char c) {
456#define MAP(val, sym) case val: copy(sym, oi); break
467 if (
static_cast<unsigned char>(c) < 0x20 || c == 0x7f) {
469 SNPRINTF(buf,
sizeof(buf),
"\\u%04x", c & 0xff);
470 copy(buf, buf + 6, oi);
479 template <
typename Iter>
void serialize_str(
const std::string& s, Iter oi) {
481 serialize_str_char<Iter> process_char = { oi };
482 std::for_each(s.begin(), s.end(), process_char);
486 template <
typename Iter>
void value::serialize(Iter oi,
bool prettify)
const {
487 return _serialize(oi, prettify ? 0 : -1);
490 inline std::string value::serialize(
bool prettify)
const {
491 return _serialize(prettify ? 0 : -1);
494 template <
typename Iter>
void value::_indent(Iter oi,
int indent) {
496 for (
int i = 0; i < indent * INDENT_WIDTH; ++i) {
501 template <
typename Iter>
void value::_serialize(Iter oi,
int indent)
const {
504 serialize_str(*u_.string_, oi);
511 for (array::const_iterator i = u_.array_->begin();
512 i != u_.array_->end();
514 if (i != u_.array_->begin()) {
520 i->_serialize(oi, indent);
524 if (! u_.array_->empty()) {
536 for (object::const_iterator i = u_.object_->begin();
537 i != u_.object_->end();
539 if (i != u_.object_->begin()) {
545 serialize_str(i->first, oi);
550 i->second._serialize(oi, indent);
554 if (! u_.object_->empty()) {
570 inline std::string value::_serialize(
int indent)
const {
572 _serialize(std::back_inserter(s), indent);
576 template <
typename Iter>
class input {
582 input(
const Iter& first,
const Iter& last) : cur_(first), end_(last), consumed_(false), line_(1) {}
602 input<Iter> *self =
const_cast<input<Iter>*
>(
this);
603 self->consumed_ =
false;
608 int line()
const {
return line_; }
612 if (! (ch ==
' ' || ch ==
'\t' || ch ==
'\n' || ch ==
'\r')) {
618 bool expect(
int expect) {
620 if (getc() != expect) {
626 bool match(
const std::string& pattern) {
627 for (std::string::const_iterator pi(pattern.begin());
639 template<
typename Iter>
inline int _parse_quadhex(input<Iter> &in) {
641 for (
int i = 0; i < 4; i++) {
642 if ((hex = in.getc()) == -1) {
645 if (
'0' <= hex && hex <=
'9') {
647 }
else if (
'A' <= hex && hex <=
'F') {
649 }
else if (
'a' <= hex && hex <=
'f') {
655 uni_ch = uni_ch * 16 + hex;
660 template<
typename String,
typename Iter>
inline bool _parse_codepoint(String& out, input<Iter>& in) {
662 if ((uni_ch = _parse_quadhex(in)) == -1) {
665 if (0xd800 <= uni_ch && uni_ch <= 0xdfff) {
666 if (0xdc00 <= uni_ch) {
671 if (in.getc() !=
'\\' || in.getc() !=
'u') {
675 int second = _parse_quadhex(in);
676 if (! (0xdc00 <= second && second <= 0xdfff)) {
679 uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff);
683 out.push_back(uni_ch);
685 if (uni_ch < 0x800) {
686 out.push_back(0xc0 | (uni_ch >> 6));
688 if (uni_ch < 0x10000) {
689 out.push_back(0xe0 | (uni_ch >> 12));
691 out.push_back(0xf0 | (uni_ch >> 18));
692 out.push_back(0x80 | ((uni_ch >> 12) & 0x3f));
694 out.push_back(0x80 | ((uni_ch >> 6) & 0x3f));
696 out.push_back(0x80 | (uni_ch & 0x3f));
701 template<
typename String,
typename Iter>
inline bool _parse_string(String& out, input<Iter>& in) {
707 }
else if (ch ==
'"') {
709 }
else if (ch ==
'\\') {
710 if ((ch = in.getc()) == -1) {
714#define MAP(sym, val) case sym: out.push_back(val); break
725 if (! _parse_codepoint(out, in)) {
739 template <
typename Context,
typename Iter>
inline bool _parse_array(Context& ctx, input<Iter>& in) {
740 if (! ctx.parse_array_start()) {
744 if (in.expect(
']')) {
745 return ctx.parse_array_stop(idx);
748 if (! ctx.parse_array_item(in, idx)) {
752 }
while (in.expect(
','));
753 return in.expect(
']') && ctx.parse_array_stop(idx);
756 template <
typename Context,
typename Iter>
inline bool _parse_object(Context& ctx, input<Iter>& in) {
757 if (! ctx.parse_object_start()) {
760 if (in.expect(
'}')) {
766 || ! _parse_string(key, in)
767 || ! in.expect(
':')) {
770 if (! ctx.parse_object_item(in, key)) {
773 }
while (in.expect(
','));
774 return in.expect(
'}');
777 template <
typename Iter>
inline std::string _parse_number(input<Iter>& in) {
781 if ((
'0' <= ch && ch <=
'9') || ch ==
'+' || ch ==
'-'
782 || ch ==
'e' || ch ==
'E') {
783 num_str.push_back(ch);
784 }
else if (ch ==
'.') {
785#if PICOJSON_USE_LOCALE
788 num_str.push_back(
'.');
798 template <
typename Context,
typename Iter>
inline bool _parse(Context& ctx, input<Iter>& in) {
802#define IS(ch, text, op) case ch: \
803 if (in.match(text) && op) { \
808 IS(
'n',
"ull", ctx.set_null());
809 IS(
'f',
"alse", ctx.set_bool(
false));
810 IS(
't',
"rue", ctx.set_bool(
true));
813 return ctx.parse_string(in);
815 return _parse_array(ctx, in);
817 return _parse_object(ctx, in);
819 if ((
'0' <= ch && ch <=
'9') || ch ==
'-') {
823 std::string num_str = _parse_number(in);
824 if (num_str.empty()) {
827#ifdef PICOJSON_USE_INT64
830 intmax_t ival = strtoimax(num_str.c_str(), &endp, 10);
834 && endp == num_str.c_str() + num_str.size()) {
840 f = strtod(num_str.c_str(), &endp);
841 if (endp == num_str.c_str() + num_str.size()) {
853 class deny_parse_context {
855 bool set_null() {
return false; }
856 bool set_bool(
bool) {
return false; }
857#ifdef PICOJSON_USE_INT64
858 bool set_int64(int64_t) {
return false; }
860 bool set_number(
double) {
return false; }
861 template <
typename Iter>
bool parse_string(input<Iter>&) {
return false; }
862 bool parse_array_start() {
return false; }
863 template <
typename Iter>
bool parse_array_item(input<Iter>&,
size_t) {
866 bool parse_array_stop(
size_t) {
return false; }
867 bool parse_object_start() {
return false; }
868 template <
typename Iter>
bool parse_object_item(input<Iter>&,
const std::string&) {
873 class default_parse_context {
877 default_parse_context(value* out) : out_(out) {}
882 bool set_bool(
bool b) {
886#ifdef PICOJSON_USE_INT64
887 bool set_int64(int64_t i) {
892 bool set_number(
double f) {
896 template<
typename Iter>
bool parse_string(input<Iter>& in) {
897 *out_ = value(string_type,
false);
898 return _parse_string(out_->get<std::string>(), in);
900 bool parse_array_start() {
901 *out_ = value(array_type,
false);
904 template <
typename Iter>
bool parse_array_item(input<Iter>& in,
size_t) {
905 array& a = out_->get<array>();
906 a.push_back(value());
907 default_parse_context ctx(&a.back());
908 return _parse(ctx, in);
910 bool parse_array_stop(
size_t) {
return true; }
911 bool parse_object_start() {
912 *out_ = value(object_type,
false);
915 template <
typename Iter>
bool parse_object_item(input<Iter>& in,
const std::string& key) {
916 object& o = out_->get<
object>();
917 default_parse_context ctx(&o[key]);
918 return _parse(ctx, in);
921 default_parse_context(
const default_parse_context&);
922 default_parse_context& operator=(
const default_parse_context&);
925 class null_parse_context {
928 void push_back(
int) {}
931 null_parse_context() {}
932 bool set_null() {
return true; }
933 bool set_bool(
bool) {
return true; }
934#ifdef PICOJSON_USE_INT64
935 bool set_int64(int64_t) {
return true; }
937 bool set_number(
double) {
return true; }
938 template <
typename Iter>
bool parse_string(input<Iter>& in) {
940 return _parse_string(s, in);
942 bool parse_array_start() {
return true; }
943 template <
typename Iter>
bool parse_array_item(input<Iter>& in,
size_t) {
944 return _parse(*
this, in);
946 bool parse_array_stop(
size_t) {
return true; }
947 bool parse_object_start() {
return true; }
948 template <
typename Iter>
bool parse_object_item(input<Iter>& in,
const std::string&) {
949 return _parse(*
this, in);
952 null_parse_context(
const null_parse_context&);
953 null_parse_context& operator=(
const null_parse_context&);
957 template <
typename Iter>
inline std::string parse(value& out, Iter& pos,
const Iter& last) {
959 pos = parse(out, pos, last, &err);
963 template <
typename Context,
typename Iter>
inline Iter _parse(Context& ctx,
const Iter& first,
const Iter& last, std::string* err) {
964 input<Iter> in(first, last);
965 if (! _parse(ctx, in) && err != NULL) {
967 SNPRINTF(buf,
sizeof(buf),
"syntax error at line %d near: ", in.line());
971 if (ch == -1 || ch ==
'\n') {
973 }
else if (ch >=
' ') {
981 template <
typename Iter>
inline Iter parse(value& out,
const Iter& first,
const Iter& last, std::string* err) {
982 default_parse_context ctx(&out);
983 return _parse(ctx, first, last, err);
986 inline std::string parse(value& out,
const std::string& s) {
988 parse(out, s.begin(), s.end(), &err);
992 inline std::string parse(value& out, std::istream& is) {
994 parse(out, std::istreambuf_iterator<char>(is.rdbuf()),
995 std::istreambuf_iterator<char>(), &err);
999 template <
typename T>
struct last_error_t {
1000 static std::string s;
1002 template <
typename T> std::string last_error_t<T>::s;
1004 inline void set_last_error(
const std::string& s) {
1005 last_error_t<bool>::s = s;
1008 inline const std::string& get_last_error() {
1009 return last_error_t<bool>::s;
1012 inline bool operator==(
const value& x,
const value& y) {
1014 return y.is<null>();
1015#define PICOJSON_CMP(type) \
1017 return y.is<type>() && x.get<type>() == y.get<type>()
1019 PICOJSON_CMP(
double);
1020 PICOJSON_CMP(std::string);
1021 PICOJSON_CMP(array);
1022 PICOJSON_CMP(
object);
1031 inline bool operator!=(
const value& x,
const value& y) {
1036#if !PICOJSON_USE_RVALUE_REFERENCE
1038 template<>
inline void swap(picojson::value& x, picojson::value& y)
1045inline std::istream& operator>>(std::istream& is, picojson::value& x)
1047 picojson::set_last_error(std::string());
1048 std::string err = picojson::parse(x, is);
1049 if (! err.empty()) {
1050 picojson::set_last_error(err);
1051 is.setstate(std::ios::failbit);
1056inline std::ostream& operator<<(std::ostream& os,
const picojson::value& x)
1058 x.serialize(std::ostream_iterator<char>(os));
1062 #pragma warning(pop)