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 66 #endif//PICOJSON_USE_RVALUE_REFERENCE 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) // conversion from int to char 94 #pragma warning(disable : 4127) // conditional expression is constant 95 #pragma warning(disable : 4702) // unreachable code 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 explicit value(
const char* s);
149 value(
const char* s,
size_t len);
153 #if PICOJSON_USE_RVALUE_REFERENCE 157 void swap(
value& x)
throw();
158 template <
typename T>
bool is()
const;
159 template <
typename T>
const T&
get()
const;
160 template <
typename T> T&
get();
161 template <
typename T>
void set(
const T &);
162 #if PICOJSON_USE_RVALUE_REFERENCE 163 template <
typename T>
void set(T &&);
165 bool evaluate_as_boolean()
const;
166 const value&
get(
size_t idx)
const;
167 const value&
get(
const std::string& key)
const;
168 value&
get(
size_t idx);
169 value&
get(
const std::string& key);
171 bool contains(
size_t idx)
const;
172 bool contains(
const std::string& key)
const;
173 std::string to_str()
const;
174 template <
typename Iter>
void serialize(Iter os,
bool prettify =
false)
const;
175 std::string serialize(
bool prettify =
false)
const;
177 template <
typename T>
value(
const T*);
178 template <
typename Iter>
static void _indent(Iter os,
int indent);
179 template <
typename Iter>
void _serialize(Iter os,
int indent)
const;
180 std::string _serialize(
int indent)
const;
184 typedef value::array array;
185 typedef value::object object;
187 inline value::value() : type_(null_type) {}
189 inline value::value(
int type,
bool) : type_(type) {
191 #define INIT(p, v) case p##type: u_.p = v; break 192 INIT(boolean_,
false);
194 #ifdef PICOJSON_USE_INT64 197 INIT(string_,
new std::string());
198 INIT(array_,
new array());
199 INIT(object_,
new object());
205 inline value::value(
bool b) : type_(boolean_type) {
209 #ifdef PICOJSON_USE_INT64 210 inline value::value(int64_t i) : type_(int64_type) {
215 inline value::value(
double n) : type_(number_type) {
219 #elif __cplusplus>=201103L || !(defined(isnan) && defined(isinf))
220 std::isnan(n) || std::isinf(n)
225 throw std::overflow_error(
"");
230 inline value::value(
const std::string& s) : type_(string_type) {
231 u_.string_ =
new std::string(s);
234 inline value::value(
const array& a) : type_(array_type) {
235 u_.array_ =
new array(a);
238 inline value::value(
const object& o) : type_(object_type) {
239 u_.object_ =
new object(o);
242 inline value::value(
const char* s) : type_(string_type) {
243 u_.string_ =
new std::string(s);
246 inline value::value(
const char* s,
size_t len) : type_(string_type) {
247 u_.string_ =
new std::string(s, len);
250 inline void value::clear() {
252 #define DEINIT(p) case p##type: delete u_.p; break 261 inline value::~value() {
265 inline value::value(
const value& x) : type_(x.type_) {
267 #define INIT(p, v) case p##type: u_.p = v; break 268 INIT(string_,
new std::string(*x.u_.string_));
269 INIT(array_,
new array(*x.u_.array_));
270 INIT(object_,
new object(*x.u_.object_));
278 inline value& value::operator=(
const value& x) {
286 #if PICOJSON_USE_RVALUE_REFERENCE 287 inline value::value(
value&& x)
throw() : type_(null_type) {
290 inline value& value::operator=(
value&& x)
throw() {
295 inline void value::swap(
value& x)
throw() {
296 std::swap(type_, x.type_);
300 #define IS(ctype, jtype) \ 301 template <> inline bool value::is<ctype>() const { \ 302 return type_ == jtype##_type; \ 306 #ifdef PICOJSON_USE_INT64 309 IS(std::string,
string)
313 template <>
inline bool value::is<double>()
const {
314 return type_ == number_type
315 #ifdef PICOJSON_USE_INT64 316 || type_ == int64_type
321 #define GET(ctype, var) \ 322 template <> inline const ctype& value::get<ctype>() const { \ 323 PICOJSON_ASSERT("type mismatch! call is<type>() before get<type>()" \ 327 template <> inline ctype& value::get<ctype>() { \ 328 PICOJSON_ASSERT("type mismatch! call is<type>() before get<type>()" \ 332 GET(
bool, u_.boolean_)
333 GET(std::string, *u_.string_)
334 GET(array, *u_.array_)
335 GET(
object, *u_.object_)
336 #ifdef PICOJSON_USE_INT64 337 GET(
double, (type_ == int64_type && (const_cast<value*>(
this)->type_ = number_type, const_cast<value*>(
this)->u_.number_ = u_.int64_), u_.number_))
338 GET(int64_t, u_.int64_)
340 GET(
double, u_.number_)
344 #define SET(ctype, jtype, setter) \ 345 template <> inline void value::set<ctype>(const ctype &_val) { \ 347 type_ = jtype##_type; \ 350 SET(
bool,
boolean, u_.boolean_ = _val;)
351 SET(std::string,
string, u_.string_ =
new std::string(_val);)
352 SET(array, array, u_.array_ =
new array(_val);)
353 SET(
object,
object, u_.object_ =
new object(_val);)
354 SET(
double, number, u_.number_ = _val;)
355 #ifdef PICOJSON_USE_INT64 356 SET(int64_t, int64, u_.int64_ = _val;)
360 #if PICOJSON_USE_RVALUE_REFERENCE 361 #define MOVESET(ctype, jtype, setter) \ 362 template <> inline void value::set<ctype>(ctype &&_val) { \ 364 type_ = jtype##_type; \ 367 MOVESET(std::string,
string, u_.string_ =
new std::string(std::move(_val));)
368 MOVESET(array, array, u_.array_ =
new array(std::move(_val));)
369 MOVESET(
object,
object, u_.object_ =
new object(std::move(_val));)
373 inline bool value::evaluate_as_boolean()
const {
380 return u_.number_ != 0;
381 #ifdef PICOJSON_USE_INT64 383 return u_.int64_ != 0;
386 return ! u_.string_->empty();
392 inline const value& value::get(
size_t idx)
const {
394 PICOJSON_ASSERT(is<array>());
395 return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null;
398 inline value& value::get(
size_t idx) {
400 PICOJSON_ASSERT(is<array>());
401 return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null;
404 inline const value& value::get(
const std::string& key)
const {
406 PICOJSON_ASSERT(is<object>());
407 object::const_iterator i = u_.object_->find(key);
408 return i != u_.object_->end() ? i->second : s_null;
411 inline value& value::get(
const std::string& key) {
413 PICOJSON_ASSERT(is<object>());
414 object::iterator i = u_.object_->find(key);
415 return i != u_.object_->end() ? i->second : s_null;
418 inline bool value::contains(
size_t idx)
const {
419 PICOJSON_ASSERT(is<array>());
420 return idx < u_.array_->size();
423 inline bool value::contains(
const std::string& key)
const {
424 PICOJSON_ASSERT(is<object>());
425 object::const_iterator i = u_.object_->find(key);
426 return i != u_.object_->end();
429 inline std::string value::to_str()
const {
431 case null_type:
return "null";
432 case boolean_type:
return u_.boolean_ ?
"true" :
"false";
433 #ifdef PICOJSON_USE_INT64 435 char buf[
sizeof(
"-9223372036854775808")];
436 SNPRINTF(buf,
sizeof(buf),
"%" PRId64, u_.int64_);
443 SNPRINTF(buf,
sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ?
"%.f" :
"%.17g", u_.number_);
444 #if PICOJSON_USE_LOCALE 445 char *decimal_point = localeconv()->decimal_point;
446 if (strcmp(decimal_point,
".") != 0) {
447 size_t decimal_point_len = strlen(decimal_point);
448 for (
char *p = buf; *p !=
'\0'; ++p) {
449 if (strncmp(p, decimal_point, decimal_point_len) == 0) {
450 return std::string(buf, p) +
"." + (p + decimal_point_len);
457 case string_type:
return *u_.string_;
458 case array_type:
return "array";
459 case object_type:
return "object";
460 default: PICOJSON_ASSERT(0);
465 return std::string();
468 template <
typename Iter>
void copy(
const std::string& s, Iter oi) {
469 std::copy(s.begin(), s.end(), oi);
472 template <
typename Iter>
475 void operator()(
char c) {
477 #define MAP(val, sym) case val: copy(sym, oi); break 488 if (static_cast<unsigned char>(c) < 0x20 || c == 0x7f) {
490 SNPRINTF(buf,
sizeof(buf),
"\\u%04x", c & 0xff);
491 copy(buf, buf + 6, oi);
500 template <
typename Iter>
void serialize_str(
const std::string& s, Iter oi) {
503 std::for_each(s.begin(), s.end(), process_char);
507 template <
typename Iter>
void value::serialize(Iter oi,
bool prettify)
const {
508 return _serialize(oi, prettify ? 0 : -1);
511 inline std::string value::serialize(
bool prettify)
const {
512 return _serialize(prettify ? 0 : -1);
515 template <
typename Iter>
void value::_indent(Iter oi,
int indent) {
517 for (
int i = 0; i < indent * INDENT_WIDTH; ++i) {
522 template <
typename Iter>
void value::_serialize(Iter oi,
int indent)
const {
525 serialize_str(*u_.string_, oi);
532 for (array::const_iterator i = u_.array_->begin();
533 i != u_.array_->end();
535 if (i != u_.array_->begin()) {
541 i->_serialize(oi, indent);
545 if (! u_.array_->empty()) {
557 for (object::const_iterator i = u_.object_->begin();
558 i != u_.object_->end();
560 if (i != u_.object_->begin()) {
566 serialize_str(i->first, oi);
571 i->second._serialize(oi, indent);
575 if (! u_.object_->empty()) {
591 inline std::string value::_serialize(
int indent)
const {
593 _serialize(std::back_inserter(s), indent);
597 template <
typename Iter>
class input {
603 input(
const Iter& first,
const Iter& last) : cur_(first), end_(last), consumed_(
false), line_(1) {}
624 self->consumed_ =
false;
629 int line()
const {
return line_; }
633 if (! (ch ==
' ' || ch ==
'\t' || ch ==
'\n' || ch ==
'\r')) {
639 bool expect(
int expect) {
641 if (getc() != expect) {
647 bool match(
const std::string& pattern) {
648 for (std::string::const_iterator pi(pattern.begin());
660 template<
typename Iter>
inline int _parse_quadhex(
input<Iter> &in) {
662 for (
int i = 0; i < 4; i++) {
663 if ((hex = in.getc()) == -1) {
666 if (
'0' <= hex && hex <=
'9') {
668 }
else if (
'A' <= hex && hex <=
'F') {
670 }
else if (
'a' <= hex && hex <=
'f') {
676 uni_ch = uni_ch * 16 + hex;
681 template<
typename String,
typename Iter>
inline bool _parse_codepoint(String& out,
input<Iter>& in) {
683 if ((uni_ch = _parse_quadhex(in)) == -1) {
686 if (0xd800 <= uni_ch && uni_ch <= 0xdfff) {
687 if (0xdc00 <= uni_ch) {
692 if (in.getc() !=
'\\' || in.getc() !=
'u') {
696 int second = _parse_quadhex(in);
697 if (! (0xdc00 <= second && second <= 0xdfff)) {
700 uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff);
704 out.push_back(uni_ch);
706 if (uni_ch < 0x800) {
707 out.push_back(0xc0 | (uni_ch >> 6));
709 if (uni_ch < 0x10000) {
710 out.push_back(0xe0 | (uni_ch >> 12));
712 out.push_back(0xf0 | (uni_ch >> 18));
713 out.push_back(0x80 | ((uni_ch >> 12) & 0x3f));
715 out.push_back(0x80 | ((uni_ch >> 6) & 0x3f));
717 out.push_back(0x80 | (uni_ch & 0x3f));
722 template<
typename String,
typename Iter>
inline bool _parse_string(String& out,
input<Iter>& in) {
728 }
else if (ch ==
'"') {
730 }
else if (ch ==
'\\') {
731 if ((ch = in.getc()) == -1) {
735 #define MAP(sym, val) case sym: out.push_back(val); break 746 if (! _parse_codepoint(out, in)) {
760 template <
typename Context,
typename Iter>
inline bool _parse_array(Context& ctx,
input<Iter>& in) {
761 if (! ctx.parse_array_start()) {
765 if (in.expect(
']')) {
766 return ctx.parse_array_stop(idx);
769 if (! ctx.parse_array_item(in, idx)) {
773 }
while (in.expect(
','));
774 return in.expect(
']') && ctx.parse_array_stop(idx);
777 template <
typename Context,
typename Iter>
inline bool _parse_object(Context& ctx,
input<Iter>& in) {
778 if (! ctx.parse_object_start()) {
781 if (in.expect(
'}')) {
787 || ! _parse_string(key, in)
788 || ! in.expect(
':')) {
791 if (! ctx.parse_object_item(in, key)) {
794 }
while (in.expect(
','));
795 return in.expect(
'}');
798 template <
typename Iter>
inline std::string _parse_number(
input<Iter>& in) {
802 if ((
'0' <= ch && ch <=
'9') || ch ==
'+' || ch ==
'-' 803 || ch ==
'e' || ch ==
'E') {
804 num_str.push_back(ch);
805 }
else if (ch ==
'.') {
806 #if PICOJSON_USE_LOCALE 807 num_str += localeconv()->decimal_point;
809 num_str.push_back(
'.');
819 template <
typename Context,
typename Iter>
inline bool _parse(Context& ctx,
input<Iter>& in) {
823 #define IS(ch, text, op) case ch: \ 824 if (in.match(text) && op) { \ 829 IS(
'n',
"ull", ctx.set_null());
830 IS(
'f',
"alse", ctx.set_bool(
false));
831 IS(
't',
"rue", ctx.set_bool(
true));
834 return ctx.parse_string(in);
836 return _parse_array(ctx, in);
838 return _parse_object(ctx, in);
840 if ((
'0' <= ch && ch <=
'9') || ch ==
'-') {
844 std::string num_str = _parse_number(in);
845 if (num_str.empty()) {
848 #ifdef PICOJSON_USE_INT64 851 intmax_t ival = strtoimax(num_str.c_str(), &endp, 10);
853 && std::numeric_limits<int64_t>::min() <= ival
854 && ival <= std::numeric_limits<int64_t>::max()
855 && endp == num_str.c_str() + num_str.size()) {
861 f = strtod(num_str.c_str(), &endp);
862 if (endp == num_str.c_str() + num_str.size()) {
876 bool set_null() {
return false; }
877 bool set_bool(
bool) {
return false; }
878 #ifdef PICOJSON_USE_INT64 879 bool set_int64(int64_t) {
return false; }
881 bool set_number(
double) {
return false; }
882 template <
typename Iter>
bool parse_string(
input<Iter>&) {
return false; }
883 bool parse_array_start() {
return false; }
884 template <
typename Iter>
bool parse_array_item(
input<Iter>&,
size_t) {
887 bool parse_array_stop(
size_t) {
return false; }
888 bool parse_object_start() {
return false; }
889 template <
typename Iter>
bool parse_object_item(
input<Iter>&,
const std::string&) {
903 bool set_bool(
bool b) {
907 #ifdef PICOJSON_USE_INT64 908 bool set_int64(int64_t i) {
913 bool set_number(
double f) {
917 template<
typename Iter>
bool parse_string(
input<Iter>& in) {
918 *out_ =
value(string_type,
false);
919 return _parse_string(out_->get<std::string>(), in);
921 bool parse_array_start() {
922 *out_ =
value(array_type,
false);
925 template <
typename Iter>
bool parse_array_item(
input<Iter>& in,
size_t) {
926 array& a = out_->get<array>();
927 a.push_back(
value());
929 return _parse(ctx, in);
931 bool parse_array_stop(
size_t) {
return true; }
932 bool parse_object_start() {
933 *out_ =
value(object_type,
false);
936 template <
typename Iter>
bool parse_object_item(
input<Iter>& in,
const std::string& key) {
937 object& o = out_->get<
object>();
939 return _parse(ctx, in);
949 void push_back(
int) {}
953 bool set_null() {
return true; }
954 bool set_bool(
bool) {
return true; }
955 #ifdef PICOJSON_USE_INT64 956 bool set_int64(int64_t) {
return true; }
958 bool set_number(
double) {
return true; }
959 template <
typename Iter>
bool parse_string(
input<Iter>& in) {
961 return _parse_string(s, in);
963 bool parse_array_start() {
return true; }
964 template <
typename Iter>
bool parse_array_item(
input<Iter>& in,
size_t) {
965 return _parse(*
this, in);
967 bool parse_array_stop(
size_t) {
return true; }
968 bool parse_object_start() {
return true; }
969 template <
typename Iter>
bool parse_object_item(
input<Iter>& in,
const std::string&) {
970 return _parse(*
this, in);
978 template <
typename Iter>
inline std::string parse(
value& out, Iter& pos,
const Iter& last) {
980 pos = parse(out, pos, last, &err);
984 template <
typename Context,
typename Iter>
inline Iter _parse(Context& ctx,
const Iter& first,
const Iter& last, std::string* err) {
986 if (! _parse(ctx, in) && err != NULL) {
988 SNPRINTF(buf,
sizeof(buf),
"syntax error at line %d near: ", in.line());
992 if (ch == -1 || ch ==
'\n') {
994 }
else if (ch >=
' ') {
1002 template <
typename Iter>
inline Iter parse(
value& out,
const Iter& first,
const Iter& last, std::string* err) {
1004 return _parse(ctx, first, last, err);
1007 inline std::string parse(
value& out,
const std::string& s) {
1009 parse(out, s.begin(), s.end(), &err);
1013 inline std::string parse(
value& out, std::istream& is) {
1015 parse(out, std::istreambuf_iterator<char>(is.rdbuf()),
1016 std::istreambuf_iterator<char>(), &err);
1021 static std::string s;
1025 inline void set_last_error(
const std::string& s) {
1029 inline const std::string& get_last_error() {
1033 inline bool operator==(
const value& x,
const value& y) {
1035 return y.is<
null>();
1036 #define PICOJSON_CMP(type) \ 1038 return y.is<type>() && x.get<type>() == y.get<type>() 1040 PICOJSON_CMP(
double);
1041 PICOJSON_CMP(std::string);
1042 PICOJSON_CMP(array);
1043 PICOJSON_CMP(
object);
1052 inline bool operator!=(
const value& x,
const value& y) {
1057 #if !PICOJSON_USE_RVALUE_REFERENCE 1068 picojson::set_last_error(std::string());
1069 std::string err = picojson::parse(x, is);
1070 if (! err.empty()) {
1071 picojson::set_last_error(err);
1072 is.setstate(std::ios::failbit);
1077 inline std::ostream& operator<<(std::ostream& os,
const picojson::value& x)
1079 x.serialize(std::ostream_iterator<char>(os));
1083 #pragma warning(pop) Definition: picojson.h:124
Definition: picojson.h:1020
Definition: picojson.h:946
Definition: picojson.h:1058
Definition: picojson.h:894
Definition: picojson.h:120
Definition: picojson.h:118
Definition: picojson.h:473
Definition: picojson.h:100
Definition: picojson.h:874
Definition: picojson.h:948