Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions include/nlohmann/detail/json_metadata.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

namespace nlohmann
{
namespace detail
{
template<class MetaDataType>
class json_metadata
{
public:
using metadata_t = MetaDataType;
metadata_t& metadata()
{
return m_metadata;
}
const metadata_t& metadata() const
{
return m_metadata;
}
private:
metadata_t m_metadata = {};
};

template<>
class json_metadata<void>
{
//no metadata
};
} // namespace detail
} // namespace nlohmann
5 changes: 3 additions & 2 deletions include/nlohmann/detail/macro_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,13 @@
class NumberUnsignedType, class NumberFloatType, \
template<typename> class AllocatorType, \
template<typename, typename = void> class JSONSerializer, \
class BinaryType>
class BinaryType, \
class MetaDataType>

#define NLOHMANN_BASIC_JSON_TPL \
basic_json<ObjectType, ArrayType, StringType, BooleanType, \
NumberIntegerType, NumberUnsignedType, NumberFloatType, \
AllocatorType, JSONSerializer, BinaryType>
AllocatorType, JSONSerializer, BinaryType, MetaDataType>

// Macros to simplify conversion from/to types

Expand Down
13 changes: 10 additions & 3 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ SOFTWARE.
#include <nlohmann/detail/iterators/iteration_proxy.hpp>
#include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
#include <nlohmann/detail/iterators/primitive_iterator.hpp>
#include <nlohmann/detail/json_metadata.hpp>
#include <nlohmann/detail/json_pointer.hpp>
#include <nlohmann/detail/json_ref.hpp>
#include <nlohmann/detail/macro_scope.hpp>
Expand Down Expand Up @@ -174,6 +175,7 @@ Format](https://tools.ietf.org/html/rfc8259)
*/
NLOHMANN_BASIC_JSON_TPL_DECLARATION
class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
: public ::nlohmann::detail::json_metadata<MetaDataType>
{
private:
template<detail::value_t> friend struct detail::external_constructor;
Expand All @@ -196,6 +198,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// workaround type for MSVC
using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
using json_metadata_t = ::nlohmann::detail::json_metadata<MetaDataType>;

JSON_PRIVATE_UNLESS_TESTED:
// convenience aliases for types residing in namespace detail;
Expand Down Expand Up @@ -2188,7 +2191,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@since version 1.0.0
*/
basic_json(const basic_json& other)
: m_type(other.m_type)
: json_metadata_t(other),
m_type(other.m_type)
{
// check of passed value is valid
other.assert_invariant();
Expand Down Expand Up @@ -2280,7 +2284,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@since version 1.0.0
*/
basic_json(basic_json&& other) noexcept
: m_type(std::move(other.m_type)),
: json_metadata_t(std::move(other)),
m_type(std::move(other.m_type)),
m_value(std::move(other.m_value))
{
// check that passed value is valid
Expand Down Expand Up @@ -2321,7 +2326,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
std::is_nothrow_move_constructible<value_t>::value&&
std::is_nothrow_move_assignable<value_t>::value&&
std::is_nothrow_move_constructible<json_value>::value&&
std::is_nothrow_move_assignable<json_value>::value
std::is_nothrow_move_assignable<json_value>::value&&
std::is_nothrow_move_assignable<json_metadata_t>::value
)
{
// check that passed value is valid
Expand All @@ -2330,6 +2336,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
using std::swap;
swap(m_type, other.m_type);
swap(m_value, other.m_value);
json_metadata_t::operator=(std::move(other));

set_parents();
assert_invariant();
Expand Down
3 changes: 2 additions & 1 deletion include/nlohmann/json_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ template<template<typename U, typename V, typename... Args> class ObjectType =
template<typename U> class AllocatorType = std::allocator,
template<typename T, typename SFINAE = void> class JSONSerializer =
adl_serializer,
class BinaryType = std::vector<std::uint8_t>>
class BinaryType = std::vector<std::uint8_t>,
class MetaDataType = void>
class basic_json;

/*!
Expand Down
54 changes: 47 additions & 7 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2343,12 +2343,13 @@ JSON_HEDLEY_DIAGNOSTIC_POP
class NumberUnsignedType, class NumberFloatType, \
template<typename> class AllocatorType, \
template<typename, typename = void> class JSONSerializer, \
class BinaryType>
class BinaryType, \
class MetaDataType>

#define NLOHMANN_BASIC_JSON_TPL \
basic_json<ObjectType, ArrayType, StringType, BooleanType, \
NumberIntegerType, NumberUnsignedType, NumberFloatType, \
AllocatorType, JSONSerializer, BinaryType>
AllocatorType, JSONSerializer, BinaryType, MetaDataType>

// Macros to simplify conversion from/to types

Expand Down Expand Up @@ -3380,7 +3381,8 @@ template<template<typename U, typename V, typename... Args> class ObjectType =
template<typename U> class AllocatorType = std::allocator,
template<typename T, typename SFINAE = void> class JSONSerializer =
adl_serializer,
class BinaryType = std::vector<std::uint8_t>>
class BinaryType = std::vector<std::uint8_t>,
class MetaDataType = void>
class basic_json;

/*!
Expand Down Expand Up @@ -12354,6 +12356,38 @@ class json_reverse_iterator : public std::reverse_iterator<Base>

// #include <nlohmann/detail/iterators/primitive_iterator.hpp>

// #include <nlohmann/detail/json_metadata.hpp>


namespace nlohmann
{
namespace detail
{
template<class MetaDataType>
class json_metadata
{
public:
using metadata_t = MetaDataType;
metadata_t& metadata()
{
return m_metadata;
}
const metadata_t& metadata() const
{
return m_metadata;
}
private:
metadata_t m_metadata = {};
};

template<>
class json_metadata<void>
{
//no metadata
};
} // namespace detail
} // namespace nlohmann

// #include <nlohmann/detail/json_pointer.hpp>


Expand Down Expand Up @@ -17580,6 +17614,7 @@ Format](https://tools.ietf.org/html/rfc8259)
*/
NLOHMANN_BASIC_JSON_TPL_DECLARATION
class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
: public ::nlohmann::detail::json_metadata<MetaDataType>
{
private:
template<detail::value_t> friend struct detail::external_constructor;
Expand All @@ -17602,6 +17637,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// workaround type for MSVC
using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
using json_metadata_t = ::nlohmann::detail::json_metadata<MetaDataType>;

JSON_PRIVATE_UNLESS_TESTED:
// convenience aliases for types residing in namespace detail;
Expand All @@ -17613,7 +17649,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
detail::parser_callback_t<basic_json>cb = nullptr,
const bool allow_exceptions = true,
const bool ignore_comments = false
)
)
{
return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
std::move(cb), allow_exceptions, ignore_comments);
Expand Down Expand Up @@ -19594,7 +19630,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@since version 1.0.0
*/
basic_json(const basic_json& other)
: m_type(other.m_type)
: json_metadata_t(other),
m_type(other.m_type)
{
// check of passed value is valid
other.assert_invariant();
Expand Down Expand Up @@ -19686,7 +19723,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@since version 1.0.0
*/
basic_json(basic_json&& other) noexcept
: m_type(std::move(other.m_type)),
: json_metadata_t(std::move(other)),
m_type(std::move(other.m_type)),
m_value(std::move(other.m_value))
{
// check that passed value is valid
Expand Down Expand Up @@ -19727,7 +19765,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
std::is_nothrow_move_constructible<value_t>::value&&
std::is_nothrow_move_assignable<value_t>::value&&
std::is_nothrow_move_constructible<json_value>::value&&
std::is_nothrow_move_assignable<json_value>::value
std::is_nothrow_move_assignable<json_value>::value&&
std::is_nothrow_move_assignable<json_metadata_t>::value
)
{
// check that passed value is valid
Expand All @@ -19736,6 +19775,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
using std::swap;
swap(m_type, other.m_type);
swap(m_value, other.m_value);
json_metadata_t::operator=(std::move(other));

set_parents();
assert_invariant();
Expand Down
Loading