323 static const int cached_bit = 2;
324 static const int error_bit = 4;
332 lsize_{}, lremain_{}, t_{
t}
335 rvalue(type
t,
char*
s,
char* e)
noexcept:
336 start_{
s}, end_{e}, t_{
t}
338 determine_num_type();
342 start_(r.start_), end_(r.end_), key_(r.key_), t_(r.t_), nt_(r.nt_), option_(r.option_)
349 *
this = std::move(r);
367 key_ = std::move(r.key_);
368 l_ = std::move(r.l_);
370 lremain_ = r.lremain_;
377 explicit operator bool()
const noexcept
379 return (option_ & error_bit) == 0;
382 explicit operator int64_t()
const
387 explicit operator uint64_t()
const
392 explicit operator int()
const
394 return static_cast<int>(
i());
398 explicit operator std::string()
const
400#ifndef CROW_JSON_NO_ERROR_CHECK
401 if (
t() == type::Object ||
t() == type::List)
402 throw std::runtime_error(
"json type container");
407 return std::string(
s());
409 return std::string(
"null");
411 return std::string(
"true");
413 return std::string(
"false");
415 return std::string(start_, end_ - start_);
422#ifndef CROW_JSON_NO_ERROR_CHECK
423 if (option_ & error_bit)
425 throw std::runtime_error(
"invalid json object");
434#ifndef CROW_JSON_NO_ERROR_CHECK
435 if (option_ & error_bit)
437 throw std::runtime_error(
"invalid json object");
446#ifndef CROW_JSON_NO_ERROR_CHECK
451 return utility::lexical_cast<int64_t>(start_, end_ - start_);
453 const std::string msg =
"expected number, got: " + std::string(get_type_str(
t()));
454 throw std::runtime_error(msg);
457 return utility::lexical_cast<int64_t>(start_, end_ - start_);
464#ifndef CROW_JSON_NO_ERROR_CHECK
469 return utility::lexical_cast<uint64_t>(start_, end_ - start_);
471 throw std::runtime_error(std::string(
"expected number, got: ") + get_type_str(
t()));
474 return utility::lexical_cast<uint64_t>(start_, end_ - start_);
480#ifndef CROW_JSON_NO_ERROR_CHECK
481 if (
t() != type::Number)
482 throw std::runtime_error(
"value is not number");
484 return utility::lexical_cast<double>(start_, end_ - start_);
490#ifndef CROW_JSON_NO_ERROR_CHECK
491 if (
t() != type::True &&
t() != type::False)
492 throw std::runtime_error(
"value is not boolean");
494 return t() == type::True;
500#ifndef CROW_JSON_NO_ERROR_CHECK
501 if (
t() != type::String)
502 throw std::runtime_error(
"value is not string");
509 std::vector<rvalue>
lo()
const
511#ifndef CROW_JSON_NO_ERROR_CHECK
512 if (
t() != type::Object &&
t() != type::List)
513 throw std::runtime_error(
"value is not a container");
515 std::vector<rvalue> ret;
517 for (uint32_t
i = 0;
i < lsize_;
i++)
519 ret.emplace_back(l_[
i]);
537 case '"': *tail++ =
'"';
break;
538 case '\\': *tail++ =
'\\';
break;
539 case '/': *tail++ =
'/';
break;
540 case 'b': *tail++ =
'\b';
break;
541 case 'f': *tail++ =
'\f';
break;
542 case 'n': *tail++ =
'\n';
break;
543 case 'r': *tail++ =
'\r';
break;
544 case 't': *tail++ =
'\t';
break;
547 auto from_hex = [](
char c) {
555 (from_hex(head[1]) << 12) +
556 (from_hex(head[2]) << 8) +
557 (from_hex(head[3]) << 4) +
560 if (code >= 0xD800 && code <= 0xDFFF)
565 unsigned int low = 0;
566 if (code <= 0xDBFF && head[1] ==
'\\' && head[2] ==
'u')
568 low = (from_hex(head[3]) << 12) +
569 (from_hex(head[4]) << 8) +
570 (from_hex(head[5]) << 4) +
573 if (low >= 0xDC00 && low <= 0xDFFF)
575 code = 0x10000 + ((code - 0xD800) << 10) + (low - 0xDC00);
576 *tail++ = 0xF0 | (code >> 18);
577 *tail++ = 0x80 | ((code >> 12) & 0x3F);
578 *tail++ = 0x80 | ((code >> 6) & 0x3F);
579 *tail++ = 0x80 | (code & 0x3F);
589 else if (code >= 0x800)
591 *tail++ = 0xE0 | (code >> 12);
592 *tail++ = 0x80 | ((code >> 6) & 0x3F);
593 *tail++ = 0x80 | (code & 0x3F);
595 else if (code >= 0x80)
597 *tail++ = 0xC0 | (code >> 6);
598 *tail++ = 0x80 | (code & 0x3F);
619 bool has(
const char* str)
const
621 return has(std::string(str));
624 bool has(
const std::string& str)
const
630 return l.key_ < r.key_;
632 bool operator()(
const rvalue& l,
const std::string& r)
const
636 bool operator()(
const std::string& l,
const rvalue& r)
const
643 std::sort(begin(), end(), Pred());
646 auto it = lower_bound(begin(), end(), str, Pred());
647 return it != end() && it->key_ == str;
650 int count(
const std::string& str)
const
652 return has(str) ? 1 : 0;
655 rvalue* begin()
const
657#ifndef CROW_JSON_NO_ERROR_CHECK
658 if (
t() != type::Object &&
t() != type::List)
659 throw std::runtime_error(
"value is not a container");
665#ifndef CROW_JSON_NO_ERROR_CHECK
666 if (
t() != type::Object &&
t() != type::List)
667 throw std::runtime_error(
"value is not a container");
669 return l_.get() + lsize_;
672 const detail::r_string& key()
const
679 if (
t() == type::String)
681#ifndef CROW_JSON_NO_ERROR_CHECK
682 if (
t() != type::Object &&
t() != type::List)
683 throw std::runtime_error(
"value is not a container");
688 const rvalue& operator[](
int index)
const
690#ifndef CROW_JSON_NO_ERROR_CHECK
691 if (
t() != type::List)
692 throw std::runtime_error(
"value is not a list");
693 if (index >=
static_cast<int>(lsize_) || index < 0)
694 throw std::runtime_error(
"list out of bound");
699 const rvalue& operator[](
size_t index)
const
701#ifndef CROW_JSON_NO_ERROR_CHECK
702 if (
t() != type::List)
703 throw std::runtime_error(
"value is not a list");
705 throw std::runtime_error(
"list out of bound");
710 const rvalue& operator[](
const char* str)
const
712 return this->operator[](std::string(str));
715 const rvalue& operator[](
const std::string& str)
const
717#ifndef CROW_JSON_NO_ERROR_CHECK
718 if (
t() != type::Object)
719 throw std::runtime_error(
"value is not an object");
723 bool operator()(
const rvalue& l,
const rvalue& r)
const
725 return l.key_ < r.key_;
727 bool operator()(
const rvalue& l,
const std::string& r)
const
731 bool operator()(
const std::string& l,
const rvalue& r)
const
738 std::sort(begin(), end(), Pred());
741 auto it = lower_bound(begin(), end(), str, Pred());
742 if (it != end() && it->key_ == str)
744#ifndef CROW_JSON_NO_ERROR_CHECK
745 throw std::runtime_error(
"cannot find key: " + str);
747 static rvalue nullValue;
754 option_ |= error_bit;
759 return (option_ & error_bit) != 0;
762 std::vector<std::string> keys()
const
764#ifndef CROW_JSON_NO_ERROR_CHECK
765 if (
t() != type::Object)
766 throw std::runtime_error(
"value is not an object");
768 std::vector<std::string> ret;
770 for (uint32_t
i = 0;
i < lsize_;
i++)
772 ret.emplace_back(std::string(l_[
i].key()));
778 bool is_cached()
const
780 return (option_ & cached_bit) != 0;
782 void set_cached()
const
784 option_ |= cached_bit;
786 void copy_l(
const rvalue& r)
788 if (r.t() != type::Object && r.t() != type::List)
792 l_.reset(
new rvalue[lsize_]);
793 std::copy(r.begin(), r.end(), begin());
796 void emplace_back(rvalue&& v)
800 int new_size = lsize_ + lsize_;
801 if (new_size - lsize_ > 60000)
802 new_size = lsize_ + 60000;
805 rvalue* p =
new rvalue[new_size];
807 for (
auto& x : *this)
808 *p2++ = std::move(x);
810 lremain_ = new_size - lsize_;
812 l_[lsize_++] = std::move(v);
817 void determine_num_type()
819 if (t_ != type::Number)
821 nt_ = num_type::Null;
825 const std::size_t len = end_ - start_;
826 const bool has_minus = std::memchr(start_,
'-', len) !=
nullptr;
827 const bool has_e = std::memchr(start_,
'e', len) !=
nullptr || std::memchr(start_,
'E', len) !=
nullptr;
828 const bool has_dec_sep = std::memchr(start_,
'.', len) !=
nullptr;
829 if (has_dec_sep || has_e)
830 nt_ = num_type::Floating_point;
832 nt_ = num_type::Signed_integer;
834 nt_ = num_type::Unsigned_integer;
837 mutable char* start_;
839 detail::r_string key_;
840 std::unique_ptr<rvalue[]> l_;
844 num_type nt_{num_type::Null};
845 mutable uint8_t option_{0};
847 friend rvalue load_nocopy_internal(
char* data,
size_t size);
848 friend rvalue load(
const char* data,
size_t size);
849 friend std::ostream& operator<<(std::ostream& os,
const rvalue& r)
854 case type::Null: os <<
"null";
break;
855 case type::False: os <<
"false";
break;
856 case type::True: os <<
"true";
break;
861 case num_type::Floating_point: os << r.d();
break;
862 case num_type::Double_precision_floating_point: os << r.d();
break;
863 case num_type::Signed_integer: os << r.i();
break;
864 case num_type::Unsigned_integer: os << r.u();
break;
865 case num_type::Null:
throw std::runtime_error(
"Number with num_type Null");
869 case type::String: os <<
'"' << r.s() <<
'"';
break;
892 os <<
'"' << escape(x.key_) <<
"\":";
899 case type::Function: os <<
"custom function";
break;
1383 using object = detail::object_policy::object_type<wvalue>;
1385 using list = std::vector<wvalue>;
1387 type t()
const {
return t_; }
1393 type t_{type::Null};
1394 num_type nt{num_type::Null};
1402 constexpr number() noexcept:
1404 constexpr number(std::uint64_t value)
noexcept:
1406 constexpr number(std::int64_t value)
noexcept:
1408 explicit constexpr number(
double value)
noexcept:
1410 explicit constexpr number(
float value)
noexcept:
1414 std::unique_ptr<list> l;
1415 std::unique_ptr<object> o;
1416 std::function<std::string(std::string&)> f;
1420 returnable(
"application/json") {}
1422 wvalue(std::nullptr_t):
1423 returnable(
"application/json"), t_(type::Null) {}
1426 returnable(
"application/json"), t_(value ? type::True : type::False) {}
1428 wvalue(std::uint8_t value):
1429 returnable(
"application/json"), t_(type::Number), nt(num_type::Unsigned_integer), num(static_cast<std::uint64_t>(value)) {}
1430 wvalue(std::uint16_t value):
1431 returnable(
"application/json"), t_(type::Number), nt(num_type::Unsigned_integer), num(static_cast<std::uint64_t>(value)) {}
1432 wvalue(std::uint32_t value):
1433 returnable(
"application/json"), t_(type::Number), nt(num_type::Unsigned_integer), num(static_cast<std::uint64_t>(value)) {}
1434 wvalue(std::uint64_t value):
1435 returnable(
"application/json"), t_(type::Number), nt(num_type::Unsigned_integer), num(static_cast<std::uint64_t>(value)) {}
1437 wvalue(std::int8_t value):
1438 returnable(
"application/json"), t_(type::Number), nt(num_type::Signed_integer), num(static_cast<std::int64_t>(value)) {}
1439 wvalue(std::int16_t value):
1440 returnable(
"application/json"), t_(type::Number), nt(num_type::Signed_integer), num(static_cast<std::int64_t>(value)) {}
1441 wvalue(std::int32_t value):
1442 returnable(
"application/json"), t_(type::Number), nt(num_type::Signed_integer), num(static_cast<std::int64_t>(value)) {}
1443 wvalue(std::int64_t value):
1444 returnable(
"application/json"), t_(type::Number), nt(num_type::Signed_integer), num(static_cast<std::int64_t>(value)) {}
1446 wvalue(
float value):
1447 returnable(
"application/json"), t_(type::Number), nt(num_type::Floating_point), num(static_cast<double>(value)) {}
1448 wvalue(
double value):
1449 returnable(
"application/json"), t_(type::Number), nt(num_type::Double_precision_floating_point), num(static_cast<double>(value)) {}
1451 wvalue(
char const* value):
1452 returnable(
"application/json"), t_(type::String), s(value) {}
1454 wvalue(std::string
const& value):
1455 returnable(
"application/json"), t_(type::String), s(value) {}
1456 wvalue(std::string&& value):
1457 returnable(
"application/json"), t_(type::String), s(std::move(value)) {}
1459 wvalue(std::initializer_list<std::pair<std::string const, wvalue>> initializer_list):
1460 returnable(
"application/json"), t_(type::Object), o(new object(initializer_list)) {}
1462 wvalue(
object const& value):
1463 returnable(
"application/json"), t_(type::Object), o(new object(value)) {}
1464 wvalue(
object&& value):
1465 returnable(
"application/json"), t_(type::Object), o(new object(std::move(value))) {}
1467 wvalue(
const list& r):
1468 returnable(
"application/json")
1471 l = std::make_unique<list>();
1472 l->reserve(r.size());
1473 for (
auto it = r.begin(); it != r.end(); ++it)
1474 l->emplace_back(*it);
1477 returnable(
"application/json")
1480 l = std::make_unique<list>();
1481 l->reserve(r.size());
1482 for (
auto it = r.begin(); it != r.end(); ++it)
1483 l->emplace_back(*it);
1496 case type::Function:
1500 if (nt == num_type::Floating_point || nt == num_type::Double_precision_floating_point)
1502 else if (nt == num_type::Signed_integer)
1511 l = std::make_unique<list>();
1512 l->reserve(r.size());
1513 for (
auto it = r.begin(); it != r.end(); ++it)
1514 l->emplace_back(*it);
1517 o = std::make_unique<object>();
1518 for (
auto it = r.begin(); it != r.end(); ++it)
1519 o->emplace(it->key(), *it);
1536 if (nt == num_type::Floating_point || nt == num_type::Double_precision_floating_point)
1538 else if (nt == num_type::Signed_integer)
1547 l = std::make_unique<list>();
1548 l->reserve(r.
size());
1549 for (
auto it = r.l->begin(); it != r.l->end(); ++it)
1550 l->emplace_back(*it);
1553 o = std::make_unique<object>();
1554 o->insert(r.o->begin(), r.o->end());
1556 case type::Function:
1562 returnable(
"application/json")
1564 *
this = std::move(r);
1567 wvalue& operator=(wvalue&& r)
1591 wvalue& operator=(std::nullptr_t)
1596 wvalue& operator=(
bool value)
1606 wvalue& operator=(
float value)
1611 nt = num_type::Floating_point;
1615 wvalue& operator=(
double value)
1620 nt = num_type::Double_precision_floating_point;
1624 wvalue& operator=(
unsigned short value)
1629 nt = num_type::Unsigned_integer;
1633 wvalue& operator=(
short value)
1638 nt = num_type::Signed_integer;
1642 wvalue& operator=(
long long value)
1647 nt = num_type::Signed_integer;
1651 wvalue& operator=(
long value)
1656 nt = num_type::Signed_integer;
1660 wvalue& operator=(
int value)
1665 nt = num_type::Signed_integer;
1669 wvalue& operator=(
unsigned long long value)
1674 nt = num_type::Unsigned_integer;
1678 wvalue& operator=(
unsigned long value)
1683 nt = num_type::Unsigned_integer;
1687 wvalue& operator=(
unsigned int value)
1692 nt = num_type::Unsigned_integer;
1696 wvalue& operator=(
const char* str)
1704 wvalue& operator=(
const std::string& str)
1712 wvalue& operator=(list&& v)
1714 if (t_ != type::List)
1718 l = std::make_unique<list>();
1720 l->resize(v.size());
1724 (*l)[idx++] = std::move(x);
1729 template<
typename T>
1730 wvalue& operator=(
const std::vector<T>& v)
1732 if (t_ != type::List)
1736 l = std::make_unique<list>();
1738 l->resize(v.size());
1747 wvalue& operator=(std::initializer_list<std::pair<std::string const, wvalue>> initializer_list)
1749 if (t_ != type::Object)
1753 o = std::make_unique<object>(initializer_list);
1757#if defined(__APPLE__) || defined(__MACH__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(_LIBCPP_VERSION)
1758 o = std::make_unique<object>(initializer_list);
1760 (*o) = initializer_list;
1766 wvalue& operator=(
object const& value)
1768 if (t_ != type::Object)
1772 o = std::make_unique<object>(value);
1776#if defined(__APPLE__) || defined(__MACH__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(_LIBCPP_VERSION)
1777 o = std::make_unique<object>(value);
1785 wvalue& operator=(
object&& value)
1787 if (t_ != type::Object)
1791 o = std::make_unique<object>(std::move(value));
1795 (*o) = std::move(value);
1800 wvalue& operator=(std::function<std::string(std::string&)>&& func)
1803 t_ = type::Function;
1804 f = std::move(func);
1808 wvalue& operator[](
unsigned index)
1810 if (t_ != type::List)
1814 l = std::make_unique<list>();
1815 if (l->size() < index + 1)
1816 l->resize(index + 1);
1820 const wvalue& operator[](
unsigned index)
const
1822 return const_cast<wvalue*
>(
this)->
operator[](index);
1826 bool has(
const char* key)
const
1828 return has(std::string(key));
1832 bool has(
const std::string& key)
const
1834 if (t_ != type::Object)
1838#if (__cplusplus>=202002L)
1839 return o->contains(key);
1841 return o->count(key)>0;
1845 size_t count(
const std::string& str)
const
1847 if (t_ != type::Object)
1851 return o->count(str);
1854 wvalue& operator[](
const std::string& str)
1856 if (t_ != type::Object)
1860 o = std::make_unique<object>();
1864 const wvalue& operator[](
const std::string& str)
const
1866 return const_cast<wvalue*
>(
this)->
operator[](str);
1869 std::vector<std::string> keys()
const
1871 if (t_ != type::Object)
1873 std::vector<std::string> result;
1876 result.push_back(kv.first);
1881 std::string execute(std::string txt =
"") const
1883 if (t_ != type::Function)
1891 if (t_ != type::List)
1901 case type::Null:
return 4;
1902 case type::False:
return 5;
1903 case type::True:
return 4;
1904 case type::Number:
return 30;
1905 case type::String:
return 2 + s.size() + s.size() / 2;
1914 sum += x.estimate_length();
1927 sum += 2 + kv.first.size() + kv.first.size() / 2;
1928 sum += kv.second.estimate_length();
1933 case type::Function:
1940 inline void dump_string(
const std::string& str, std::string& out)
const
1947 inline void dump_indentation_part(std::string& out,
const size_t indent,
const char separator,
const int indent_level)
const
1949 out.push_back(
'\n');
1950 out.append(indent_level * indent, separator);
1954 inline void dump_internal(
const wvalue& v, std::string& out,
const size_t indent,
const char separator,
const int indent_level = 0)
const
1958 case type::Null: out +=
"null";
break;
1959 case type::False: out +=
"false";
break;
1960 case type::True: out +=
"true";
break;
1963 if (v.nt == num_type::Floating_point || v.nt == num_type::Double_precision_floating_point)
1965 if (isnan(v.num.d) || isinf(v.num.d))
1968 CROW_LOG_WARNING <<
"Invalid JSON value detected (" << v.num.d <<
"), value set to null";
1979 if (v.nt == num_type::Double_precision_floating_point)
1982 sprintf_s(outbuf,
sizeof(outbuf),
"%.*g", std::numeric_limits<double>::max_digits10, v.num.d);
1984 snprintf(outbuf,
sizeof(outbuf),
"%.*g", std::numeric_limits<double>::max_digits10, v.num.d);
1990 sprintf_s(outbuf,
sizeof(outbuf),
"%f", v.num.d);
1992 snprintf(outbuf,
sizeof(outbuf),
"%f", v.num.d);
1995 char* p = &outbuf[0];
1996 char* pos_first_trailing_0 =
nullptr;
1997 char* pos_exponent =
nullptr;
2008 char fch = *(p + 1);
2010 if (fch !=
'\0' && fch ==
'0') p++;
2019 pos_first_trailing_0 = p;
2036 pos_first_trailing_0 =
nullptr;
2046 if (pos_first_trailing_0 !=
nullptr)
2048 *pos_first_trailing_0 =
'\0';
2049 if (pos_exponent !=
nullptr)
2051 strcpy(pos_first_trailing_0, pos_exponent);
2056 else if (v.nt == num_type::Signed_integer)
2058 out += std::to_string(v.num.si);
2062 out += std::to_string(v.num.ui);
2066 case type::String: dump_string(v.s, out);
break;
2071 if (indent !=std::string::npos)
2073 dump_indentation_part(out, indent, separator, indent_level + 1);
2079 for (
auto& x : *v.l)
2085 if (indent != std::string::npos)
2087 dump_indentation_part(out, indent, separator, indent_level + 1);
2091 dump_internal(x, out, indent, separator, indent_level + 1);
2095 if (indent !=std::string::npos)
2097 dump_indentation_part(out, indent, separator, indent_level);
2107 if (indent != std::string::npos)
2109 dump_indentation_part(out, indent, separator, indent_level + 1);
2115 for (
auto& kv : *v.o)
2120 if (indent != std::string::npos)
2122 dump_indentation_part(out, indent, separator, indent_level + 1);
2126 dump_string(kv.first, out);
2129 if (indent != std::string::npos)
2134 dump_internal(kv.second, out, indent, separator, indent_level + 1);
2138 if (indent != std::string::npos)
2140 dump_indentation_part(out, indent, separator, indent_level);
2147 case type::Function:
2148 out +=
"custom function";
2154 std::string dump(
const size_t indent,
const char separator =
' ')
const
2158 dump_internal(*
this, ret, indent, separator);
2162 std::string dump()
const override
2164 static constexpr size_t DontIndent = std::string::npos;
2166 return dump(DontIndent);
2170 explicit operator std::string()
const