12 #ifndef DOCWIRE_SERIALIZATION_BASE_H
13 #define DOCWIRE_SERIALIZATION_BASE_H
15 #include "concepts_container.h"
16 #include "concepts_misc.h"
17 #include "concepts_string.h"
18 #include "concepts_variant.h"
22 #include <type_traits>
25 #include "type_name.h"
46 namespace serialization
71 struct array { std::vector<value> v; };
74 struct object { std::map<std::string, value> v; };
90 {
"typeid", typeid_str},
111 template <
typename T>
114 template <
typename T>
117 template <
typename T>
120 return serializer<T>{}.typed_summary(
value);
126 template <
typename T>
133 template <value_alternative T>
136 static constexpr
serializer_kind kind = serializer_kind::value_alternative;
150 template <
typename T>
requires(std::is_arithmetic_v<T> && !value_alternative<T>)
156 if constexpr (std::is_integral_v<T> && std::is_signed_v<T>)
157 return static_cast<std::int64_t
>(
value);
158 else if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T>)
159 return static_cast<std::uint64_t
>(
value);
161 return static_cast<double>(
value);
172 template <
typename T>
requires string_like<T> && (!value_alternative<T>)
178 if constexpr (std::is_pointer_v<std::decay_t<T>>) {
179 if (val ==
nullptr) {
183 return std::string(val);
191 template <strong_type_alias T>
204 value full(
const T& val)
const {
return object{}; }
213 template <
typename T>
214 requires (dereferenceable<T> && !container<T> && !string_like<T> && !value_alternative<T>)
217 static constexpr
serializer_kind kind = serializer_kind::dereferenceable;
230 {
"typeid", type_name::pretty<T>()},
244 template <
typename T>
requires (container<T> && !string_like<T> && !value_alternative<T>)
260 arr.v.push_back(serialization::typed_summary(item));
273 template<
typename... Ts>
276 value full(
const std::variant<Ts...>& variant)
const
278 return std::visit([](
const auto&
value) {
283 value typed_summary(
const std::variant<Ts...>& variant)
const {
return decorate_with_typeid(this->
full(variant), type_name::pretty<std::variant<Ts...>>()); }
value decorate_with_typeid(const value &base_val, const std::string &typeid_str)
Helper to decorate a serialized value with a typeid string.
requires(std::is_arithmetic_v< T > &&!value_alternative< T >) struct serializer< T >
Specialization for arithmetic types (integers, floats).
std::variant< std::nullptr_t, bool, std::int64_t, std::uint64_t, double, std::string, array, object > value
A variant type representing any serialized value.
serializer_kind
An enum to identify the kind of serializer specialization.
concept value_alternative
A specific concept to check if a type T is one of the alternatives in docwire::serialization::value.
value full(const T &value)
Serializes a value of type T into a docwire::serialization::value.
The main namespace for the DocWire SDK.
concept container
Concept to detect if a type is a container (iterable and not self-recursive).
concept dereferenceable
Concept to detect if a type is dereferenceable like a pointer.
concept string_like
Concept for string-like types that can be converted to a string view.
Represents a serialized array (list of values).
Represents a serialized object (map of string keys to values).
Specialization for types that are direct alternatives in docwire::serialization::value....
Primary template for the serializer.