Go to the documentation of this file.
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
68 #ifndef PICOJSON_NOEXCEPT
69 #if PICOJSON_USE_RVALUE_REFERENCE
70 #define PICOJSON_NOEXCEPT noexcept
72 #define PICOJSON_NOEXCEPT throw()
77 #ifdef PICOJSON_USE_INT64
78 #define __STDC_FORMAT_MACROS
80 #if __cplusplus >= 201103L
90 #ifndef PICOJSON_USE_LOCALE
91 #define PICOJSON_USE_LOCALE 1
93 #if PICOJSON_USE_LOCALE
99 #ifndef PICOJSON_ASSERT
100 #define PICOJSON_ASSERT(e) \
103 throw std::runtime_error(#e); \
108 #define SNPRINTF _snprintf_s
109 #pragma warning(push)
110 #pragma warning(disable : 4244) // conversion from int to char
111 #pragma warning(disable : 4127) // conditional expression is constant
112 #pragma warning(disable : 4702) // unreachable code
113 #pragma warning(disable : 4706) // assignment within conditional expression
115 #define SNPRINTF snprintf
127 #ifdef PICOJSON_USE_INT64
140 typedef std::map<std::string, value>
object;
144 #ifdef PICOJSON_USE_INT64
159 explicit value(
bool b);
160 #ifdef PICOJSON_USE_INT64
161 explicit value(int64_t
i);
163 explicit value(
double n);
164 explicit value(
const std::string &
s);
166 explicit value(
const object &o);
167 #if PICOJSON_USE_RVALUE_REFERENCE
168 explicit value(std::string &&
s);
170 explicit value(
object &&o);
172 explicit value(
const char *
s);
173 value(
const char *
s,
size_t len);
177 #if PICOJSON_USE_RVALUE_REFERENCE
182 template <
typename T>
bool is()
const;
183 template <
typename T>
const T &
get()
const;
184 template <
typename T> T &
get();
185 template <
typename T>
void set(
const T &);
186 #if PICOJSON_USE_RVALUE_REFERENCE
187 template <
typename T>
void set(T &&);
190 const value &
get(
const size_t idx)
const;
191 const value &
get(
const std::string &key)
const;
195 bool contains(
const size_t idx)
const;
196 bool contains(
const std::string &key)
const;
197 std::string
to_str()
const;
198 template <
typename Iter>
void serialize(Iter os,
bool prettify =
false)
const;
199 std::string
serialize(
bool prettify =
false)
const;
202 template <
typename T>
value(
const T *);
203 template <
typename Iter>
static void _indent(Iter os,
int indent);
204 template <
typename Iter>
void _serialize(Iter os,
int indent)
const;
205 std::string _serialize(
int indent)
const;
221 INIT(boolean_,
false);
223 #ifdef PICOJSON_USE_INT64
226 INIT(string_,
new std::string());
228 INIT(object_,
new object());
239 #ifdef PICOJSON_USE_INT64
249 #elif __cplusplus >= 201103L
250 std::isnan(n) || std::isinf(n)
255 throw std::overflow_error(
"");
272 #if PICOJSON_USE_RVALUE_REFERENCE
294 inline void value::clear() {
337 #if PICOJSON_USE_RVALUE_REFERENCE
351 #define IS(ctype, jtype) \
352 template <> inline bool value::is<ctype>() const { \
353 return type_ == jtype##_type; \
357 #ifdef PICOJSON_USE_INT64
360 IS(std::string,
string)
364 template <>
inline bool value::is<double>()
const {
366 #ifdef PICOJSON_USE_INT64
367 ||
type_ == int64_type
372 #define GET(ctype, var) \
373 template <> inline const ctype &value::get<ctype>() const { \
374 PICOJSON_ASSERT("type mismatch! call is<type>() before get<type>()" && is<ctype>()); \
377 template <> inline ctype &value::get<ctype>() { \
378 PICOJSON_ASSERT("type mismatch! call is<type>() before get<type>()" && is<ctype>()); \
381 GET(
bool, u_.boolean_)
382 GET(
std::
string, *u_.string_)
384 GET(
object, *u_.object_)
385 #ifdef PICOJSON_USE_INT64
387 (type_ == int64_type && (
const_cast<value *
>(
this)->type_ =
number_type, (
const_cast<value *
>(
this)->u_.number_ = u_.int64_)),
389 GET(int64_t, u_.int64_)
391 GET(
double, u_.number_)
395 #define SET(ctype, jtype, setter) \
396 template <> inline void value::set<ctype>(const ctype &_val) { \
398 type_ = jtype##_type; \
401 SET(
bool,
boolean, u_.boolean_ = _val;)
402 SET(std::string,
string, u_.string_ =
new std::string(_val);)
404 SET(
object,
object, u_.object_ =
new object(_val);)
405 SET(
double, number, u_.number_ = _val;)
406 #ifdef PICOJSON_USE_INT64
407 SET(int64_t, int64, u_.int64_ = _val;)
411 #if PICOJSON_USE_RVALUE_REFERENCE
412 #define MOVESET(ctype, jtype, setter) \
413 template <> inline void value::set<ctype>(ctype && _val) { \
415 type_ = jtype##_type; \
418 MOVESET(std::string,
string, u_.string_ =
new std::string(std::move(_val));)
420 MOVESET(
object,
object, u_.object_ =
new object(std::move(_val));)
432 #ifdef PICOJSON_USE_INT64
434 return u_.int64_ != 0;
458 object::const_iterator
i =
u_.
object_->find(key);
459 return i !=
u_.
object_->end() ?
i->second : s_null;
466 return i !=
u_.
object_->end() ?
i->second : s_null;
476 object::const_iterator
i =
u_.
object_->find(key);
486 #ifdef PICOJSON_USE_INT64
488 char buf[
sizeof(
"-9223372036854775808")];
489 SNPRINTF(buf,
sizeof(buf),
"%" PRId64,
u_.int64_);
497 #if PICOJSON_USE_LOCALE
498 char *decimal_point = localeconv()->decimal_point;
499 if (strcmp(decimal_point,
".") != 0) {
500 size_t decimal_point_len = strlen(decimal_point);
501 for (
char *p = buf; *p !=
'\0'; ++p) {
502 if (strncmp(p, decimal_point, decimal_point_len) == 0) {
503 return std::string(buf, p) +
"." + (p + decimal_point_len);
522 return std::string();
525 template <
typename Iter>
void copy(
const std::string &
s, Iter oi) {
533 #define MAP(val, sym) \
547 if (
static_cast<unsigned char>(
c) < 0x20 ||
c == 0x7f) {
549 SNPRINTF(buf,
sizeof(buf),
"\\u%04x",
c & 0xff);
562 std::for_each(
s.begin(),
s.end(), process_char);
567 return _serialize(oi, prettify ? 0 : -1);
571 return _serialize(prettify ? 0 : -1);
574 template <
typename Iter>
void value::_indent(Iter oi,
int indent) {
581 template <
typename Iter>
void value::_serialize(Iter oi,
int indent)
const {
598 i->_serialize(oi, indent);
626 i->second._serialize(oi, indent);
646 inline std::string value::_serialize(
int indent)
const {
648 _serialize(std::back_inserter(
s), indent);
652 template <
typename Iter>
class input {
692 if (!(ch ==
' ' || ch ==
'\t' || ch ==
'\n' || ch ==
'\r')) {
700 if (
getc() != expected) {
706 bool match(
const std::string &pattern) {
707 for (std::string::const_iterator pi(pattern.begin()); pi != pattern.end(); ++pi) {
719 for (
int i = 0;
i < 4;
i++) {
720 if ((hex = in.
getc()) == -1) {
723 if (
'0' <= hex && hex <=
'9') {
725 }
else if (
'A' <= hex && hex <=
'F') {
727 }
else if (
'a' <= hex && hex <=
'f') {
733 uni_ch = uni_ch * 16 + hex;
743 if (0xd800 <= uni_ch && uni_ch <= 0xdfff) {
744 if (0xdc00 <= uni_ch) {
749 if (in.
getc() !=
'\\' || in.
getc() !=
'u') {
754 if (!(0xdc00 <= second && second <= 0xdfff)) {
757 uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff);
761 out.push_back(
static_cast<char>(uni_ch));
763 if (uni_ch < 0x800) {
764 out.push_back(
static_cast<char>(0xc0 | (uni_ch >> 6)));
766 if (uni_ch < 0x10000) {
767 out.push_back(
static_cast<char>(0xe0 | (uni_ch >> 12)));
769 out.push_back(
static_cast<char>(0xf0 | (uni_ch >> 18)));
770 out.push_back(
static_cast<char>(0x80 | ((uni_ch >> 12) & 0x3f)));
772 out.push_back(
static_cast<char>(0x80 | ((uni_ch >> 6) & 0x3f)));
774 out.push_back(
static_cast<char>(0x80 | (uni_ch & 0x3f)));
785 }
else if (ch ==
'"') {
787 }
else if (ch ==
'\\') {
788 if ((ch = in.
getc()) == -1) {
792 #define MAP(sym, val) \
794 out.push_back(val); \
814 out.push_back(
static_cast<char>(ch));
821 if (!ctx.parse_array_start()) {
826 return ctx.parse_array_stop(idx);
829 if (!ctx.parse_array_item(in, idx)) {
834 return in.
expect(
']') && ctx.parse_array_stop(idx);
838 if (!ctx.parse_object_start()) {
842 return ctx.parse_object_stop();
849 if (!ctx.parse_object_item(in, key)) {
853 return in.
expect(
'}') && ctx.parse_object_stop();
860 if ((
'0' <= ch && ch <=
'9') || ch ==
'+' || ch ==
'-' || ch ==
'e' || ch ==
'E') {
861 num_str.push_back(
static_cast<char>(ch));
862 }
else if (ch ==
'.') {
863 #if PICOJSON_USE_LOCALE
864 num_str += localeconv()->decimal_point;
866 num_str.push_back(
'.');
880 #define IS(ch, text, op) \
882 if (in.match(text) && op) { \
887 IS(
'n',
"ull", ctx.set_null());
888 IS(
'f',
"alse", ctx.set_bool(
false));
889 IS(
't',
"rue", ctx.set_bool(
true));
892 return ctx.parse_string(in);
898 if ((
'0' <= ch && ch <=
'9') || ch ==
'-') {
903 if (num_str.empty()) {
906 #ifdef PICOJSON_USE_INT64
909 intmax_t ival = strtoimax(num_str.c_str(), &endp, 10);
910 if (errno == 0 && std::numeric_limits<int64_t>::min() <= ival && ival <= std::numeric_limits<int64_t>::max() &&
911 endp == num_str.c_str() + num_str.size()) {
917 f = strtod(num_str.c_str(), &endp);
918 if (endp == num_str.c_str() + num_str.size()) {
938 #ifdef PICOJSON_USE_INT64
939 bool set_int64(int64_t) {
982 #ifdef PICOJSON_USE_INT64
983 bool set_int64(int64_t
i) {
1005 a.push_back(
value());
1020 object &o =
out_->
get<
object>();
1053 #ifdef PICOJSON_USE_INT64
1054 bool set_int64(int64_t) {
1072 return _parse(*
this, in);
1086 return _parse(*
this, in);
1098 template <
typename Iter>
inline std::string
parse(
value &out, Iter &pos,
const Iter &
last) {
1104 template <
typename Context,
typename Iter>
inline Iter
_parse(Context &ctx,
const Iter &first,
const Iter &
last, std::string *err) {
1106 if (!
_parse(ctx, in) && err != NULL) {
1108 SNPRINTF(buf,
sizeof(buf),
"syntax error at line %d near: ", in.
line());
1112 if (ch == -1 || ch ==
'\n') {
1114 }
else if (ch >=
' ') {
1115 err->push_back(
static_cast<char>(ch));
1122 template <
typename Iter>
inline Iter
parse(
value &out,
const Iter &first,
const Iter &
last, std::string *err) {
1129 parse(out,
s.begin(),
s.end(), &err);
1135 parse(out, std::istreambuf_iterator<char>(is.rdbuf()), std::istreambuf_iterator<char>(), &err);
1152 return y.
is<
null>();
1153 #define PICOJSON_CMP(type) \
1155 return y.is<type>() && x.get<type>() == y.get<type>()
1174 #if !PICOJSON_USE_RVALUE_REFERENCE
1187 is.setstate(std::ios::failbit);
1193 x.
serialize(std::ostream_iterator<char>(os));
1197 #pragma warning(pop)
bool set_null()
Definition: picojson.h:1047
Definition: picojson.h:966
bool set_number(double f)
Definition: picojson.h:988
#define SNPRINTF
Definition: picojson.h:115
#define IS(ctype, jtype)
Definition: picojson.h:351
Definition: picojson.h:137
bool parse_object_item(input< Iter > &, const std::string &)
Definition: picojson.h:961
value * out_
Definition: picojson.h:968
bool parse_array_item(input< Iter > &in, size_t)
Definition: picojson.h:1003
std::vector< value > array
Definition: picojson.h:139
void swap(picojson::value &x, picojson::value &y)
Definition: picojson.h:1176
#define PICOJSON_NOEXCEPT
Definition: picojson.h:72
void serialize_str(const std::string &s, Iter oi)
Definition: picojson.h:559
null_parse_context(size_t depths=DEFAULT_MAX_DEPTHS)
Definition: picojson.h:1045
bool parse_array_item(input< Iter > &, size_t)
Definition: picojson.h:952
Iter oi
Definition: picojson.h:530
object * object_
Definition: picojson.h:149
bool parse_object_stop()
Definition: picojson.h:1088
@ INDENT_WIDTH
Definition: picojson.h:133
bool parse_object_item(input< Iter > &in, const std::string &key)
Definition: picojson.h:1019
bool set_null()
Definition: picojson.h:932
@ boolean_type
Definition: picojson.h:122
bool set_number(double)
Definition: picojson.h:1058
void push_back(int)
Definition: picojson.h:1040
bool parse_array_start()
Definition: picojson.h:949
#define GET(ctype, var)
Definition: picojson.h:372
bool _parse_string(String &out, input< Iter > &in)
Definition: picojson.h:779
void serialize(Iter os, bool prettify=false) const
Definition: picojson.h:566
Definition: picojson.h:1139
#define SET(ctype, jtype, setter)
Definition: picojson.h:395
double number_
Definition: picojson.h:143
bool _parse(Context &ctx, input< Iter > &in)
Definition: picojson.h:876
@ string_type
Definition: picojson.h:124
bool parse_array_stop(size_t)
Definition: picojson.h:955
bool parse_string(input< Iter > &)
Definition: picojson.h:946
Definition: picojson.h:1039
bool _parse_codepoint(String &out, input< Iter > &in)
Definition: picojson.h:738
bool set_bool(bool)
Definition: picojson.h:1050
std::ostream & operator<<(std::ostream &os, const picojson::value &x)
Definition: picojson.h:1192
_storage u_
Definition: picojson.h:154
value & operator=(const value &x)
Definition: picojson.h:329
bool parse_array_item(input< Iter > &in, size_t)
Definition: picojson.h:1071
Definition: picojson.h:930
bool boolean_
Definition: picojson.h:142
@ null_type
Definition: picojson.h:121
static std::string s
Definition: picojson.h:1139
@ number_type
Definition: picojson.h:123
array * array_
Definition: picojson.h:148
bool parse_array_stop(size_t)
Definition: picojson.h:1009
void set_last_error(const std::string &s)
Definition: picojson.h:1142
#define PICOJSON_ASSERT(e)
Definition: picojson.h:100
Definition: picojson.h:118
std::string to_str() const
Definition: picojson.h:480
int _parse_quadhex(input< Iter > &in)
Definition: picojson.h:717
std::string _parse_number(input< Iter > &in)
Definition: picojson.h:856
int type_
Definition: picojson.h:153
value::object object
Definition: picojson.h:210
std::string * string_
Definition: picojson.h:147
const std::string & get_last_error()
Definition: picojson.h:1146
size_t depths_
Definition: picojson.h:969
bool contains(const size_t idx) const
Definition: picojson.h:469
std::string parse(value &out, Iter &pos, const Iter &last)
Definition: picojson.h:1098
bool parse_array_stop(size_t)
Definition: picojson.h:1074
bool parse_object_start()
Definition: picojson.h:1078
std::map< std::string, value > object
Definition: picojson.h:140
Definition: picojson.h:1034
type
Generic JSON types used in JWTs.
Definition: jwt.h:1794
bool parse_string(input< Iter > &in)
Definition: picojson.h:992
size_t depths_
Definition: picojson.h:1036
default_parse_context(value *out, size_t depths=DEFAULT_MAX_DEPTHS)
Definition: picojson.h:972
void operator()(char c)
Definition: picojson.h:531
@ object_type
Definition: picojson.h:126
Definition: picojson.h:529
std::istream & operator>>(std::istream &is, picojson::value &x)
Definition: picojson.h:1182
bool _parse_object(Context &ctx, input< Iter > &in)
Definition: picojson.h:837
bool _parse_array(Context &ctx, input< Iter > &in)
Definition: picojson.h:820
value()
Definition: picojson.h:212
#define PICOJSON_CMP(type)
unsigned long long last
Definition: CscMrptAutonomousExploration.cpp:309
Definition: json.hpp:4598
bool operator!=(const value &x, const value &y)
Definition: picojson.h:1169
void copy(const std::string &s, Iter oi)
Definition: picojson.h:525
bool parse_string(input< Iter > &in)
Definition: picojson.h:1061
value::array array
Definition: picojson.h:209
bool evaluate_as_boolean() const
Definition: picojson.h:424
bool operator==(const value &x, const value &y)
Definition: picojson.h:1150
bool set_bool(bool)
Definition: picojson.h:935
double c
Definition: HybridAStar.cpp:84
@ array_type
Definition: picojson.h:125
bool parse_object_stop()
Definition: picojson.h:1024
bool set_bool(bool b)
Definition: picojson.h:978
double s
Definition: HybridAStar.cpp:85
bool parse_object_item(input< Iter > &in, const std::string &)
Definition: picojson.h:1084
bool parse_array_start()
Definition: picojson.h:996
bool parse_object_start()
Definition: picojson.h:1013
void swap(value &x) PICOJSON_NOEXCEPT
Definition: picojson.h:346
double f
Definition: HybridAStar.cpp:190
~value()
Definition: picojson.h:309
bool parse_object_start()
Definition: picojson.h:958
bool set_null()
Definition: picojson.h:974
bool parse_array_start()
Definition: picojson.h:1065
Definition: picojson.h:141
Definition: picojson.h:135
bool set_number(double)
Definition: picojson.h:943
@ DEFAULT_MAX_DEPTHS
Definition: picojson.h:133
int i
Definition: HybridAStar.cpp:191