277 static const int cached_bit = 2;
278 static const int error_bit = 4;
286 lsize_{}, lremain_{}, t_{
t}
289 rvalue(type
t,
char*
s,
char* e)
noexcept:
290 start_{
s}, end_{e}, t_{
t}
292 determine_num_type();
296 start_(r.start_), end_(r.end_), key_(r.key_), t_(r.t_), nt_(r.nt_), option_(r.option_)
303 *
this = std::move(r);
321 key_ = std::move(r.key_);
322 l_ = std::move(r.l_);
324 lremain_ = r.lremain_;
331 explicit operator bool()
const noexcept
333 return (option_ & error_bit) == 0;
336 explicit operator int64_t()
const
341 explicit operator uint64_t()
const
346 explicit operator int()
const
348 return static_cast<int>(
i());
352 explicit operator std::string()
const
354#ifndef CROW_JSON_NO_ERROR_CHECK
355 if (
t() == type::Object ||
t() == type::List)
356 throw std::runtime_error(
"json type container");
361 return std::string(
s());
363 return std::string(
"null");
365 return std::string(
"true");
367 return std::string(
"false");
369 return std::string(start_, end_ - start_);
376#ifndef CROW_JSON_NO_ERROR_CHECK
377 if (option_ & error_bit)
379 throw std::runtime_error(
"invalid json object");
388#ifndef CROW_JSON_NO_ERROR_CHECK
389 if (option_ & error_bit)
391 throw std::runtime_error(
"invalid json object");
400#ifndef CROW_JSON_NO_ERROR_CHECK
405 return utility::lexical_cast<int64_t>(start_, end_ - start_);
407 const std::string msg =
"expected number, got: " + std::string(get_type_str(
t()));
408 throw std::runtime_error(msg);
411 return utility::lexical_cast<int64_t>(start_, end_ - start_);
417#ifndef CROW_JSON_NO_ERROR_CHECK
422 return utility::lexical_cast<uint64_t>(start_, end_ - start_);
424 throw std::runtime_error(std::string(
"expected number, got: ") + get_type_str(
t()));
427 return utility::lexical_cast<uint64_t>(start_, end_ - start_);
433#ifndef CROW_JSON_NO_ERROR_CHECK
434 if (
t() != type::Number)
435 throw std::runtime_error(
"value is not number");
437 return utility::lexical_cast<double>(start_, end_ - start_);
443#ifndef CROW_JSON_NO_ERROR_CHECK
444 if (
t() != type::True &&
t() != type::False)
445 throw std::runtime_error(
"value is not boolean");
447 return t() == type::True;
453#ifndef CROW_JSON_NO_ERROR_CHECK
454 if (
t() != type::String)
455 throw std::runtime_error(
"value is not string");
462 std::vector<rvalue>
lo()
464#ifndef CROW_JSON_NO_ERROR_CHECK
465 if (
t() != type::Object &&
t() != type::List)
466 throw std::runtime_error(
"value is not a container");
468 std::vector<rvalue> ret;
470 for (uint32_t
i = 0;
i < lsize_;
i++)
472 ret.emplace_back(l_[
i]);
490 case '"': *tail++ =
'"';
break;
491 case '\\': *tail++ =
'\\';
break;
492 case '/': *tail++ =
'/';
break;
493 case 'b': *tail++ =
'\b';
break;
494 case 'f': *tail++ =
'\f';
break;
495 case 'n': *tail++ =
'\n';
break;
496 case 'r': *tail++ =
'\r';
break;
497 case 't': *tail++ =
'\t';
break;
500 auto from_hex = [](
char c) {
508 (from_hex(head[1]) << 12) +
509 (from_hex(head[2]) << 8) +
510 (from_hex(head[3]) << 4) +
514 *tail++ = 0xE0 | (code >> 12);
515 *tail++ = 0x80 | ((code >> 6) & 0x3F);
516 *tail++ = 0x80 | (code & 0x3F);
518 else if (code >= 0x80)
520 *tail++ = 0xC0 | (code >> 6);
521 *tail++ = 0x80 | (code & 0x3F);
543 bool has(
const char* str)
const
545 return has(std::string(str));
548 bool has(
const std::string& str)
const
554 return l.key_ < r.key_;
556 bool operator()(
const rvalue& l,
const std::string& r)
const
560 bool operator()(
const std::string& l,
const rvalue& r)
const
567 std::sort(begin(), end(), Pred());
570 auto it = lower_bound(begin(), end(), str, Pred());
571 return it != end() && it->key_ == str;
574 int count(
const std::string& str)
576 return has(str) ? 1 : 0;
579 rvalue* begin()
const
581#ifndef CROW_JSON_NO_ERROR_CHECK
582 if (
t() != type::Object &&
t() != type::List)
583 throw std::runtime_error(
"value is not a container");
589#ifndef CROW_JSON_NO_ERROR_CHECK
590 if (
t() != type::Object &&
t() != type::List)
591 throw std::runtime_error(
"value is not a container");
593 return l_.get() + lsize_;
596 const detail::r_string& key()
const
603 if (
t() == type::String)
605#ifndef CROW_JSON_NO_ERROR_CHECK
606 if (
t() != type::Object &&
t() != type::List)
607 throw std::runtime_error(
"value is not a container");
612 const rvalue& operator[](
int index)
const
614#ifndef CROW_JSON_NO_ERROR_CHECK
615 if (
t() != type::List)
616 throw std::runtime_error(
"value is not a list");
617 if (index >=
static_cast<int>(lsize_) || index < 0)
618 throw std::runtime_error(
"list out of bound");
623 const rvalue& operator[](
size_t index)
const
625#ifndef CROW_JSON_NO_ERROR_CHECK
626 if (
t() != type::List)
627 throw std::runtime_error(
"value is not a list");
629 throw std::runtime_error(
"list out of bound");
634 const rvalue& operator[](
const char* str)
const
636 return this->operator[](std::string(str));
639 const rvalue& operator[](
const std::string& str)
const
641#ifndef CROW_JSON_NO_ERROR_CHECK
642 if (
t() != type::Object)
643 throw std::runtime_error(
"value is not an object");
647 bool operator()(
const rvalue& l,
const rvalue& r)
const
649 return l.key_ < r.key_;
651 bool operator()(
const rvalue& l,
const std::string& r)
const
655 bool operator()(
const std::string& l,
const rvalue& r)
const
662 std::sort(begin(), end(), Pred());
665 auto it = lower_bound(begin(), end(), str, Pred());
666 if (it != end() && it->key_ == str)
668#ifndef CROW_JSON_NO_ERROR_CHECK
669 throw std::runtime_error(
"cannot find key");
671 static rvalue nullValue;
678 option_ |= error_bit;
683 return (option_ & error_bit) != 0;
686 std::vector<std::string> keys()
const
688#ifndef CROW_JSON_NO_ERROR_CHECK
689 if (
t() != type::Object)
690 throw std::runtime_error(
"value is not an object");
692 std::vector<std::string> ret;
694 for (uint32_t
i = 0;
i < lsize_;
i++)
696 ret.emplace_back(std::string(l_[
i].key()));
702 bool is_cached()
const
704 return (option_ & cached_bit) != 0;
706 void set_cached()
const
708 option_ |= cached_bit;
710 void copy_l(
const rvalue& r)
712 if (r.t() != type::Object && r.t() != type::List)
716 l_.reset(
new rvalue[lsize_]);
717 std::copy(r.begin(), r.end(), begin());
720 void emplace_back(rvalue&& v)
724 int new_size = lsize_ + lsize_;
725 if (new_size - lsize_ > 60000)
726 new_size = lsize_ + 60000;
729 rvalue* p =
new rvalue[new_size];
731 for (
auto& x : *this)
732 *p2++ = std::move(x);
734 lremain_ = new_size - lsize_;
736 l_[lsize_++] = std::move(v);
741 void determine_num_type()
743 if (t_ != type::Number)
745 nt_ = num_type::Null;
749 const std::size_t len = end_ - start_;
750 const bool has_minus = std::memchr(start_,
'-', len) !=
nullptr;
751 const bool has_e = std::memchr(start_,
'e', len) !=
nullptr || std::memchr(start_,
'E', len) !=
nullptr;
752 const bool has_dec_sep = std::memchr(start_,
'.', len) !=
nullptr;
753 if (has_dec_sep || has_e)
754 nt_ = num_type::Floating_point;
756 nt_ = num_type::Signed_integer;
758 nt_ = num_type::Unsigned_integer;
761 mutable char* start_;
763 detail::r_string key_;
764 std::unique_ptr<rvalue[]> l_;
768 num_type nt_{num_type::Null};
769 mutable uint8_t option_{0};
771 friend rvalue load_nocopy_internal(
char* data,
size_t size);
772 friend rvalue load(
const char* data,
size_t size);
773 friend std::ostream& operator<<(std::ostream& os,
const rvalue& r)
778 case type::Null: os <<
"null";
break;
779 case type::False: os <<
"false";
break;
780 case type::True: os <<
"true";
break;
785 case num_type::Floating_point: os << r.d();
break;
786 case num_type::Double_precision_floating_point: os << r.d();
break;
787 case num_type::Signed_integer: os << r.i();
break;
788 case num_type::Unsigned_integer: os << r.u();
break;
789 case num_type::Null:
throw std::runtime_error(
"Number with num_type Null");
793 case type::String: os <<
'"' << r.s() <<
'"';
break;
816 os <<
'"' << escape(x.key_) <<
"\":";
823 case type::Function: os <<
"custom function";
break;
1295#ifdef CROW_JSON_USE_MAP
1296 std::map<std::string, wvalue>;
1298 std::unordered_map<std::string, wvalue>;
1301 using list = std::vector<wvalue>;
1303 type t()
const {
return t_; }
1309 type t_{type::Null};
1310 num_type nt{num_type::Null};
1318 constexpr number() noexcept:
1320 constexpr number(std::uint64_t value)
noexcept:
1322 constexpr number(std::int64_t value)
noexcept:
1324 explicit constexpr number(
double value)
noexcept:
1326 explicit constexpr number(
float value)
noexcept:
1330 std::unique_ptr<list> l;
1331 std::unique_ptr<object> o;
1332 std::function<std::string(std::string&)> f;
1336 returnable(
"application/json") {}
1338 wvalue(std::nullptr_t):
1339 returnable(
"application/json"), t_(type::Null) {}
1342 returnable(
"application/json"), t_(value ? type::True : type::False) {}
1344 wvalue(std::uint8_t value):
1345 returnable(
"application/json"), t_(type::Number), nt(num_type::Unsigned_integer), num(static_cast<std::uint64_t>(value)) {}
1346 wvalue(std::uint16_t value):
1347 returnable(
"application/json"), t_(type::Number), nt(num_type::Unsigned_integer), num(static_cast<std::uint64_t>(value)) {}
1348 wvalue(std::uint32_t value):
1349 returnable(
"application/json"), t_(type::Number), nt(num_type::Unsigned_integer), num(static_cast<std::uint64_t>(value)) {}
1350 wvalue(std::uint64_t value):
1351 returnable(
"application/json"), t_(type::Number), nt(num_type::Unsigned_integer), num(static_cast<std::uint64_t>(value)) {}
1353 wvalue(std::int8_t value):
1354 returnable(
"application/json"), t_(type::Number), nt(num_type::Signed_integer), num(static_cast<std::int64_t>(value)) {}
1355 wvalue(std::int16_t value):
1356 returnable(
"application/json"), t_(type::Number), nt(num_type::Signed_integer), num(static_cast<std::int64_t>(value)) {}
1357 wvalue(std::int32_t value):
1358 returnable(
"application/json"), t_(type::Number), nt(num_type::Signed_integer), num(static_cast<std::int64_t>(value)) {}
1359 wvalue(std::int64_t value):
1360 returnable(
"application/json"), t_(type::Number), nt(num_type::Signed_integer), num(static_cast<std::int64_t>(value)) {}
1362 wvalue(
float value):
1363 returnable(
"application/json"), t_(type::Number), nt(num_type::Floating_point), num(static_cast<double>(value)) {}
1364 wvalue(
double value):
1365 returnable(
"application/json"), t_(type::Number), nt(num_type::Double_precision_floating_point), num(static_cast<double>(value)) {}
1367 wvalue(
char const* value):
1368 returnable(
"application/json"), t_(type::String), s(value) {}
1370 wvalue(std::string
const& value):
1371 returnable(
"application/json"), t_(type::String), s(value) {}
1372 wvalue(std::string&& value):
1373 returnable(
"application/json"), t_(type::String), s(std::move(value)) {}
1375 wvalue(std::initializer_list<std::pair<std::string const, wvalue>> initializer_list):
1376 returnable(
"application/json"), t_(type::Object), o(new object(initializer_list)) {}
1378 wvalue(
object const& value):
1379 returnable(
"application/json"), t_(type::Object), o(new object(value)) {}
1380 wvalue(
object&& value):
1381 returnable(
"application/json"), t_(type::Object), o(new object(std::move(value))) {}
1383 wvalue(
const list& r):
1384 returnable(
"application/json")
1387 l = std::unique_ptr<list>(
new list{});
1388 l->reserve(r.size());
1389 for (
auto it = r.begin(); it != r.end(); ++it)
1390 l->emplace_back(*it);
1393 returnable(
"application/json")
1396 l = std::unique_ptr<list>(
new list{});
1397 l->reserve(r.size());
1398 for (
auto it = r.begin(); it != r.end(); ++it)
1399 l->emplace_back(*it);
1412 case type::Function:
1416 if (nt == num_type::Floating_point || nt == num_type::Double_precision_floating_point)
1418 else if (nt == num_type::Signed_integer)
1427 l = std::unique_ptr<list>(
new list{});
1428 l->reserve(r.size());
1429 for (
auto it = r.begin(); it != r.end(); ++it)
1430 l->emplace_back(*it);
1433 o = std::unique_ptr<object>(
new object{});
1434 for (
auto it = r.begin(); it != r.end(); ++it)
1435 o->emplace(it->key(), *it);
1452 if (nt == num_type::Floating_point || nt == num_type::Double_precision_floating_point)
1454 else if (nt == num_type::Signed_integer)
1463 l = std::unique_ptr<list>(
new list{});
1464 l->reserve(r.
size());
1465 for (
auto it = r.l->begin(); it != r.l->end(); ++it)
1466 l->emplace_back(*it);
1469 o = std::unique_ptr<object>(
new object{});
1470 o->insert(r.o->begin(), r.o->end());
1472 case type::Function:
1478 returnable(
"application/json")
1480 *
this = std::move(r);
1483 wvalue& operator=(wvalue&& r)
1507 wvalue& operator=(std::nullptr_t)
1512 wvalue& operator=(
bool value)
1522 wvalue& operator=(
float value)
1527 nt = num_type::Floating_point;
1531 wvalue& operator=(
double value)
1536 nt = num_type::Double_precision_floating_point;
1540 wvalue& operator=(
unsigned short value)
1545 nt = num_type::Unsigned_integer;
1549 wvalue& operator=(
short value)
1554 nt = num_type::Signed_integer;
1558 wvalue& operator=(
long long value)
1563 nt = num_type::Signed_integer;
1567 wvalue& operator=(
long value)
1572 nt = num_type::Signed_integer;
1576 wvalue& operator=(
int value)
1581 nt = num_type::Signed_integer;
1585 wvalue& operator=(
unsigned long long value)
1590 nt = num_type::Unsigned_integer;
1594 wvalue& operator=(
unsigned long value)
1599 nt = num_type::Unsigned_integer;
1603 wvalue& operator=(
unsigned int value)
1608 nt = num_type::Unsigned_integer;
1612 wvalue& operator=(
const char* str)
1620 wvalue& operator=(
const std::string& str)
1628 wvalue& operator=(list&& v)
1630 if (t_ != type::List)
1634 l = std::unique_ptr<list>(
new list{});
1636 l->resize(v.size());
1640 (*l)[idx++] = std::move(x);
1645 template<
typename T>
1646 wvalue& operator=(
const std::vector<T>& v)
1648 if (t_ != type::List)
1652 l = std::unique_ptr<list>(
new list{});
1654 l->resize(v.size());
1663 wvalue& operator=(std::initializer_list<std::pair<std::string const, wvalue>> initializer_list)
1665 if (t_ != type::Object)
1669 o = std::unique_ptr<object>(
new object(initializer_list));
1673#if defined(__APPLE__) || defined(__MACH__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(_LIBCPP_VERSION)
1674 o = std::unique_ptr<object>(
new object(initializer_list));
1676 (*o) = initializer_list;
1682 wvalue& operator=(
object const& value)
1684 if (t_ != type::Object)
1688 o = std::unique_ptr<object>(
new object(value));
1692#if defined(__APPLE__) || defined(__MACH__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(_LIBCPP_VERSION)
1693 o = std::unique_ptr<object>(
new object(value));
1701 wvalue& operator=(
object&& value)
1703 if (t_ != type::Object)
1707 o = std::unique_ptr<object>(
new object(std::move(value)));
1711 (*o) = std::move(value);
1716 wvalue& operator=(std::function<std::string(std::string&)>&& func)
1719 t_ = type::Function;
1720 f = std::move(func);
1724 wvalue& operator[](
unsigned index)
1726 if (t_ != type::List)
1730 l = std::unique_ptr<list>(
new list{});
1731 if (l->size() < index + 1)
1732 l->resize(index + 1);
1736 const wvalue& operator[](
unsigned index)
const
1738 return const_cast<wvalue*
>(
this)->
operator[](index);
1741 int count(
const std::string& str)
const
1743 if (t_ != type::Object)
1747 return o->count(str);
1750 wvalue& operator[](
const std::string& str)
1752 if (t_ != type::Object)
1756 o = std::unique_ptr<object>(
new object{});
1760 const wvalue& operator[](
const std::string& str)
const
1762 return const_cast<wvalue*
>(
this)->
operator[](str);
1765 std::vector<std::string> keys()
const
1767 if (t_ != type::Object)
1769 std::vector<std::string> result;
1772 result.push_back(kv.first);
1777 std::string execute(std::string txt =
"") const
1779 if (t_ != type::Function)
1787 if (t_ != type::List)
1797 case type::Null:
return 4;
1798 case type::False:
return 5;
1799 case type::True:
return 4;
1800 case type::Number:
return 30;
1801 case type::String:
return 2 + s.size() + s.size() / 2;
1810 sum += x.estimate_length();
1823 sum += 2 + kv.first.size() + kv.first.size() / 2;
1824 sum += kv.second.estimate_length();
1829 case type::Function:
1836 inline void dump_string(
const std::string& str, std::string& out)
const
1843 inline void dump_indentation_part(std::string& out,
const int indent,
const char separator,
const int indent_level)
const
1845 out.push_back(
'\n');
1846 out.append(indent_level * indent, separator);
1850 inline void dump_internal(
const wvalue& v, std::string& out,
const int indent,
const char separator,
const int indent_level = 0)
const
1854 case type::Null: out +=
"null";
break;
1855 case type::False: out +=
"false";
break;
1856 case type::True: out +=
"true";
break;
1859 if (v.nt == num_type::Floating_point || v.nt == num_type::Double_precision_floating_point)
1861 if (isnan(v.num.d) || isinf(v.num.d))
1864 CROW_LOG_WARNING <<
"Invalid JSON value detected (" << v.num.d <<
"), value set to null";
1874 if (v.nt == num_type::Double_precision_floating_point)
1877 sprintf_s(outbuf,
sizeof(outbuf),
"%.*g", DECIMAL_DIG, v.num.d);
1879 snprintf(outbuf,
sizeof(outbuf),
"%.*g", DECIMAL_DIG, v.num.d);
1885 sprintf_s(outbuf,
sizeof(outbuf),
"%f", v.num.d);
1887 snprintf(outbuf,
sizeof(outbuf),
"%f", v.num.d);
1890 char *p = &outbuf[0], *o =
nullptr;
1901 char fch = *(p + 1);
1903 if (fch !=
'\0' && fch ==
'0') p++;
1930 else if (v.nt == num_type::Signed_integer)
1932 out += std::to_string(v.num.si);
1936 out += std::to_string(v.num.ui);
1940 case type::String: dump_string(v.s, out);
break;
1947 dump_indentation_part(out, indent, separator, indent_level + 1);
1953 for (
auto& x : *v.l)
1961 dump_indentation_part(out, indent, separator, indent_level + 1);
1965 dump_internal(x, out, indent, separator, indent_level + 1);
1971 dump_indentation_part(out, indent, separator, indent_level);
1983 dump_indentation_part(out, indent, separator, indent_level + 1);
1989 for (
auto& kv : *v.o)
1996 dump_indentation_part(out, indent, separator, indent_level + 1);
2000 dump_string(kv.first, out);
2008 dump_internal(kv.second, out, indent, separator, indent_level + 1);
2014 dump_indentation_part(out, indent, separator, indent_level);
2021 case type::Function:
2022 out +=
"custom function";
2028 std::string dump(
const int indent,
const char separator =
' ')
const
2032 dump_internal(*
this, ret, indent, separator);
2036 std::string dump()
const
2038 static constexpr int DontIndent = -1;
2040 return dump(DontIndent);