44 #if __cplusplus>=201103L
50 # elif defined(__INTEL_COMPILER)
59 #ifdef PICOJSON_USE_INT64
60 # define __STDC_FORMAT_MACROS
62 # include <inttypes.h>
66 #ifndef PICOJSON_USE_LOCALE
67 # define PICOJSON_USE_LOCALE 1
69 #if PICOJSON_USE_LOCALE
75 #ifndef PICOJSON_ASSERT
76 # define PICOJSON_ASSERT(e) do { if (! (e)) throw std::runtime_error(#e); } while (0)
80 #define SNPRINTF _snprintf_s
82 #pragma warning(disable : 4244) // conversion from int to char
83 #pragma warning(disable : 4127) // conditional expression is constant
84 #pragma warning(disable : 4702) // unreachable code
86 #define SNPRINTF snprintf
98 #ifdef PICOJSON_USE_INT64
111 typedef std::vector<value> array;
112 typedef std::map<std::string, value> object;
116 #ifdef PICOJSON_USE_INT64
119 std::string* string_;
129 explicit value(
bool b);
130 #ifdef PICOJSON_USE_INT64
131 explicit value(int64_t i);
133 explicit value(
double n);
134 explicit value(
const std::string& s);
135 explicit value(
const array& a);
136 explicit value(
const object& o);
137 explicit value(
const char* s);
138 value(
const char* s,
size_t len);
143 template <
typename T>
bool is()
const;
144 template <
typename T>
const T&
get()
const;
145 template <
typename T> T&
get();
146 bool evaluate_as_boolean()
const;
147 const value&
get(
size_t idx)
const;
148 const value&
get(
const std::string& key)
const;
149 value&
get(
size_t idx);
150 value&
get(
const std::string& key);
152 bool contains(
size_t idx)
const;
153 bool contains(
const std::string& key)
const;
154 std::string to_str()
const;
155 template <
typename Iter>
void serialize(Iter os,
bool prettify =
false)
const;
156 std::string serialize(
bool prettify =
false)
const;
158 template <
typename T>
value(
const T*);
159 template <
typename Iter>
static void _indent(Iter os,
int indent);
160 template <
typename Iter>
void _serialize(Iter os,
int indent)
const;
161 std::string _serialize(
int indent)
const;
164 typedef value::array array;
165 typedef value::object object;
167 inline value::value() : type_(null_type) {}
169 inline value::value(
int type,
bool) : type_(type) {
171 #define INIT(p, v) case p##type: u_.p = v; break
172 INIT(boolean_,
false);
174 #ifdef PICOJSON_USE_INT64
177 INIT(string_,
new std::string());
178 INIT(array_,
new array());
179 INIT(object_,
new object());
185 inline value::value(
bool b) : type_(boolean_type) {
189 #ifdef PICOJSON_USE_INT64
190 inline value::value(int64_t i) : type_(int64_type) {
195 inline value::value(
double n) : type_(number_type) {
199 #elif __cplusplus>=201103L || !(defined(isnan) && defined(isinf))
200 std::isnan(n) || std::isinf(n)
205 throw std::overflow_error(
"");
210 inline value::value(
const std::string& s) : type_(string_type) {
211 u_.string_ =
new std::string(s);
214 inline value::value(
const array& a) : type_(array_type) {
215 u_.array_ =
new array(a);
218 inline value::value(
const object& o) : type_(object_type) {
219 u_.object_ =
new object(o);
222 inline value::value(
const char* s) : type_(string_type) {
223 u_.string_ =
new std::string(s);
226 inline value::value(
const char* s,
size_t len) : type_(string_type) {
227 u_.string_ =
new std::string(s, len);
230 inline value::~value() {
232 #define DEINIT(p) case p##type: delete u_.p; break
241 inline value::value(
const value& x) : type_(x.type_) {
243 #define INIT(p, v) case p##type: u_.p = v; break
244 INIT(string_,
new std::string(*x.u_.string_));
245 INIT(array_,
new array(*x.u_.array_));
246 INIT(object_,
new object(*x.u_.object_));
254 inline value& value::operator=(
const value& x) {
262 inline void value::swap(value& x) {
263 std::swap(type_, x.type_);
267 #define IS(ctype, jtype) \
268 template <> inline bool value::is<ctype>() const { \
269 return type_ == jtype##_type; \
273 #ifdef PICOJSON_USE_INT64
276 IS(std::string,
string)
280 template <>
inline bool value::is<double>()
const {
281 return type_ == number_type
282 #ifdef PICOJSON_USE_INT64
283 || type_ == int64_type
288 #define GET(ctype, var) \
289 template <> inline const ctype& value::get<ctype>() const { \
290 PICOJSON_ASSERT("type mismatch! call is<type>() before get<type>()" \
294 template <> inline ctype& value::get<ctype>() { \
295 PICOJSON_ASSERT("type mismatch! call is<type>() before get<type>()" \
299 GET(
bool, u_.boolean_)
300 GET(std::
string, *u_.string_)
301 GET(array, *u_.array_)
302 GET(
object, *u_.object_)
303 #ifdef PICOJSON_USE_INT64
304 GET(
double, (type_ == int64_type && (const_cast<value*>(
this)->type_ = number_type, const_cast<value*>(
this)->u_.number_ = u_.int64_), u_.number_))
305 GET(int64_t, u_.int64_)
307 GET(
double, u_.number_)
311 inline bool value::evaluate_as_boolean()
const {
318 return u_.number_ != 0;
320 return ! u_.string_->empty();
326 inline const value& value::get(
size_t idx)
const {
328 PICOJSON_ASSERT(is<array>());
329 return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null;
332 inline value& value::get(
size_t idx) {
334 PICOJSON_ASSERT(is<array>());
335 return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null;
338 inline const value& value::get(
const std::string& key)
const {
340 PICOJSON_ASSERT(is<object>());
341 object::const_iterator i = u_.object_->find(key);
342 return i != u_.object_->end() ? i->second : s_null;
345 inline value& value::get(
const std::string& key) {
347 PICOJSON_ASSERT(is<object>());
348 object::iterator i = u_.object_->find(key);
349 return i != u_.object_->end() ? i->second : s_null;
352 inline bool value::contains(
size_t idx)
const {
353 PICOJSON_ASSERT(is<array>());
354 return idx < u_.array_->size();
357 inline bool value::contains(
const std::string& key)
const {
358 PICOJSON_ASSERT(is<object>());
359 object::const_iterator i = u_.object_->find(key);
360 return i != u_.object_->end();
363 inline std::string value::to_str()
const {
365 case null_type:
return "null";
366 case boolean_type:
return u_.boolean_ ?
"true" :
"false";
367 #ifdef PICOJSON_USE_INT64
369 char buf[
sizeof(
"-9223372036854775808")];
370 SNPRINTF(buf,
sizeof(buf),
"%" PRId64, u_.int64_);
377 SNPRINTF(buf,
sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ?
"%.f" :
"%.17g", u_.number_);
378 #if PICOJSON_USE_LOCALE
379 char *decimal_point = localeconv()->decimal_point;
380 if (strcmp(decimal_point,
".") != 0) {
381 size_t decimal_point_len = strlen(decimal_point);
382 for (
char *p = buf; *p !=
'\0'; ++p) {
383 if (strncmp(p, decimal_point, decimal_point_len) == 0) {
384 return std::string(buf, p) +
"." + (p + decimal_point_len);
391 case string_type:
return *u_.string_;
394 for(
auto i : get<array>()) {
395 if(i.is<std::string>())
396 output +=
"\"" + i.to_str() +
"\",";
398 output+= i.to_str() +
",";
400 output = output.substr(0, output.length() - 1);
401 output =
"[" + output +
"]";
406 for(
auto i : get<object>()) {
407 output +=
"\""+ i.first +
"\":" + i.second.to_str() +
",";
409 output = output.substr(0, output.length() - 1);
410 output =
"{" + output +
"}";
413 default: PICOJSON_ASSERT(0);
418 return std::string();
421 template <
typename Iter>
void copy(
const std::string& s, Iter oi) {
422 std::copy(s.begin(), s.end(), oi);
425 template <
typename Iter>
void serialize_str(
const std::string& s, Iter oi) {
427 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
429 #define MAP(val, sym) case val: copy(sym, oi); break
440 if (static_cast<unsigned char>(*i) < 0x20 || *i == 0x7f) {
442 SNPRINTF(buf,
sizeof(buf),
"\\u%04x", *i & 0xff);
443 copy(buf, buf + 6, oi);
453 template <
typename Iter>
void value::serialize(Iter oi,
bool prettify)
const {
454 return _serialize(oi, prettify ? 0 : -1);
457 inline std::string value::serialize(
bool prettify)
const {
458 return _serialize(prettify ? 0 : -1);
461 template <
typename Iter>
void value::_indent(Iter oi,
int indent) {
463 for (
int i = 0; i < indent * INDENT_WIDTH; ++i) {
468 template <
typename Iter>
void value::_serialize(Iter oi,
int indent)
const {
471 serialize_str(*u_.string_, oi);
478 for (array::const_iterator i = u_.array_->begin();
479 i != u_.array_->end();
481 if (i != u_.array_->begin()) {
487 i->_serialize(oi, indent);
491 if (! u_.array_->empty()) {
503 for (object::const_iterator i = u_.object_->begin();
504 i != u_.object_->end();
506 if (i != u_.object_->begin()) {
512 serialize_str(i->first, oi);
517 i->second._serialize(oi, indent);
521 if (! u_.object_->empty()) {
537 inline std::string value::_serialize(
int indent)
const {
539 _serialize(std::back_inserter(s), indent);
543 template <
typename Iter>
class input {
550 input(
const Iter& first,
const Iter& last) : cur_(first), end_(last), last_ch_(-1), ungot_(
false), line_(1) {}
560 if (last_ch_ ==
'\n') {
563 last_ch_ = *cur_ & 0xff;
568 if (last_ch_ != -1) {
569 PICOJSON_ASSERT(! ungot_);
573 Iter cur()
const {
return cur_; }
574 int line()
const {
return line_; }
578 if (! (ch ==
' ' || ch ==
'\t' || ch ==
'\n' || ch ==
'\r')) {
584 bool expect(
int expect) {
586 if (getc() != expect) {
592 bool match(
const std::string& pattern) {
593 for (std::string::const_iterator pi(pattern.begin());
605 template<
typename Iter>
inline int _parse_quadhex(
input<Iter> &in) {
607 for (
int i = 0; i < 4; i++) {
608 if ((hex = in.getc()) == -1) {
611 if (
'0' <= hex && hex <=
'9') {
613 }
else if (
'A' <= hex && hex <=
'F') {
615 }
else if (
'a' <= hex && hex <=
'f') {
621 uni_ch = uni_ch * 16 + hex;
626 template<
typename String,
typename Iter>
inline bool _parse_codepoint(String& out, input<Iter>& in) {
628 if ((uni_ch = _parse_quadhex(in)) == -1) {
631 if (0xd800 <= uni_ch && uni_ch <= 0xdfff) {
632 if (0xdc00 <= uni_ch) {
637 if (in.getc() !=
'\\' || in.getc() !=
'u') {
641 int second = _parse_quadhex(in);
642 if (! (0xdc00 <= second && second <= 0xdfff)) {
645 uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff);
649 out.push_back(uni_ch);
651 if (uni_ch < 0x800) {
652 out.push_back(0xc0 | (uni_ch >> 6));
654 if (uni_ch < 0x10000) {
655 out.push_back(0xe0 | (uni_ch >> 12));
657 out.push_back(0xf0 | (uni_ch >> 18));
658 out.push_back(0x80 | ((uni_ch >> 12) & 0x3f));
660 out.push_back(0x80 | ((uni_ch >> 6) & 0x3f));
662 out.push_back(0x80 | (uni_ch & 0x3f));
667 template<
typename String,
typename Iter>
inline bool _parse_string(String& out, input<Iter>& in) {
673 }
else if (ch ==
'"') {
675 }
else if (ch ==
'\\') {
676 if ((ch = in.getc()) == -1) {
680 #define MAP(sym, val) case sym: out.push_back(val); break
691 if (! _parse_codepoint(out, in)) {
705 template <
typename Context,
typename Iter>
inline bool _parse_array(Context& ctx, input<Iter>& in) {
706 if (! ctx.parse_array_start()) {
710 if (in.expect(
']')) {
711 return ctx.parse_array_stop(idx);
714 if (! ctx.parse_array_item(in, idx)) {
718 }
while (in.expect(
','));
719 return in.expect(
']') && ctx.parse_array_stop(idx);
722 template <
typename Context,
typename Iter>
inline bool _parse_object(Context& ctx, input<Iter>& in) {
723 if (! ctx.parse_object_start()) {
726 if (in.expect(
'}')) {
732 || ! _parse_string(key, in)
733 || ! in.expect(
':')) {
736 if (! ctx.parse_object_item(in, key)) {
739 }
while (in.expect(
','));
740 return in.expect(
'}');
743 template <
typename Iter>
inline std::string _parse_number(input<Iter>& in) {
747 if ((
'0' <= ch && ch <=
'9') || ch ==
'+' || ch ==
'-'
748 || ch ==
'e' || ch ==
'E') {
749 num_str.push_back(ch);
750 }
else if (ch ==
'.') {
751 #if PICOJSON_USE_LOCALE
752 num_str += localeconv()->decimal_point;
754 num_str.push_back(
'.');
764 template <
typename Context,
typename Iter>
inline bool _parse(Context& ctx, input<Iter>& in) {
768 #define IS(ch, text, op) case ch: \
769 if (in.match(text) && op) { \
774 IS(
'n',
"ull", ctx.set_null());
775 IS(
'f',
"alse", ctx.set_bool(
false));
776 IS(
't',
"rue", ctx.set_bool(
true));
779 return ctx.parse_string(in);
781 return _parse_array(ctx, in);
783 return _parse_object(ctx, in);
785 if ((
'0' <= ch && ch <=
'9') || ch ==
'-') {
789 std::string num_str = _parse_number(in);
790 if (num_str.empty()) {
793 #ifdef PICOJSON_USE_INT64
796 intmax_t ival = strtoimax(num_str.c_str(), &endp, 10);
798 && std::numeric_limits<int64_t>::min() <= ival
799 && ival <= std::numeric_limits<int64_t>::max()
800 && endp == num_str.c_str() + num_str.size()) {
806 f = strtod(num_str.c_str(), &endp);
807 if (endp == num_str.c_str() + num_str.size()) {
821 bool set_null() {
return false; }
822 bool set_bool(
bool) {
return false; }
823 #ifdef PICOJSON_USE_INT64
824 bool set_int64(int64_t) {
return false; }
826 bool set_number(
double) {
return false; }
827 template <
typename Iter>
bool parse_string(
input<Iter>&) {
return false; }
828 bool parse_array_start() {
return false; }
829 template <
typename Iter>
bool parse_array_item(
input<Iter>&,
size_t) {
832 bool parse_array_stop(
size_t) {
return false; }
833 bool parse_object_start() {
return false; }
834 template <
typename Iter>
bool parse_object_item(
input<Iter>&,
const std::string&) {
848 bool set_bool(
bool b) {
852 #ifdef PICOJSON_USE_INT64
853 bool set_int64(int64_t i) {
858 bool set_number(
double f) {
862 template<
typename Iter>
bool parse_string(
input<Iter>& in) {
863 *out_ =
value(string_type,
false);
864 return _parse_string(out_->get<std::string>(), in);
866 bool parse_array_start() {
867 *out_ =
value(array_type,
false);
870 template <
typename Iter>
bool parse_array_item(
input<Iter>& in,
size_t) {
871 array& a = out_->get<array>();
872 a.push_back(
value());
874 return _parse(ctx, in);
876 bool parse_array_stop(
size_t) {
return true; }
877 bool parse_object_start() {
878 *out_ =
value(object_type,
false);
881 template <
typename Iter>
bool parse_object_item(
input<Iter>& in,
const std::string& key) {
882 object& o = out_->get<
object>();
884 return _parse(ctx, in);
894 void push_back(
int) {}
898 bool set_null() {
return true; }
899 bool set_bool(
bool) {
return true; }
900 #ifdef PICOJSON_USE_INT64
901 bool set_int64(int64_t) {
return true; }
903 bool set_number(
double) {
return true; }
904 template <
typename Iter>
bool parse_string(input<Iter>& in) {
906 return _parse_string(s, in);
908 bool parse_array_start() {
return true; }
909 template <
typename Iter>
bool parse_array_item(input<Iter>& in,
size_t) {
910 return _parse(*
this, in);
912 bool parse_array_stop(
size_t) {
return true; }
913 bool parse_object_start() {
return true; }
914 template <
typename Iter>
bool parse_object_item(input<Iter>& in,
const std::string&) {
915 return _parse(*
this, in);
918 null_parse_context(
const null_parse_context&);
919 null_parse_context& operator=(
const null_parse_context&);
923 template <
typename Iter>
inline std::string parse(value& out, Iter& pos,
const Iter& last) {
925 pos = parse(out, pos, last, &err);
929 template <
typename Context,
typename Iter>
inline Iter _parse(Context& ctx,
const Iter& first,
const Iter& last, std::string* err) {
930 input<Iter> in(first, last);
931 if (! _parse(ctx, in) && err != NULL) {
933 SNPRINTF(buf,
sizeof(buf),
"syntax error at line %d near: ", in.line());
937 if (ch == -1 || ch ==
'\n') {
939 }
else if (ch >=
' ') {
947 template <
typename Iter>
inline Iter parse(value& out,
const Iter& first,
const Iter& last, std::string* err) {
948 default_parse_context ctx(&out);
949 return _parse(ctx, first, last, err);
952 inline std::string parse(value& out,
const std::string& s) {
954 parse(out, s.begin(), s.end(), &err);
958 inline std::string parse(value& out, std::istream& is) {
960 parse(out, std::istreambuf_iterator<char>(is.rdbuf()),
961 std::istreambuf_iterator<char>(), &err);
966 static std::string s;
970 inline void set_last_error(
const std::string& s) {
974 inline const std::string& get_last_error() {
975 return last_error_t<bool>::s;
978 inline bool operator==(
const value& x,
const value& y) {
981 #define PICOJSON_CMP(type) \
983 return y.is<type>() && x.get<type>() == y.get<type>()
985 PICOJSON_CMP(
double);
986 PICOJSON_CMP(std::string);
988 PICOJSON_CMP(
object);
997 inline bool operator!=(
const value& x,
const value& y) {
1011 picojson::set_last_error(std::string());
1012 std::string err = picojson::parse(x, is);
1013 if (! err.empty()) {
1014 picojson::set_last_error(err);
1015 is.setstate(std::ios::failbit);
1020 inline std::ostream& operator<<(std::ostream& os,
const picojson::value& x)
1022 x.serialize(std::ostream_iterator<char>(os));
1026 #pragma warning(pop)