6 #include <unordered_map> 10 #include <boost/lexical_cast.hpp> 11 #include <boost/algorithm/string/predicate.hpp> 12 #include <boost/operators.hpp> 15 #if defined(__GNUG__) || defined(__clang__) 16 #define crow_json_likely(x) __builtin_expect(x, 1) 17 #define crow_json_unlikely(x) __builtin_expect(x, 0) 19 #define crow_json_likely(x) x 20 #define crow_json_unlikely(x) x 33 inline void escape(
const std::string& str, std::string& ret)
35 ret.reserve(ret.size() + str.size()+str.size()/4);
40 case '"': ret +=
"\\\"";
break;
41 case '\\': ret +=
"\\\\";
break;
42 case '\n': ret +=
"\\n";
break;
43 case '\b': ret +=
"\\b";
break;
44 case '\f': ret +=
"\\f";
break;
45 case '\r': ret +=
"\\r";
break;
46 case '\t': ret +=
"\\t";
break;
48 if (0 <= c && c < 0x20)
51 auto to_hex = [](
char c)
67 inline std::string
escape(
const std::string& str)
93 default:
return "Unknown";
98 rvalue
load(
const char* data,
size_t size);
104 : boost::less_than_comparable<r_string>,
105 boost::less_than_comparable<r_string, std::string>,
106 boost::equality_comparable<r_string>,
107 boost::equality_comparable<r_string, std::string>
147 operator std::string ()
const 149 return std::string(
s_,
e_);
154 const char*
end()
const {
return e_; }
165 os << (std::string)
s;
179 return boost::lexicographical_compare(
l,r);
184 return boost::lexicographical_compare(
l,r);
189 return boost::lexicographical_compare(r,
l);
194 return boost::equals(
l,r);
199 return boost::equals(
l,r);
231 *
this = std::move(r);
248 key_ = std::move(r.key_);
249 l_ = std::move(r.l_);
257 explicit operator bool() const noexcept
262 explicit operator int64_t()
const 267 explicit operator uint64_t()
const 272 explicit operator int()
const 279 #ifndef CROW_JSON_NO_ERROR_CHECK 282 throw std::runtime_error(
"invalid json object");
290 #ifndef CROW_JSON_NO_ERROR_CHECK 296 const std::string msg =
"expected number, got: " 298 throw std::runtime_error(msg);
306 #ifndef CROW_JSON_NO_ERROR_CHECK 312 throw std::runtime_error(std::string(
"expected number, got: ") +
get_type_str(
t()));
320 #ifndef CROW_JSON_NO_ERROR_CHECK 322 throw std::runtime_error(
"value is not number");
329 #ifndef CROW_JSON_NO_ERROR_CHECK 331 throw std::runtime_error(
"value is not boolean");
348 case '"': *tail++ =
'"';
break;
349 case '\\': *tail++ =
'\\';
break;
350 case '/': *tail++ =
'/';
break;
351 case 'b': *tail++ =
'\b';
break;
352 case 'f': *tail++ =
'\f';
break;
353 case 'n': *tail++ =
'\n';
break;
354 case 'r': *tail++ =
'\r';
break;
355 case 't': *tail++ =
'\t';
break;
358 auto from_hex = [](
char c)
367 (from_hex(head[1])<<12) +
368 (from_hex(head[2])<< 8) +
369 (from_hex(head[3])<< 4) +
373 *tail++ = 0xE0 | (code >> 12);
374 *tail++ = 0x80 | ((code >> 6) & 0x3F);
375 *tail++ = 0x80 | (code & 0x3F);
377 else if (code >= 0x80)
379 *tail++ = 0xC0 | (code >> 6);
380 *tail++ = 0x80 | (code & 0x3F);
403 #ifndef CROW_JSON_NO_ERROR_CHECK 405 throw std::runtime_error(
"value is not string");
411 bool has(
const char* str)
const 413 return has(std::string(str));
416 bool has(
const std::string& str)
const 422 return l.key_ < r.
key_;
424 bool operator()(
const rvalue&
l,
const std::string& r)
const 428 bool operator()(
const std::string&
l,
const rvalue& r)
const 438 auto it = lower_bound(
begin(),
end(), str, Pred());
439 return it !=
end() && it->
key_ == str;
444 return has(str) ? 1 : 0;
449 #ifndef CROW_JSON_NO_ERROR_CHECK 451 throw std::runtime_error(
"value is not a container");
457 #ifndef CROW_JSON_NO_ERROR_CHECK 459 throw std::runtime_error(
"value is not a container");
473 #ifndef CROW_JSON_NO_ERROR_CHECK 475 throw std::runtime_error(
"value is not a container");
482 #ifndef CROW_JSON_NO_ERROR_CHECK 484 throw std::runtime_error(
"value is not a list");
485 if (index >= (
int)
lsize_ || index < 0)
486 throw std::runtime_error(
"list out of bound");
493 #ifndef CROW_JSON_NO_ERROR_CHECK 495 throw std::runtime_error(
"value is not a list");
497 throw std::runtime_error(
"list out of bound");
509 #ifndef CROW_JSON_NO_ERROR_CHECK 511 throw std::runtime_error(
"value is not an object");
517 return l.key_ < r.
key_;
519 bool operator()(
const rvalue&
l,
const std::string& r)
const 523 bool operator()(
const std::string&
l,
const rvalue& r)
const 533 auto it = lower_bound(
begin(),
end(), str, Pred());
534 if (it !=
end() && it->
key_ == str)
536 #ifndef CROW_JSON_NO_ERROR_CHECK 537 throw std::runtime_error(
"cannot find key");
577 if (new_size -
lsize_ > 60000)
578 new_size =
lsize_ + 60000;
584 *p2++ = std::move(x);
595 std::unique_ptr<rvalue[]>
l_;
635 os <<
'"' <<
escape(x.key_) <<
"\":";
695 Parser(
char* data,
size_t )
710 while(*data ==
' ' || *data ==
'\t' || *data ==
'\r' || *data ==
'\n') ++data;
718 uint8_t has_escaping = 0;
725 else if (*data ==
'"')
728 *(start-1) = has_escaping;
732 else if (*data ==
'\\')
740 auto check = [](
char c)
743 (
'0' <= c && c <=
'9') ||
744 (
'a' <= c && c <=
'f') ||
745 (
'A' <= c && c <=
'F');
747 if (!(check(*(data+1)) &&
792 auto v = decode_value();
819 enum NumberParsingState
835 state = (NumberParsingState)
"\2\2\7\3\4\6\6"[
state];
853 case '1':
case '2':
case '3':
854 case '4':
case '5':
case '6':
855 case '7':
case '8':
case '9':
856 state = (NumberParsingState)
"\3\3\7\3\4\6\6"[
state];
857 while(*(data+1) >=
'0' && *(data+1) <=
'9') data++;
876 state = (NumberParsingState)
"\7\7\4\4\7\7\7"[
state];
887 state = (NumberParsingState)
"\1\7\7\7\7\6\7"[
state];
900 state = (NumberParsingState)
"\7\7\7\7\7\6\7"[
state];
909 state = (NumberParsingState)
"\7\7\7\5\5\7\7"[
state];
920 state == NumberParsingState::Digits ||
921 state == NumberParsingState::DigitsAfterPoints ||
922 state == NumberParsingState::DigitsAfterE))
938 return decode_list();
940 return decode_object();
942 return decode_string();
982 return decode_number();
1006 auto t = decode_string();
1024 auto v = decode_value();
1032 v.key_ = std::move(key);
1052 auto ret = decode_value();
1054 if (ret && *data !=
'\0')
1061 return Parser(data, size).parse();
1065 char*
s =
new char[size+1];
1066 memcpy(
s, data, size);
1070 ret.key_.force(
s, size);
1078 return load(data, strlen(data));
1083 return load(str.data(), str.size());
1095 std::unique_ptr<std::vector<wvalue>>
l;
1096 std::unique_ptr<std::unordered_map<std::string, wvalue>>
o;
1117 l = std::unique_ptr<std::vector<wvalue>>(
new std::vector<wvalue>{});
1118 l->reserve(r.
size());
1119 for(
auto it = r.
begin(); it != r.
end(); ++it)
1120 l->emplace_back(*it);
1123 o = std::unique_ptr<
1124 std::unordered_map<std::string, wvalue>
1126 new std::unordered_map<std::string, wvalue>{});
1127 for(
auto it = r.
begin(); it != r.
end(); ++it)
1128 o->emplace(it->key(), *it);
1135 *
this = std::move(r);
1265 template <
typename T>
1272 l = std::unique_ptr<std::vector<wvalue>>(
new std::vector<wvalue>{});
1274 l->resize(v.size());
1289 l = std::unique_ptr<std::vector<wvalue>>(
new std::vector<wvalue>{});
1290 if (
l->size() < index+1)
1301 return o->count(str);
1310 o = std::unique_ptr<
1311 std::unordered_map<std::string, wvalue>
1313 new std::unordered_map<std::string, wvalue>{});
1334 sum += x.estimate_length();
1347 sum += 2+kv.first.size()+kv.first.size()/2;
1348 sum += kv.second.estimate_length();
1378 sprintf(outbuf,
"%g", v.
d);
1440 #undef crow_json_likely 1441 #undef crow_json_unlikely rvalue * begin() const
Definition: json.h:447
uint8_t option_
Definition: json.h:599
char * s_
Definition: json.h:160
r_string(char *s, char *e)
Definition: json.h:110
const char * const_iterator
Definition: json.h:158
wvalue & operator=(const std::string &str)
Definition: json.h:1257
type t() const
Definition: json.h:1090
void emplace_back(rvalue &&v)
Definition: json.h:572
bool operator<(const r_string &l, const r_string &r)
Definition: json.h:177
friend void dump_internal(const wvalue &v, std::string &out)
Definition: json.h:1368
r_string(const r_string &r)
Definition: json.h:119
rvalue(type t) noexcept
Definition: json.h:210
friend std::string dump(const wvalue &v)
Definition: json.h:1426
wvalue & operator=(const char *str)
Definition: json.h:1249
bool operator!=(const rvalue &l, const std::string &r)
Definition: json.h:659
rvalue(const rvalue &r)
Definition: json.h:219
size_t size() const
Definition: json.h:155
type t_
Definition: json.h:598
void dump_internal(const wvalue &v, std::string &out)
Definition: json.h:1368
friend rvalue load(const char *data, size_t size)
Definition: json.h:1063
friend rvalue load_nocopy_internal(char *data, size_t size)
Definition: json.h:690
uint8_t owned_
Definition: json.h:162
char * start_
Definition: json.h:592
double d
Definition: json.h:1093
const char * iterator
Definition: json.h:157
rvalue() noexcept
Definition: json.h:208
std::unique_ptr< std::vector< wvalue > > l
Definition: json.h:1095
void force(char *s, uint32_t)
Definition: json.h:169
bool error() const
Definition: json.h:549
const char * get_type_str(type t)
Definition: json.h:85
uint64_t u() const
Definition: json.h:304
std::string dump(const wvalue &v)
Definition: json.h:1426
wvalue & operator[](const std::string &str)
Definition: json.h:1304
wvalue(wvalue &&r)
Definition: json.h:1133
bool b() const
Definition: json.h:327
char * end_
Definition: json.h:593
const rvalue & operator[](const char *str) const
Definition: json.h:502
size_t size() const
Definition: json.h:469
Definition: mustache.h:52
size_t estimate_length() const
Definition: json.h:1317
void set_cached() const
Definition: json.h:558
rvalue(rvalue &&r) noexcept
Definition: json.h:229
const char * end() const
Definition: json.h:154
void copy(key &AA, const key &A)
Definition: rctOps.h:81
bool has(const std::string &str) const
Definition: json.h:416
uint32_t lsize_
Definition: json.h:596
std::unique_ptr< rvalue[]> l_
Definition: json.h:595
rvalue * end() const
Definition: json.h:455
friend std::ostream & operator<<(std::ostream &os, const r_string &s)
Definition: json.h:163
std::string s
Definition: json.h:1094
declaration and default definition for the functions used the API
void escape(const std::string &str, std::string &ret)
Definition: json.h:33
wvalue()
Definition: json.h:1099
uint16_t lremain_
Definition: json.h:597
type t_
Definition: json.h:1092
wvalue & operator=(wvalue &&r)
Definition: json.h:1138
const detail::r_string & key() const
Definition: json.h:464
r_string(r_string &&r)
Definition: json.h:124
rvalue(type t, char *s, char *e) noexcept
Definition: json.h:213
const char * begin() const
Definition: json.h:153
wvalue(const rvalue &r)
Definition: json.h:1101
type
Definition: json.h:74
void clear()
Definition: json.h:1148
type t() const
Definition: json.h:277
char * e_
Definition: json.h:161
int l
Definition: base.py:3
int count(const std::string &str)
Definition: json.h:442
rvalue & operator=(const rvalue &r)
Definition: json.h:234
static const int error_bit
Definition: json.h:206
rvalue load_nocopy_internal(char *data, size_t size)
Definition: json.h:690
void reset()
Definition: json.h:1155
detail::r_string s() const
Definition: json.h:401
Definition: blake256.h:37
wvalue & operator=(const std::vector< T > &v)
Definition: json.h:1266
const rvalue & operator[](const std::string &str) const
Definition: json.h:507
const rvalue & operator[](int index) const
Definition: json.h:480
rvalue load(const char *data, size_t size)
Definition: json.h:1063
~r_string()
Definition: json.h:113
int bool
Definition: stdbool.h:36
void copy_l(const rvalue &r)
Definition: json.h:562
void set_error()
Definition: json.h:544
void unescape() const
Definition: json.h:336
const rvalue & operator[](size_t index) const
Definition: json.h:491
bool is_cached() const
Definition: json.h:554
r_string & operator=(r_string &&r)
Definition: json.h:129
basic_json<> json
default JSON class
Definition: json.hpp:14369
bool operator>(const r_string &l, const std::string &r)
Definition: json.h:187
int64_t i() const
Definition: json.h:288
std::unique_ptr< std::unordered_map< std::string, wvalue > > o
Definition: json.h:1096
bool operator==(const r_string &l, const r_string &r)
Definition: json.h:192
bool operator==(const rvalue &l, const std::string &r)
Definition: json.h:649
wvalue & operator[](unsigned index)
Definition: json.h:1283
#define crow_json_likely(x)
Definition: json.h:19
#define crow_json_unlikely(x)
Definition: json.h:20
detail::r_string key_
Definition: json.h:594
#define s(x, c)
Definition: aesb.c:46
void dump_string(const std::string &str, std::string &out)
Definition: json.h:1362
bool has(const char *str) const
Definition: json.h:411
r_string()
Definition: json.h:109
double d() const
Definition: json.h:318
static const int cached_bit
Definition: json.h:205
friend std::ostream & operator<<(std::ostream &os, const rvalue &r)
Definition: json.h:603
int count(const std::string &str)
Definition: json.h:1295