simdjson  3.11.0
Ridiculously Fast JSON
deserialize.h
1 #if SIMDJSON_SUPPORTS_DESERIALIZATION
2 
3 #ifndef SIMDJSON_ONDEMAND_DESERIALIZE_H
4 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
5 #define SIMDJSON_ONDEMAND_DESERIALIZE_H
6 #include "simdjson/generic/ondemand/base.h"
7 #include "simdjson/generic/ondemand/array.h"
8 #endif // SIMDJSON_CONDITIONAL_INCLUDE
9 
10 #include <concepts>
11 namespace simdjson {
12 
13 namespace tag_invoke_fn_ns {
14 void tag_invoke();
15 
16 struct tag_invoke_fn {
17  template <typename Tag, typename... Args>
18  requires requires(Tag tag, Args &&...args) {
19  tag_invoke(std::forward<Tag>(tag), std::forward<Args>(args)...);
20  }
21  constexpr auto operator()(Tag tag, Args &&...args) const
22  noexcept(noexcept(tag_invoke(std::forward<Tag>(tag),
23  std::forward<Args>(args)...)))
24  -> decltype(tag_invoke(std::forward<Tag>(tag),
25  std::forward<Args>(args)...)) {
26  return tag_invoke(std::forward<Tag>(tag), std::forward<Args>(args)...);
27  }
28 };
29 } // namespace tag_invoke_fn_ns
30 
31 inline namespace tag_invoke_ns {
32 inline constexpr tag_invoke_fn_ns::tag_invoke_fn tag_invoke = {};
33 } // namespace tag_invoke_ns
34 
35 template <typename Tag, typename... Args>
36 concept tag_invocable = requires(Tag tag, Args... args) {
37  tag_invoke(std::forward<Tag>(tag), std::forward<Args>(args)...);
38 };
39 
40 template <typename Tag, typename... Args>
41 concept nothrow_tag_invocable =
42  tag_invocable<Tag, Args...> && requires(Tag tag, Args... args) {
43  {
44  tag_invoke(std::forward<Tag>(tag), std::forward<Args>(args)...)
45  } noexcept;
46  };
47 
48 template <typename Tag, typename... Args>
49 using tag_invoke_result =
50  std::invoke_result<decltype(tag_invoke), Tag, Args...>;
51 
52 template <typename Tag, typename... Args>
53 using tag_invoke_result_t =
54  std::invoke_result_t<decltype(tag_invoke), Tag, Args...>;
55 
56 template <auto &Tag> using tag_t = std::decay_t<decltype(Tag)>;
57 
58 
59 struct deserialize_tag;
60 
62 template <typename> struct is_builtin_deserializable : std::false_type {};
63 template <> struct is_builtin_deserializable<int64_t> : std::true_type {};
64 template <> struct is_builtin_deserializable<uint64_t> : std::true_type {};
65 template <> struct is_builtin_deserializable<double> : std::true_type {};
66 template <> struct is_builtin_deserializable<bool> : std::true_type {};
67 template <> struct is_builtin_deserializable<SIMDJSON_IMPLEMENTATION::ondemand::array> : std::true_type {};
68 template <> struct is_builtin_deserializable<SIMDJSON_IMPLEMENTATION::ondemand::object> : std::true_type {};
69 template <> struct is_builtin_deserializable<SIMDJSON_IMPLEMENTATION::ondemand::value> : std::true_type {};
70 template <> struct is_builtin_deserializable<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> : std::true_type {};
71 template <> struct is_builtin_deserializable<std::string_view> : std::true_type {};
72 
73 template <typename T>
74 concept is_builtin_deserializable_v = is_builtin_deserializable<T>::value;
75 
76 template <typename T, typename ValT = SIMDJSON_IMPLEMENTATION::ondemand::value>
77 concept custom_deserializable = tag_invocable<deserialize_tag, ValT&, T&>;
78 
79 template <typename T, typename ValT = SIMDJSON_IMPLEMENTATION::ondemand::value>
80 concept deserializable = custom_deserializable<T, ValT> || is_builtin_deserializable_v<T>;
81 
82 template <typename T, typename ValT = SIMDJSON_IMPLEMENTATION::ondemand::value>
83 concept nothrow_custom_deserializable = nothrow_tag_invocable<deserialize_tag, ValT&, T&>;
84 
85 // built-in types are noexcept and if an error happens, the value simply gets ignored and the error is returned.
86 template <typename T, typename ValT = SIMDJSON_IMPLEMENTATION::ondemand::value>
87 concept nothrow_deserializable = nothrow_custom_deserializable<T, ValT> || is_builtin_deserializable_v<T>;
88 
90 inline constexpr struct deserialize_tag {
91  using value_type = SIMDJSON_IMPLEMENTATION::ondemand::value;
92  using document_type = SIMDJSON_IMPLEMENTATION::ondemand::document;
93  using document_reference_type = SIMDJSON_IMPLEMENTATION::ondemand::document_reference;
94 
95  // Customization Point for value
96  template <typename T>
97  requires custom_deserializable<T, value_type>
98  [[nodiscard]] constexpr /* error_code */ auto operator()(value_type &object, T& output) const noexcept(nothrow_custom_deserializable<T, value_type>) {
99  return tag_invoke(*this, object, output);
100  }
101 
102  // Customization Point for document
103  template <typename T>
104  requires custom_deserializable<T, document_type>
105  [[nodiscard]] constexpr /* error_code */ auto operator()(document_type &object, T& output) const noexcept(nothrow_custom_deserializable<T, document_type>) {
106  return tag_invoke(*this, object, output);
107  }
108 
109  // Customization Point for document reference
110  template <typename T>
111  requires custom_deserializable<T, document_reference_type>
112  [[nodiscard]] constexpr /* error_code */ auto operator()(document_reference_type &object, T& output) const noexcept(nothrow_custom_deserializable<T, document_reference_type>) {
113  return tag_invoke(*this, object, output);
114  }
115 
116 
117 } deserialize{};
118 
119 } // namespace simdjson
120 
121 #endif // SIMDJSON_ONDEMAND_DESERIALIZE_H
122 #endif // SIMDJSON_SUPPORTS_DESERIALIZATION
123 
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8