12 #ifndef __JSONV_SERIALIZATION_HPP_INCLUDED__ 13 #define __JSONV_SERIALIZATION_HPP_INCLUDED__ 25 #include <type_traits> 39 class extraction_context;
40 class serialization_context;
48 using version_element = std::uint32_t;
52 constexpr
version(version_element major = 0, version_element minor = 0) :
60 return major == 0 && minor == 0;
66 explicit constexpr
operator std::uint64_t()
const 68 return static_cast<std::uint64_t
>(major) << 32
69 | static_cast<std::uint64_t>(minor) << 0;
75 return static_cast<std::uint64_t
>(*this) ==
static_cast<std::uint64_t
>(other);
81 return static_cast<std::uint64_t
>(*this) !=
static_cast<std::uint64_t
>(other);
87 return static_cast<std::uint64_t
>(*this) <
static_cast<std::uint64_t
>(other);
93 return static_cast<std::uint64_t
>(*this) <=
static_cast<std::uint64_t
>(other);
99 return static_cast<std::uint64_t
>(*this) >
static_cast<std::uint64_t
>(other);
105 return static_cast<std::uint64_t
>(*this) >=
static_cast<std::uint64_t
>(other);
109 version_element major;
110 version_element minor;
130 public std::invalid_argument
137 std::type_index type_index()
const;
140 std::type_index _type_index;
147 public std::runtime_error,
148 public nested_exception
165 public std::runtime_error
178 std::type_index type_index()
const;
181 std::type_index _type_index;
182 std::string _type_name;
187 public std::runtime_error
200 std::type_index type_index()
const;
203 std::type_index _type_index;
204 std::string _type_name;
216 virtual const std::type_info& get_type()
const = 0;
243 virtual const std::type_info& get_type()
const = 0;
324 using list = std::vector<formats>;
388 static formats compose(
const list& bases);
396 void extract(
const std::type_info& type,
406 const extractor& get_extractor(std::type_index type)
const;
412 const extractor& get_extractor(
const std::type_info& type)
const;
429 const serializer& get_serializer(std::type_index type)
const;
435 const serializer& get_serializer(
const std::type_info& type)
const;
449 void register_extractor(std::shared_ptr<const extractor>,
464 void register_serializer(std::shared_ptr<const serializer>,
481 void register_adapter(std::shared_ptr<const adapter>,
487 bool operator==(
const formats& other)
const;
490 bool operator!=(
const formats& other)
const;
496 explicit formats(std::vector<std::shared_ptr<const data>> bases);
499 std::shared_ptr<data> _data;
512 const void* userdata =
nullptr 538 const void* _user_data;
552 const void* userdata =
nullptr 571 template <
typename T>
574 typename std::aligned_storage<sizeof(T), alignof(T)>::type place[1];
575 T* ptr =
reinterpret_cast<T*
>(place);
576 extract(
typeid(T), from, static_cast<void*>(ptr));
577 auto destroy = detail::on_scope_exit([ptr] { ptr->~T(); });
578 return std::move(*ptr);
581 void extract(
const std::type_info& type,
592 template <
typename T>
595 typename std::aligned_storage<sizeof(T), alignof(T)>::type place[1];
596 T* ptr =
reinterpret_cast<T*
>(place);
597 extract_sub(
typeid(T), from, std::move(subpath), static_cast<void*>(ptr));
598 auto destroy = detail::on_scope_exit([ptr] { ptr->~T(); });
599 return std::move(*ptr);
602 void extract_sub(
const std::type_info& type,
const value& from,
jsonv::path subpath,
void* into)
const;
610 template <
typename T>
613 return extract_sub<T>(from,
jsonv::path({ elem }));
621 template <
typename T>
625 return context.
extract<T>(from);
629 template <
typename T>
633 return context.
extract<T>(from);
646 const void* userdata =
nullptr 655 template <
typename T>
658 return to_json(
typeid(T), static_cast<const void*>(&from));
665 value to_json(
const std::type_info& type,
const void* from)
const;
669 template <
typename T>
677 template <
typename T>
Definition of the on_scope_exit utility.
Copyright (c) 2015 by Travis Gockel.
constexpr bool operator>=(const version &other) const
Check that this version is greater than or equal to other.
A duplicate_type_error should be thrown.
constexpr bool operator==(const version &other) const
Test for equality with other.
Copyright (c) 2014-2019 by Travis Gockel.
constexpr version(version_element major=0, version_element minor=0)
Initialize an instance with the given major and minor version info.
An adapter is both an extractor and a serializer.
The existing extractor or serializer should be kept, but no exception should be thrown.
constexpr bool empty() const
Check if this version is an "empty" value – meaning major and minor are both 0.
Thrown when formats::to_json does not have a serializer for the provided type.
The new extractor or serializer should be inserted, and no exception should be thrown.
constexpr bool operator<(const version &other) const
Check that this version is less than other.
A serializer holds the method for converting an arbitrary C++ type into a value.
const jsonv::version version() const
Get the version this extraction_context was created with.
Represents an exact path in some JSON structure.
const jsonv::formats & formats() const
Get the formats object backing extraction and encoding.
constexpr bool operator!=(const version &other) const
Test for inequality with other.
constexpr bool operator<=(const version &other) const
Check that this version is less than or equal to other.
Represents a version used to extract and encode JSON objects from C++ classes.
Exception thrown if an insertion of an extractor or serializer into a formats is attempted, but there is already an extractor or serializer for that type.
T extract(const value &from)
Extract a C++ value from from using jsonv::formats::global().
Provides extra information to routines used for extraction.
value to_json(const T &from) const
Convenience function for converting a C++ object into a JSON value.
constexpr bool operator>(const version &other) const
Check that this version is greater than other.
#define JSONV_PUBLIC
This function or class is part of the public API for JsonVoorhees.
JSONV_STRING_VIEW_TYPE string_view
A non-owning reference to a string.
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
value to_json(const T &from)
Encode a JSON value from from using jsonv::formats::global().
Copyright (c) 2012-2018 by Travis Gockel.
const void * user_data() const
Get a pointer to arbitrary user data.
duplicate_type_action
The action to take when an insertion of an extractor or serializer into a formats is attempted...