-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
142f29c
commit 327ba90
Showing
40 changed files
with
26,448 additions
and
110,522 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#pragma once | ||
|
||
#include <type_traits> | ||
#include <utility> | ||
|
||
#include <nlohmann/detail/conversions/from_json.hpp> | ||
#include <nlohmann/detail/conversions/to_json.hpp> | ||
#include <nlohmann/detail/meta/identity_tag.hpp> | ||
#include <nlohmann/detail/meta/type_traits.hpp> | ||
|
||
namespace nlohmann | ||
{ | ||
|
||
template<typename ValueType, typename> | ||
struct adl_serializer | ||
{ | ||
/*! | ||
@brief convert a JSON value to any value type | ||
This function is usually called by the `get()` function of the | ||
@ref basic_json class (either explicit or via conversion operators). | ||
@note This function is chosen for default-constructible value types. | ||
@param[in] j JSON value to read from | ||
@param[in,out] val value to write to | ||
*/ | ||
template<typename BasicJsonType, typename TargetType = ValueType> | ||
static auto from_json(BasicJsonType && j, TargetType& val) noexcept( | ||
noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val))) | ||
-> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void()) | ||
{ | ||
::nlohmann::from_json(std::forward<BasicJsonType>(j), val); | ||
} | ||
|
||
/*! | ||
@brief convert a JSON value to any value type | ||
This function is usually called by the `get()` function of the | ||
@ref basic_json class (either explicit or via conversion operators). | ||
@note This function is chosen for value types which are not default-constructible. | ||
@param[in] j JSON value to read from | ||
@return copy of the JSON value, converted to @a ValueType | ||
*/ | ||
template<typename BasicJsonType, typename TargetType = ValueType> | ||
static auto from_json(BasicJsonType && j) noexcept( | ||
noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))) | ||
-> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})) | ||
{ | ||
return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}); | ||
} | ||
|
||
/*! | ||
@brief convert any value type to a JSON value | ||
This function is usually called by the constructors of the @ref basic_json | ||
class. | ||
@param[in,out] j JSON value to write to | ||
@param[in] val value to read from | ||
*/ | ||
template<typename BasicJsonType, typename TargetType = ValueType> | ||
static auto to_json(BasicJsonType& j, TargetType && val) noexcept( | ||
noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val)))) | ||
-> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void()) | ||
{ | ||
::nlohmann::to_json(j, std::forward<TargetType>(val)); | ||
} | ||
}; | ||
} // namespace nlohmann |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
#pragma once | ||
|
||
#include <cstdint> // uint8_t, uint64_t | ||
#include <tuple> // tie | ||
#include <utility> // move | ||
|
||
namespace nlohmann | ||
{ | ||
|
||
/*! | ||
@brief an internal type for a backed binary type | ||
This type extends the template parameter @a BinaryType provided to `basic_json` | ||
with a subtype used by BSON and MessagePack. This type exists so that the user | ||
does not have to specify a type themselves with a specific naming scheme in | ||
order to override the binary type. | ||
@tparam BinaryType container to store bytes (`std::vector<std::uint8_t>` by | ||
default) | ||
@since version 3.8.0; changed type of subtypes to std::uint64_t in 3.10.0. | ||
*/ | ||
template<typename BinaryType> | ||
class byte_container_with_subtype : public BinaryType | ||
{ | ||
public: | ||
/// the type of the underlying container | ||
using container_type = BinaryType; | ||
/// the type of the subtype | ||
using subtype_type = std::uint64_t; | ||
|
||
byte_container_with_subtype() noexcept(noexcept(container_type())) | ||
: container_type() | ||
{} | ||
|
||
byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b))) | ||
: container_type(b) | ||
{} | ||
|
||
byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b)))) | ||
: container_type(std::move(b)) | ||
{} | ||
|
||
byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b))) | ||
: container_type(b) | ||
, m_subtype(subtype_) | ||
, m_has_subtype(true) | ||
{} | ||
|
||
byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b)))) | ||
: container_type(std::move(b)) | ||
, m_subtype(subtype_) | ||
, m_has_subtype(true) | ||
{} | ||
|
||
bool operator==(const byte_container_with_subtype& rhs) const | ||
{ | ||
return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) == | ||
std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype); | ||
} | ||
|
||
bool operator!=(const byte_container_with_subtype& rhs) const | ||
{ | ||
return !(rhs == *this); | ||
} | ||
|
||
/*! | ||
@brief sets the binary subtype | ||
Sets the binary subtype of the value, also flags a binary JSON value as | ||
having a subtype, which has implications for serialization. | ||
@complexity Constant. | ||
@exceptionsafety No-throw guarantee: this member function never throws | ||
exceptions. | ||
@sa see @ref subtype() -- return the binary subtype | ||
@sa see @ref clear_subtype() -- clears the binary subtype | ||
@sa see @ref has_subtype() -- returns whether or not the binary value has a | ||
subtype | ||
@since version 3.8.0 | ||
*/ | ||
void set_subtype(subtype_type subtype_) noexcept | ||
{ | ||
m_subtype = subtype_; | ||
m_has_subtype = true; | ||
} | ||
|
||
/*! | ||
@brief return the binary subtype | ||
Returns the numerical subtype of the value if it has a subtype. If it does | ||
not have a subtype, this function will return subtype_type(-1) as a sentinel | ||
value. | ||
@return the numerical subtype of the binary value | ||
@complexity Constant. | ||
@exceptionsafety No-throw guarantee: this member function never throws | ||
exceptions. | ||
@sa see @ref set_subtype() -- sets the binary subtype | ||
@sa see @ref clear_subtype() -- clears the binary subtype | ||
@sa see @ref has_subtype() -- returns whether or not the binary value has a | ||
subtype | ||
@since version 3.8.0; fixed return value to properly return | ||
subtype_type(-1) as documented in version 3.10.0 | ||
*/ | ||
constexpr subtype_type subtype() const noexcept | ||
{ | ||
return m_has_subtype ? m_subtype : subtype_type(-1); | ||
} | ||
|
||
/*! | ||
@brief return whether the value has a subtype | ||
@return whether the value has a subtype | ||
@complexity Constant. | ||
@exceptionsafety No-throw guarantee: this member function never throws | ||
exceptions. | ||
@sa see @ref subtype() -- return the binary subtype | ||
@sa see @ref set_subtype() -- sets the binary subtype | ||
@sa see @ref clear_subtype() -- clears the binary subtype | ||
@since version 3.8.0 | ||
*/ | ||
constexpr bool has_subtype() const noexcept | ||
{ | ||
return m_has_subtype; | ||
} | ||
|
||
/*! | ||
@brief clears the binary subtype | ||
Clears the binary subtype and flags the value as not having a subtype, which | ||
has implications for serialization; for instance MessagePack will prefer the | ||
bin family over the ext family. | ||
@complexity Constant. | ||
@exceptionsafety No-throw guarantee: this member function never throws | ||
exceptions. | ||
@sa see @ref subtype() -- return the binary subtype | ||
@sa see @ref set_subtype() -- sets the binary subtype | ||
@sa see @ref has_subtype() -- returns whether or not the binary value has a | ||
subtype | ||
@since version 3.8.0 | ||
*/ | ||
void clear_subtype() noexcept | ||
{ | ||
m_subtype = 0; | ||
m_has_subtype = false; | ||
} | ||
|
||
private: | ||
subtype_type m_subtype = 0; | ||
bool m_has_subtype = false; | ||
}; | ||
|
||
} // namespace nlohmann |
Oops, something went wrong.