Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
json_value.h
1#ifndef SOURCEMETA_CORE_JSON_VALUE_H_
2#define SOURCEMETA_CORE_JSON_VALUE_H_
3
4#ifndef SOURCEMETA_CORE_JSON_EXPORT
5#include <sourcemeta/core/json_export.h>
6#endif
7
8#include <sourcemeta/core/json_array.h>
9#include <sourcemeta/core/json_error.h>
10#include <sourcemeta/core/json_hash.h>
11#include <sourcemeta/core/json_object.h>
12
13#include <sourcemeta/core/numeric.h>
14#include <sourcemeta/core/preprocessor.h>
15
16#include <algorithm> // std::any_of
17#include <bitset> // std::bitset
18#include <cassert> // assert
19#include <cmath> // std::modf, std::trunc, std::isinf, std::isnan
20#include <concepts> // std::same_as
21#include <cstddef> // std::size_t
22#include <cstdint> // std::int64_t, std::uint8_t
23#include <functional> // std::less, std::reference_wrapper, std::function
24#include <initializer_list> // std::initializer_list
25#include <limits> // std::numeric_limits
26#include <memory> // std::allocator
27#include <set> // std::set
28#include <span> // std::span
29#include <sstream> // std::basic_istringstream
30#include <string> // std::basic_string, std::char_traits
31#include <string_view> // std::basic_string_view
32#include <type_traits> // std::is_same_v, std::remove_cvref_t
33#include <utility> // std::pair
34
35namespace sourcemeta::core {
36
39class SOURCEMETA_CORE_JSON_EXPORT JSON {
40public:
42 using Char = char;
44 using CharTraits = std::char_traits<Char>;
46 using Integer = std::int64_t;
48 using Real = double;
50 template <typename T> using Allocator = std::allocator<T>;
52 using String = std::basic_string<Char, CharTraits, Allocator<Char>>;
54 using StringView = std::basic_string_view<Char, CharTraits>;
60 enum class ParsePhase : std::uint8_t {
62 Pre,
65 };
66
67 // The enumeration indexes must stay in sync with the internal variant
69 enum class Type : std::uint8_t {
71 Null = 0,
73 Boolean = 1,
77 Real = 3,
79 String = 4,
81 Array = 5,
83 Object = 6,
86 };
87
89 using TypeSet = std::bitset<8>;
90
92 enum class ParseContext : std::uint8_t {
94 Root,
96 Property,
99 };
100
103 using ParseCallback = std::function<void(
104 const ParsePhase phase, const Type type, const std::uint64_t line,
105 const std::uint64_t column, const ParseContext context,
106 const std::size_t index, const String &property)>;
107
110 using KeyComparison = std::function<bool(const String &, const String &)>;
111
112 /*
113 Constructors
114 */
115
124 explicit JSON(const std::int64_t value);
125
134 explicit JSON(const std::size_t value);
135
144 explicit JSON(const int value);
145
146 // On some systems, `std::int64_t` might be equal to `long`
148 template <typename T = std::int64_t>
149 explicit JSON(const long value)
150 requires(!std::is_same_v<T, std::int64_t>)
151 : current_type_{Type::Integer} {
152 this->data_integer = value;
153 }
154
163 explicit JSON(const double value);
164
173 explicit JSON(const float value);
174
182 explicit JSON(const bool value);
183
191 explicit JSON(const std::nullptr_t);
192
200 explicit JSON(const String &value);
201
210 explicit JSON(String &&value);
211
219 explicit JSON(const std::basic_string_view<Char, CharTraits> &value);
220
228 explicit JSON(const Char *const value);
229
231 explicit JSON(const Array &value);
232
247 explicit JSON(std::initializer_list<Object::pair_value_type> values);
248
250 explicit JSON(const Object &value);
251
253 explicit JSON(const Decimal &value);
254
256 explicit JSON(Decimal &&value);
257
259 JSON(const JSON &other);
261 JSON(JSON &&other) noexcept;
262 auto operator=(const JSON &other) -> JSON &;
263 auto operator=(JSON &&other) noexcept -> JSON &;
264
267
282 static auto make_array() -> JSON;
283
304 static auto make_array(std::initializer_list<JSON> values) -> JSON;
305
320 static auto make_object() -> JSON;
321
332 static auto size(const String &value) noexcept -> std::size_t;
333
334 /*
335 * Operators
336 */
337
338 auto operator<(const JSON &other) const -> bool;
339 auto operator<=(const JSON &other) const -> bool;
340 auto operator>(const JSON &other) const -> bool;
341 auto operator>=(const JSON &other) const -> bool;
342
355 auto operator==(const JSON &other) const -> bool;
356 auto operator!=(const JSON &) const -> bool = default;
357
371 auto operator+(const JSON &other) const -> JSON;
372
386 auto operator-(const JSON &other) const -> JSON;
387
402 auto operator+=(const JSON &additive) -> JSON &;
403
418 auto operator-=(const JSON &substractive) -> JSON &;
419
420 /*
421 * Type checking
422 */
423
433 [[nodiscard]] SOURCEMETA_FORCEINLINE auto is_boolean() const noexcept
434 -> bool {
435 return this->current_type_ == Type::Boolean;
436 }
437
447 [[nodiscard]] SOURCEMETA_FORCEINLINE auto is_null() const noexcept -> bool {
448 return this->current_type_ == Type::Null;
449 }
450
460 [[nodiscard]] SOURCEMETA_FORCEINLINE auto is_integer() const noexcept
461 -> bool {
462 return this->current_type_ == Type::Integer;
463 }
464
474 [[nodiscard]] SOURCEMETA_FORCEINLINE auto is_real() const noexcept -> bool {
475 return this->current_type_ == Type::Real;
476 }
477
488 [[nodiscard]] SOURCEMETA_FORCEINLINE auto is_integral() const noexcept
489 -> bool {
490 switch (this->type()) {
491 case Type::Integer:
492 return true;
493 case Type::Real: {
494 Real integral_part = 0.0;
495 return std::modf(this->to_real(), &integral_part) == 0.0;
496 }
497 case Type::Decimal:
498 return this->to_decimal().is_integral();
499 default:
500 return false;
501 }
502 }
503
516 [[nodiscard]] SOURCEMETA_FORCEINLINE auto is_number() const noexcept -> bool {
517 return this->is_integer() || this->is_real() || this->is_decimal();
518 }
519
540 [[nodiscard]] SOURCEMETA_FORCEINLINE auto is_double() const noexcept -> bool {
541 assert(this->is_number());
542 if (this->is_real()) {
543 return true;
544 }
545 if (this->is_integer()) {
547 }
548 return this->to_decimal().is_double();
549 }
550
569 [[nodiscard]] SOURCEMETA_FORCEINLINE auto is_float() const noexcept -> bool {
570 assert(this->is_number());
571 if (this->is_real()) {
572 return is_representable_as<float>(this->to_real());
573 }
574 if (this->is_integer()) {
576 }
577 return this->to_decimal().is_float();
578 }
579
592 [[nodiscard]] auto is_positive() const noexcept -> bool;
593
603 [[nodiscard]] SOURCEMETA_FORCEINLINE auto is_string() const noexcept -> bool {
604 return this->current_type_ == Type::String;
605 }
606
617 [[nodiscard]] SOURCEMETA_FORCEINLINE auto is_array() const noexcept -> bool {
618 return this->current_type_ == Type::Array;
619 }
620
631 [[nodiscard]] SOURCEMETA_FORCEINLINE auto is_object() const noexcept -> bool {
632 return this->current_type_ == Type::Object;
633 }
634
646 [[nodiscard]] SOURCEMETA_FORCEINLINE auto is_decimal() const noexcept
647 -> bool {
648 return this->current_type_ == Type::Decimal;
649 }
650
660 [[nodiscard]] SOURCEMETA_FORCEINLINE auto type() const noexcept -> Type {
661 return this->current_type_;
662 }
663
664 /*
665 * Type conversion
666 */
667
679 [[nodiscard]] SOURCEMETA_FORCEINLINE auto to_boolean() const noexcept
680 -> bool {
681 assert(this->is_boolean());
682 return this->data_boolean;
683 }
684
697 [[nodiscard]] SOURCEMETA_FORCEINLINE auto to_integer() const noexcept
698 -> Integer {
699 assert(this->is_integer());
700 return this->data_integer;
701 }
702
715 [[nodiscard]] SOURCEMETA_FORCEINLINE auto to_real() const noexcept -> Real {
716 assert(this->is_real());
717 assert(!std::isinf(this->data_real));
718 assert(!std::isnan(this->data_real));
719 return this->data_real;
720 }
721
734 [[nodiscard]] SOURCEMETA_FORCEINLINE auto to_decimal() const noexcept
735 -> const Decimal & {
736 assert(this->is_decimal());
737 assert(this->data_decimal.is_finite());
738 assert(!this->data_decimal.is_nan());
739 return this->data_decimal;
740 }
741
754 [[nodiscard]] SOURCEMETA_FORCEINLINE auto to_string() const noexcept
755 -> const String & {
756 assert(this->is_string());
757 return this->data_string;
758 }
759
773 [[nodiscard]] auto to_stringstream() const
774 -> std::basic_istringstream<Char, CharTraits, Allocator<Char>>;
775
794 // TODO: Merge const/non-const overloads of as_array, as_object, at, front,
795 // back using deducing this once Apple Clang supports it
796 // (__cpp_explicit_this_parameter)
797 [[nodiscard]] SOURCEMETA_FORCEINLINE auto as_array() const noexcept
798 -> const Array & {
799 assert(this->is_array());
800 return this->data_array;
801 }
802
815 [[nodiscard]] SOURCEMETA_FORCEINLINE auto as_array() noexcept -> Array & {
816 assert(this->is_array());
817 return this->data_array;
818 }
819
842 [[nodiscard]] SOURCEMETA_FORCEINLINE auto as_object() noexcept -> Object & {
843 assert(this->is_object());
844 return this->data_object;
845 }
846
865 [[nodiscard]] SOURCEMETA_FORCEINLINE auto as_object() const noexcept
866 -> const Object & {
867 assert(this->is_object());
868 return this->data_object;
869 }
870
884 [[nodiscard]] SOURCEMETA_FORCEINLINE auto as_real() const -> Real {
885 assert(this->is_number());
886 if (this->is_real()) {
887 return this->to_real();
888 }
889 if (this->is_integer()) {
890 return static_cast<Real>(this->to_integer());
891 }
892 return this->to_decimal().to_double();
893 }
894
907 [[nodiscard]] SOURCEMETA_FORCEINLINE auto as_integer() const -> Integer {
908 assert(this->is_number());
909 if (this->is_integer()) {
910 return this->to_integer();
911 }
912 if (this->is_real()) {
913 const auto truncated{std::trunc(this->to_real())};
914 if (truncated < static_cast<Real>(std::numeric_limits<Integer>::min()) ||
915 truncated >= static_cast<Real>(std::numeric_limits<Integer>::max())) {
917 "The real number does not fit in a 64-bit integer"};
918 }
919
920 return static_cast<Integer>(truncated);
921 }
922 const auto integral{this->to_decimal().to_integral()};
923 if (!integral.is_int64()) {
925 "The decimal number does not fit in a 64-bit integer"};
926 }
927
928 return integral.to_int64();
929 }
930
931 /*
932 * Getters
933 */
934
953 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
954 at(const Array::size_type index) const -> const JSON & {
955 assert(this->is_array());
956 assert(index < this->size());
957 return this->data_array.data_[index];
958 }
959
978 [[nodiscard]] SOURCEMETA_FORCEINLINE auto at(const Array::size_type index)
979 -> JSON & {
980 assert(this->is_array());
981 assert(index < this->size());
982 return this->data_array.data_[index];
983 }
984
997 [[nodiscard]] SOURCEMETA_FORCEINLINE auto at(const String &key) const
998 -> const JSON & {
999 assert(this->is_object());
1000 assert(this->defines(key));
1001 const auto &object{this->data_object};
1002 return object.at(key, object.hash(key));
1003 }
1004
1006 template <typename T>
1007 requires std::same_as<std::remove_cvref_t<T>, StringView>
1008 [[nodiscard]] SOURCEMETA_FORCEINLINE auto at(T key) const -> const JSON & {
1009 assert(this->is_object());
1010 assert(this->defines(key));
1011 const auto &object{this->data_object};
1012 return object.at(key, object.hash(key));
1013 }
1014
1029 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
1030 at(const String &key, const Object::hash_type hash) const -> const JSON & {
1031 assert(this->is_object());
1032 assert(this->defines(key));
1033 return this->data_object.at(key, hash);
1034 }
1035
1038 template <typename T>
1039 requires std::same_as<std::remove_cvref_t<T>, StringView>
1040 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
1041 at(T key, const Object::hash_type hash) const -> const JSON & {
1042 assert(this->is_object());
1043 assert(this->defines(key));
1044 return this->data_object.at(key, hash);
1045 }
1046
1059 [[nodiscard]] SOURCEMETA_FORCEINLINE auto at(const String &key) -> JSON & {
1060 assert(this->is_object());
1061 assert(this->defines(key));
1062 auto &object{this->data_object};
1063 return object.at(key, object.hash(key));
1064 }
1065
1067 template <typename T>
1068 requires std::same_as<std::remove_cvref_t<T>, StringView>
1069 [[nodiscard]] SOURCEMETA_FORCEINLINE auto at(T key) -> JSON & {
1070 assert(this->is_object());
1071 assert(this->defines(key));
1072 auto &object{this->data_object};
1073 return object.at(key, object.hash(key));
1074 }
1075
1090 [[nodiscard]] SOURCEMETA_FORCEINLINE auto at(const String &key,
1091 const Object::hash_type hash)
1092 -> JSON & {
1093 assert(this->is_object());
1094 assert(this->defines(key));
1095 return this->data_object.at(key, hash);
1096 }
1097
1100 template <typename T>
1101 requires std::same_as<std::remove_cvref_t<T>, StringView>
1102 [[nodiscard]] SOURCEMETA_FORCEINLINE auto at(T key,
1103 const Object::hash_type hash)
1104 -> JSON & {
1105 assert(this->is_object());
1106 assert(this->defines(key));
1107 return this->data_object.at(key, hash);
1108 }
1109
1124 [[nodiscard]] auto at_or(const String &key, const JSON &otherwise) const
1125 -> const JSON &;
1126
1129 [[nodiscard]] auto at_or(const String &key, JSON &&otherwise) const
1130 -> const JSON & = delete;
1131
1148 [[nodiscard]] auto at_or(const String &key, const Object::hash_type &hash,
1149 const JSON &otherwise) const -> const JSON &;
1150
1153 [[nodiscard]] auto at_or(const String &key, const Object::hash_type &hash,
1154 JSON &&otherwise) const -> const JSON & = delete;
1155
1168 [[nodiscard]] SOURCEMETA_FORCEINLINE auto front() -> JSON & {
1169 assert(this->is_array());
1170 assert(!this->empty());
1171 return this->data_array.data_.front();
1172 }
1173
1186 [[nodiscard]] SOURCEMETA_FORCEINLINE auto front() const -> const JSON & {
1187 assert(this->is_array());
1188 assert(!this->empty());
1189 return this->data_array.data_.front();
1190 }
1191
1204 [[nodiscard]] SOURCEMETA_FORCEINLINE auto back() -> JSON & {
1205 assert(this->is_array());
1206 assert(!this->empty());
1207 return this->data_array.data_.back();
1208 }
1209
1222 [[nodiscard]] SOURCEMETA_FORCEINLINE auto back() const -> const JSON & {
1223 assert(this->is_array());
1224 assert(!this->empty());
1225 return this->data_array.data_.back();
1226 }
1227
1228 /*
1229 * Read operations
1230 */
1231
1252 [[nodiscard]] SOURCEMETA_FORCEINLINE auto size() const -> std::size_t {
1253 if (this->is_object()) {
1254 return this->object_size();
1255 }
1256 if (this->is_array()) {
1257 return this->array_size();
1258 }
1259 return this->string_size();
1260 }
1261
1273 [[nodiscard]] SOURCEMETA_FORCEINLINE auto string_size() const -> std::size_t {
1274 assert(this->is_string());
1275 return JSON::size(this->data_string);
1276 }
1277
1290 [[nodiscard]] SOURCEMETA_FORCEINLINE auto array_size() const -> std::size_t {
1291 assert(this->is_array());
1292 return this->data_array.data_.size();
1293 }
1294
1307 [[nodiscard]] SOURCEMETA_FORCEINLINE auto object_size() const -> std::size_t {
1308 assert(this->is_object());
1309 return this->data_object.size();
1310 }
1311
1324 [[nodiscard]] SOURCEMETA_FORCEINLINE auto byte_size() const -> std::size_t {
1325 assert(this->is_string());
1326 return this->data_string.size();
1327 }
1328
1343 [[nodiscard]] auto estimated_byte_size() const -> std::uint64_t;
1344
1360 [[nodiscard]] auto fast_hash() const -> std::uint64_t;
1361
1374 [[nodiscard]] auto divisible_by(const JSON &divisor) const -> bool;
1375
1395 [[nodiscard]] SOURCEMETA_FORCEINLINE auto empty() const -> bool {
1396 if (this->is_object()) {
1397 return this->data_object.empty();
1398 }
1399 if (this->is_array()) {
1400 return this->data_array.data_.empty();
1401 }
1402 return this->data_string.empty();
1403 }
1404
1419 [[nodiscard]] SOURCEMETA_FORCEINLINE auto try_at(const String &key) const
1420 -> const JSON * {
1421 assert(this->is_object());
1422 const auto &object{this->data_object};
1423 return object.try_at(key, object.hash(key));
1424 }
1425
1427 template <typename T>
1428 requires std::same_as<std::remove_cvref_t<T>, StringView>
1429 [[nodiscard]] SOURCEMETA_FORCEINLINE auto try_at(T key) const
1430 -> const JSON * {
1431 assert(this->is_object());
1432 const auto &object{this->data_object};
1433 return object.try_at(key, object.hash(key));
1434 }
1435
1452 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
1453 try_at(const String &key, const Object::hash_type hash) const
1454 -> const JSON * {
1455 assert(this->is_object());
1456 const auto &object{this->data_object};
1457 return object.try_at(key, hash);
1458 }
1459
1462 template <typename T>
1463 requires std::same_as<std::remove_cvref_t<T>, StringView>
1464 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
1465 try_at(T key, const Object::hash_type hash) const -> const JSON * {
1466 assert(this->is_object());
1467 const auto &object{this->data_object};
1468 return object.try_at(key, hash);
1469 }
1470
1485 [[nodiscard]] SOURCEMETA_FORCEINLINE auto try_at(const String &key)
1486 -> JSON * {
1487 assert(this->is_object());
1488 auto &object{this->data_object};
1489 return object.try_at(key, object.hash(key));
1490 }
1491
1493 template <typename T>
1494 requires std::same_as<std::remove_cvref_t<T>, StringView>
1495 [[nodiscard]] SOURCEMETA_FORCEINLINE auto try_at(T key) -> JSON * {
1496 assert(this->is_object());
1497 auto &object{this->data_object};
1498 return object.try_at(key, object.hash(key));
1499 }
1500
1516 [[nodiscard]] SOURCEMETA_FORCEINLINE auto try_at(const String &key,
1517 const Object::hash_type hash)
1518 -> JSON * {
1519 assert(this->is_object());
1520 return this->data_object.try_at(key, hash);
1521 }
1522
1525 template <typename T>
1526 requires std::same_as<std::remove_cvref_t<T>, StringView>
1527 [[nodiscard]] SOURCEMETA_FORCEINLINE auto try_at(T key,
1528 const Object::hash_type hash)
1529 -> JSON * {
1530 assert(this->is_object());
1531 return this->data_object.try_at(key, hash);
1532 }
1533
1558 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
1559 try_at(const String &key, const Object::hash_type hash,
1560 Object::size_type &start) const -> const JSON * {
1561 assert(this->is_object());
1562 const auto &object{this->data_object};
1563 return object.try_at(key, hash, start);
1564 }
1565
1568 template <typename T>
1569 requires std::same_as<std::remove_cvref_t<T>, StringView>
1570 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
1571 try_at(T key, const Object::hash_type hash, Object::size_type &start) const
1572 -> const JSON * {
1573 assert(this->is_object());
1574 const auto &object{this->data_object};
1575 return object.try_at(key, hash, start);
1576 }
1577
1590 [[nodiscard]] SOURCEMETA_FORCEINLINE auto defines(const String &key) const
1591 -> bool {
1592 assert(this->is_object());
1593 const auto &object{this->data_object};
1594 return object.defines(key, object.hash(key));
1595 }
1596
1599 template <typename T>
1600 requires std::same_as<std::remove_cvref_t<T>, StringView>
1601 [[nodiscard]] SOURCEMETA_FORCEINLINE auto defines(T key) const -> bool {
1602 assert(this->is_object());
1603 const auto &object{this->data_object};
1604 return object.defines(key, object.hash(key));
1605 }
1606
1621 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
1622 defines(const String &key, const Object::hash_type hash) const -> bool {
1623 assert(this->is_object());
1624 return this->data_object.defines(key, hash);
1625 }
1626
1629 template <typename T>
1630 requires std::same_as<std::remove_cvref_t<T>, StringView>
1631 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
1632 defines(T key, const Object::hash_type hash) const -> bool {
1633 assert(this->is_object());
1634 return this->data_object.defines(key, hash);
1635 }
1636
1649 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
1650 defines(const Array::size_type index) const -> bool {
1651 return this->defines(std::to_string(index));
1652 }
1653
1669 template <typename Iterator>
1670 [[nodiscard]] auto defines_any(Iterator begin, Iterator end) const -> bool {
1671 assert(this->is_object());
1672 return std::any_of(begin, end, [this](const auto &key) -> auto {
1673 return this->defines(key);
1674 });
1675 }
1676
1689 [[nodiscard]] auto defines_any(std::initializer_list<String> keys) const
1690 -> bool;
1691
1704 [[nodiscard]] auto contains(const JSON &element) const -> bool;
1705
1718 [[nodiscard]] auto contains(const StringView element) const -> bool;
1719
1733 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
1734 array_member_contains(const StringView key, const Object::hash_type hash,
1735 const StringView value) const -> bool {
1736 assert(this->is_object());
1737 const auto *member{this->try_at(key, hash)};
1738 return member != nullptr && member->is_array() && member->contains(value);
1739 }
1740
1752 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
1753 array_member_contains(const StringView key, const StringView value) const
1754 -> bool {
1755 return this->array_member_contains(key, Object::hash(key), value);
1756 }
1757
1769 [[nodiscard]] auto is_array_of_strings() const -> bool {
1770 if (!this->is_array()) {
1771 return false;
1772 }
1773
1774 for (const auto &element : this->as_array()) {
1775 if (!element.is_string()) {
1776 return false;
1777 }
1778 }
1779
1780 return true;
1781 }
1782
1794 [[nodiscard]] auto includes(const String &input) const -> bool;
1795
1807 [[nodiscard]] auto includes(const String::value_type input) const -> bool;
1808
1820 [[nodiscard]] auto unique() const -> bool;
1821
1833 [[nodiscard]] auto unique_keys() const -> bool;
1834
1835 /*
1836 * Write operations
1837 */
1838
1853 auto push_back(const JSON &value) -> void;
1854
1868 auto push_back(JSON &&value) -> void;
1869
1886 auto push_back_if_unique(const JSON &value)
1887 -> std::pair<std::reference_wrapper<const JSON>, bool>;
1888
1907 -> std::pair<std::reference_wrapper<const JSON>, bool>;
1908
1923 auto assign(const String &key, const JSON &value) -> void;
1924
1927 template <typename T>
1928 requires std::same_as<std::remove_cvref_t<T>, StringView>
1929 auto assign(T key, const JSON &value) -> void {
1930 assert(this->is_object());
1931 this->data_object.emplace(String{key}, value);
1932 }
1933
1947 auto assign(const String &key, JSON &&value) -> void;
1948
1951 template <typename T>
1952 requires std::same_as<std::remove_cvref_t<T>, StringView>
1953 auto assign(T key, JSON &&value) -> void {
1954 assert(this->is_object());
1955 this->data_object.emplace(String{key}, std::move(value));
1956 }
1957
1971 auto try_assign_before(const String &key, const JSON &value,
1972 const String &other) -> void;
1973
1994 auto assign_if_missing(const String &key, const JSON &value) -> void;
1995
1998 template <typename T>
1999 requires std::same_as<std::remove_cvref_t<T>, StringView>
2000 auto assign_if_missing(T key, const JSON &value) -> void {
2001 assert(this->is_object());
2002 if (!this->defines(key)) {
2003 this->assign(key, value);
2004 }
2005 }
2006
2024 auto assign_if_missing(const String &key, JSON &&value) -> void;
2025
2028 template <typename T>
2029 requires std::same_as<std::remove_cvref_t<T>, StringView>
2030 auto assign_if_missing(T key, JSON &&value) -> void {
2031 assert(this->is_object());
2032 if (!this->defines(key)) {
2033 this->assign(key, std::move(value));
2034 }
2035 }
2036
2051 auto assign_assume_new(const String &key, JSON &&value) -> void;
2052
2068 auto assign_assume_new(String &&key, JSON &&value) -> void;
2069
2072 auto assign_assume_new(String &&key, JSON &&value,
2073 const Object::hash_type &hash) -> JSON &;
2074
2088 SOURCEMETA_FORCEINLINE auto assign_if_nonempty(const StringView key,
2089 const Object::hash_type hash,
2090 const StringView value)
2091 -> void {
2092 if (!value.empty()) {
2093 this->assign_assume_new(String{key}, JSON{value}, hash);
2094 }
2095 }
2096
2109 SOURCEMETA_FORCEINLINE auto assign_if_nonempty(const StringView key,
2110 const StringView value)
2111 -> void {
2112 this->assign_if_nonempty(key, Object::hash(key), value);
2113 }
2114
2130 auto assign_if_nonempty(const StringView key, const Object::hash_type hash,
2131 const std::span<const StringView> values) -> void {
2132 if (values.empty()) {
2133 return;
2134 }
2135
2136 auto array{JSON::make_array()};
2137 for (const auto value : values) {
2138 array.push_back(JSON{value});
2139 }
2140
2141 this->assign_assume_new(String{key}, std::move(array), hash);
2142 }
2143
2159 const std::span<const StringView> values) -> void {
2160 this->assign_if_nonempty(key, Object::hash(key), values);
2161 }
2162
2174 auto erase(const String &key) -> Object::size_type;
2175
2177 template <typename T>
2178 requires std::same_as<std::remove_cvref_t<T>, StringView>
2179 auto erase(T key) -> Object::size_type {
2180 assert(this->is_object());
2181 return this->data_object.erase(key);
2182 }
2183
2196 SOURCEMETA_FORCEINLINE auto erase(const String &key,
2197 const Object::hash_type hash)
2198 -> Object::size_type {
2199 assert(this->is_object());
2200 return this->data_object.erase(key, hash);
2201 }
2202
2205 template <typename T>
2206 requires std::same_as<std::remove_cvref_t<T>, StringView>
2207 SOURCEMETA_FORCEINLINE auto erase(T key, const Object::hash_type hash)
2208 -> Object::size_type {
2209 assert(this->is_object());
2210 return this->data_object.erase(key, hash);
2211 }
2212
2234 template <typename Iterator>
2235 auto erase_keys(Iterator first, Iterator last) -> void {
2236 assert(this->is_object());
2237 for (auto iterator = first; iterator != last; ++iterator) {
2238 this->data_object.erase(*iterator);
2239 }
2240 }
2241
2260 auto erase_keys(std::initializer_list<String> keys) -> void;
2261
2276 auto erase(Array::const_iterator position) -> Array::iterator;
2277
2291 auto erase(Array::const_iterator first, Array::const_iterator last)
2292 -> Array::iterator;
2293
2309 auto erase_if(const std::function<bool(const JSON &)> &predicate) -> void;
2310
2327 auto clear() -> void;
2328
2351 template <typename Iterator>
2352 auto clear_except(Iterator first, Iterator last) -> void {
2353 assert(this->is_object());
2354 std::set<String, std::less<>, Allocator<String>> whitelist;
2355 for (auto iterator = first; iterator != last; ++iterator) {
2356 whitelist.insert(*iterator);
2357 }
2358
2359 std::set<String, std::less<>, Allocator<String>> blacklist;
2360 for (const auto &pair : this->as_object()) {
2361 if (!whitelist.contains(pair.first)) {
2362 blacklist.insert(pair.first);
2363 }
2364 }
2365
2366 this->erase_keys(blacklist.cbegin(), blacklist.cend());
2367 }
2368
2388 auto clear_except(std::initializer_list<String> keys) -> void;
2389
2416 auto merge(const JSON::Object &other) -> void;
2417
2427 [[nodiscard]] auto trim() const -> JSON::String;
2428
2439 auto trim() -> const JSON::String &;
2440
2453 [[nodiscard]] auto is_trimmed() const noexcept -> bool;
2454
2479 auto reorder(const KeyComparison &compare) -> void;
2480
2481 /*
2482 * Transform operations
2483 */
2484
2502 auto rename(const JSON::String &key, JSON::String &&target) -> void;
2503
2518 auto into(const JSON &other) -> void;
2519
2533 auto into(JSON &&other) noexcept -> void;
2534
2548 auto into_array() -> void;
2549
2563 auto into_object() -> void;
2564
2565private:
2566 Type current_type_ = Type::Null;
2567
2568// Exporting symbols that depends on the standard C++ library is considered
2569// safe.
2570// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
2571#if defined(_MSC_VER)
2572#pragma warning(push)
2573#pragma warning(disable : 4251)
2574#endif
2575 union {
2576 bool data_boolean;
2577 Integer data_integer;
2578 Real data_real;
2579 String data_string;
2580 Array data_array;
2581 Object data_object;
2582 Decimal data_decimal;
2583 };
2584
2585 // Storing the decimal inline must not grow the union beyond its existing
2586 // footprint
2587 static_assert(sizeof(Decimal) <= sizeof(String));
2588#if defined(_MSC_VER)
2589#pragma warning(pop)
2590#endif
2591 auto maybe_destruct_union() -> void;
2592};
2593
2594} // namespace sourcemeta::core
2595
2596#endif
static constexpr auto hash(const String &key) noexcept -> hash_type
Definition json_object.h:190
SOURCEMETA_FORCEINLINE auto is_number() const noexcept -> bool
Definition json_value.h:516
JSON(const JSON &other)
Misc constructors.
SOURCEMETA_FORCEINLINE auto type() const noexcept -> Type
Definition json_value.h:660
SOURCEMETA_FORCEINLINE auto try_at(const String &key) -> JSON *
Definition json_value.h:1485
SOURCEMETA_FORCEINLINE auto defines(const Array::size_type index) const -> bool
Definition json_value.h:1650
JSON(const Array &value)
A copy constructor for the array type.
JSON(const std::nullptr_t)
double Real
The real type used by the JSON document.
Definition json_value.h:48
char Char
The character type used by the JSON document.
Definition json_value.h:42
SOURCEMETA_FORCEINLINE auto try_at(const String &key, const Object::hash_type hash) const -> const JSON *
Definition json_value.h:1453
auto assign_assume_new(const String &key, JSON &&value) -> void
std::function< void( const ParsePhase phase, const Type type, const std::uint64_t line, const std::uint64_t column, const ParseContext context, const std::size_t index, const String &property)> ParseCallback
Definition json_value.h:103
auto defines_any(std::initializer_list< String > keys) const -> bool
std::int64_t Integer
The integer type used by the JSON document.
Definition json_value.h:46
SOURCEMETA_FORCEINLINE auto at(const String &key, const Object::hash_type hash) const -> const JSON &
Definition json_value.h:1030
SOURCEMETA_FORCEINLINE auto to_decimal() const noexcept -> const Decimal &
Definition json_value.h:734
SOURCEMETA_FORCEINLINE auto at(const Array::size_type index) -> JSON &
Definition json_value.h:978
SOURCEMETA_FORCEINLINE auto as_array() const noexcept -> const Array &
Definition json_value.h:797
auto assign_if_missing(T key, JSON &&value) -> void
Definition json_value.h:2030
SOURCEMETA_FORCEINLINE auto to_boolean() const noexcept -> bool
Definition json_value.h:679
SOURCEMETA_FORCEINLINE auto as_object() noexcept -> Object &
Definition json_value.h:842
SOURCEMETA_FORCEINLINE auto at(T key) const -> const JSON &
This method retrieves an object element by string view key.
Definition json_value.h:1008
SOURCEMETA_FORCEINLINE auto as_object() const noexcept -> const Object &
Definition json_value.h:865
std::char_traits< Char > CharTraits
The character traits used by the JSON document.
Definition json_value.h:44
auto unique_keys() const -> bool
static auto make_array() -> JSON
auto assign_assume_new(String &&key, JSON &&value, const Object::hash_type &hash) -> JSON &
SOURCEMETA_FORCEINLINE auto size() const -> std::size_t
Definition json_value.h:1252
SOURCEMETA_FORCEINLINE auto defines(T key) const -> bool
Definition json_value.h:1601
auto erase_keys(Iterator first, Iterator last) -> void
Definition json_value.h:2235
SOURCEMETA_FORCEINLINE auto try_at(T key) const -> const JSON *
This method tries to retrieve an object element by string view key.
Definition json_value.h:1429
auto reorder(const KeyComparison &compare) -> void
auto into_array() -> void
auto into(const JSON &other) -> void
std::basic_string< Char, CharTraits, Allocator< Char > > String
The string type used by the JSON document.
Definition json_value.h:52
auto contains(const StringView element) const -> bool
std::bitset< 8 > TypeSet
A set of types.
Definition json_value.h:89
auto is_positive() const noexcept -> bool
SOURCEMETA_FORCEINLINE auto at(const String &key, const Object::hash_type hash) -> JSON &
Definition json_value.h:1090
SOURCEMETA_FORCEINLINE auto to_real() const noexcept -> Real
Definition json_value.h:715
SOURCEMETA_FORCEINLINE auto as_real() const -> Real
Definition json_value.h:884
std::allocator< T > Allocator
The allocator used by the JSON document.
Definition json_value.h:50
SOURCEMETA_FORCEINLINE auto defines(T key, const Object::hash_type hash) const -> bool
Definition json_value.h:1632
JSON(std::initializer_list< Object::pair_value_type > values)
SOURCEMETA_FORCEINLINE auto try_at(T key, const Object::hash_type hash, Object::size_type &start) const -> const JSON *
Definition json_value.h:1571
SOURCEMETA_FORCEINLINE auto front() -> JSON &
Definition json_value.h:1168
auto contains(const JSON &element) const -> bool
SOURCEMETA_FORCEINLINE auto is_float() const noexcept -> bool
Definition json_value.h:569
SOURCEMETA_FORCEINLINE auto array_size() const -> std::size_t
Definition json_value.h:1290
SOURCEMETA_FORCEINLINE auto at(const String &key) const -> const JSON &
Definition json_value.h:997
SOURCEMETA_FORCEINLINE auto byte_size() const -> std::size_t
Definition json_value.h:1324
std::function< bool(const String &, const String &)> KeyComparison
Definition json_value.h:110
SOURCEMETA_FORCEINLINE auto is_decimal() const noexcept -> bool
Definition json_value.h:646
SOURCEMETA_FORCEINLINE auto assign_if_nonempty(const StringView key, const StringView value) -> void
Definition json_value.h:2109
SOURCEMETA_FORCEINLINE auto is_integral() const noexcept -> bool
Definition json_value.h:488
auto try_assign_before(const String &key, const JSON &value, const String &other) -> void
auto assign_if_nonempty(const StringView key, const std::span< const StringView > values) -> void
Definition json_value.h:2158
SOURCEMETA_FORCEINLINE auto as_array() noexcept -> Array &
Definition json_value.h:815
SOURCEMETA_FORCEINLINE auto is_integer() const noexcept -> bool
Definition json_value.h:460
auto push_back(const JSON &value) -> void
auto assign_assume_new(String &&key, JSON &&value) -> void
auto at_or(const String &key, const JSON &otherwise) const -> const JSON &
static auto make_array(std::initializer_list< JSON > values) -> JSON
auto clear_except(Iterator first, Iterator last) -> void
Definition json_value.h:2352
JSON(Decimal &&value)
A move constructor for the decimal type.
SOURCEMETA_FORCEINLINE auto try_at(const String &key, const Object::hash_type hash) -> JSON *
Definition json_value.h:1516
Type
The different types of a JSON instance.
Definition json_value.h:69
@ String
The JSON string type.
Definition json_value.h:79
@ Boolean
The JSON boolean type.
Definition json_value.h:73
@ Array
The JSON array type.
Definition json_value.h:81
@ Object
The JSON object type.
Definition json_value.h:83
@ Real
The JSON real number type.
Definition json_value.h:77
@ Decimal
The JSON decimal type.
Definition json_value.h:85
@ Integer
The JSON integer type.
Definition json_value.h:75
@ Null
The JSON null type.
Definition json_value.h:71
auto push_back_if_unique(const JSON &value) -> std::pair< std::reference_wrapper< const JSON >, bool >
SOURCEMETA_FORCEINLINE auto erase(T key, const Object::hash_type hash) -> Object::size_type
Definition json_value.h:2207
auto is_trimmed() const noexcept -> bool
auto estimated_byte_size() const -> std::uint64_t
JSON(const Char *const value)
SOURCEMETA_FORCEINLINE auto is_object() const noexcept -> bool
Definition json_value.h:631
auto assign(const String &key, JSON &&value) -> void
auto assign_if_nonempty(const StringView key, const Object::hash_type hash, const std::span< const StringView > values) -> void
Definition json_value.h:2130
SOURCEMETA_FORCEINLINE auto front() const -> const JSON &
Definition json_value.h:1186
JSON(const long value)
This constructor creates a JSON document from an integer type.
Definition json_value.h:149
SOURCEMETA_FORCEINLINE auto as_integer() const -> Integer
Definition json_value.h:907
JSON(const std::size_t value)
SOURCEMETA_FORCEINLINE auto erase(const String &key, const Object::hash_type hash) -> Object::size_type
Definition json_value.h:2196
SOURCEMETA_FORCEINLINE auto defines(const String &key, const Object::hash_type hash) const -> bool
Definition json_value.h:1622
auto fast_hash() const -> std::uint64_t
auto merge(const JSON::Object &other) -> void
SOURCEMETA_FORCEINLINE auto at(const String &key) -> JSON &
Definition json_value.h:1059
JSONArray< JSON > Array
The array type used by the JSON document.
Definition json_value.h:56
auto rename(const JSON::String &key, JSON::String &&target) -> void
auto assign(T key, JSON &&value) -> void
Definition json_value.h:1953
JSON(const bool value)
SOURCEMETA_FORCEINLINE auto try_at(T key) -> JSON *
This method tries to retrieve a mutable object element by string view key.
Definition json_value.h:1495
SOURCEMETA_FORCEINLINE auto at(const Array::size_type index) const -> const JSON &
Definition json_value.h:954
SOURCEMETA_FORCEINLINE auto is_real() const noexcept -> bool
Definition json_value.h:474
SOURCEMETA_FORCEINLINE auto at(T key) -> JSON &
This method retrieves an object element by string view key.
Definition json_value.h:1069
SOURCEMETA_FORCEINLINE auto back() const -> const JSON &
Definition json_value.h:1222
auto clear_except(std::initializer_list< String > keys) -> void
JSONObject< String, JSON, PropertyHashJSON< JSON::String > > Object
The object type used by the JSON document.
Definition json_value.h:58
auto includes(const String &input) const -> bool
auto unique() const -> bool
static auto make_object() -> JSON
auto at_or(const String &key, const Object::hash_type &hash, const JSON &otherwise) const -> const JSON &
SOURCEMETA_FORCEINLINE auto try_at(const String &key) const -> const JSON *
Definition json_value.h:1419
SOURCEMETA_FORCEINLINE auto defines(const String &key) const -> bool
Definition json_value.h:1590
auto erase(const String &key) -> Object::size_type
JSON(const String &value)
SOURCEMETA_FORCEINLINE auto array_member_contains(const StringView key, const StringView value) const -> bool
Definition json_value.h:1753
auto defines_any(Iterator begin, Iterator end) const -> bool
Definition json_value.h:1670
ParsePhase
The parsing phase of a JSON document.
Definition json_value.h:60
auto is_array_of_strings() const -> bool
Definition json_value.h:1769
auto clear() -> void
SOURCEMETA_FORCEINLINE auto is_boolean() const noexcept -> bool
Definition json_value.h:433
SOURCEMETA_FORCEINLINE auto to_integer() const noexcept -> Integer
Definition json_value.h:697
JSON(String &&value)
SOURCEMETA_FORCEINLINE auto empty() const -> bool
Definition json_value.h:1395
std::basic_string_view< Char, CharTraits > StringView
The string view type used by the JSON document.
Definition json_value.h:54
auto to_stringstream() const -> std::basic_istringstream< Char, CharTraits, Allocator< Char > >
auto into_object() -> void
auto erase_if(const std::function< bool(const JSON &)> &predicate) -> void
JSON(const Object &value)
A copy constructor for the object type.
auto assign_if_missing(T key, const JSON &value) -> void
Definition json_value.h:2000
auto at_or(const String &key, const Object::hash_type &hash, JSON &&otherwise) const -> const JSON &=delete
auto assign(const String &key, const JSON &value) -> void
SOURCEMETA_FORCEINLINE auto is_string() const noexcept -> bool
Definition json_value.h:603
auto includes(const String::value_type input) const -> bool
SOURCEMETA_FORCEINLINE auto try_at(T key, const Object::hash_type hash) const -> const JSON *
Definition json_value.h:1465
auto trim() const -> JSON::String
SOURCEMETA_FORCEINLINE auto object_size() const -> std::size_t
Definition json_value.h:1307
JSON(const double value)
auto erase_keys(std::initializer_list< String > keys) -> void
SOURCEMETA_FORCEINLINE auto at(T key, const Object::hash_type hash) -> JSON &
Definition json_value.h:1102
SOURCEMETA_FORCEINLINE auto assign_if_nonempty(const StringView key, const Object::hash_type hash, const StringView value) -> void
Definition json_value.h:2088
SOURCEMETA_FORCEINLINE auto array_member_contains(const StringView key, const Object::hash_type hash, const StringView value) const -> bool
Definition json_value.h:1734
JSON(const std::int64_t value)
SOURCEMETA_FORCEINLINE auto try_at(T key, const Object::hash_type hash) -> JSON *
Definition json_value.h:1527
auto at_or(const String &key, JSON &&otherwise) const -> const JSON &=delete
SOURCEMETA_FORCEINLINE auto is_array() const noexcept -> bool
Definition json_value.h:617
JSON(const std::basic_string_view< Char, CharTraits > &value)
SOURCEMETA_FORCEINLINE auto string_size() const -> std::size_t
Definition json_value.h:1273
JSON(const int value)
auto erase(Array::const_iterator position) -> Array::iterator
ParseContext
The context type for parse callbacks.
Definition json_value.h:92
JSON(const Decimal &value)
A copy constructor for the decimal type.
auto erase(T key) -> Object::size_type
This method deletes an object key by string view.
Definition json_value.h:2179
JSON(const float value)
auto erase(Array::const_iterator first, Array::const_iterator last) -> Array::iterator
SOURCEMETA_FORCEINLINE auto is_null() const noexcept -> bool
Definition json_value.h:447
SOURCEMETA_FORCEINLINE auto at(T key, const Object::hash_type hash) const -> const JSON &
Definition json_value.h:1041
static auto size(const String &value) noexcept -> std::size_t
auto assign_if_missing(const String &key, JSON &&value) -> void
SOURCEMETA_FORCEINLINE auto is_double() const noexcept -> bool
Definition json_value.h:540
auto divisible_by(const JSON &divisor) const -> bool
auto assign_if_missing(const String &key, const JSON &value) -> void
SOURCEMETA_FORCEINLINE auto back() -> JSON &
Definition json_value.h:1204
JSON(JSON &&other) noexcept
A move constructor.
SOURCEMETA_FORCEINLINE auto to_string() const noexcept -> const String &
Definition json_value.h:754
SOURCEMETA_FORCEINLINE auto try_at(const String &key, const Object::hash_type hash, Object::size_type &start) const -> const JSON *
Definition json_value.h:1559
Definition json_array.h:11
Definition json_error.h:82
Definition json_object.h:19
@ Index
A map of index labels carrying no RDF ranging over an object.
Definition jsonld_materialize.h:116
Definition numeric_decimal.h:21
constexpr auto is_representable_as(const Integer value) -> bool
Definition numeric_util.h:419
@ Post
A client_secret in the request body (RFC 6749 Section 2.3.1).
Definition oauth_client_authentication.h:85