Allow to add metadata to json nodes
* by default no metadata is allowed and the library behaves as it already did * if a user explicitly adds metadata (last template parameter) each node has a data member of this type and access functions for this member * add test for this feature
This commit is contained in:
parent
0b345b20c8
commit
1b11773cd6
30
include/nlohmann/detail/json_metadata.hpp
Normal file
30
include/nlohmann/detail/json_metadata.hpp
Normal file
@ -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
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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>
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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();
|
||||
@ -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
|
||||
@ -2321,12 +2326,15 @@ 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
|
||||
other.assert_invariant();
|
||||
|
||||
json_metadata_t::operator=(std::move(other));
|
||||
|
||||
using std::swap;
|
||||
swap(m_type, other.m_type);
|
||||
swap(m_value, other.m_value);
|
||||
|
||||
@ -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;
|
||||
|
||||
/*!
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
|
||||
/*!
|
||||
@ -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>
|
||||
|
||||
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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();
|
||||
@ -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
|
||||
@ -19727,12 +19765,15 @@ 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
|
||||
other.assert_invariant();
|
||||
|
||||
json_metadata_t::operator=(std::move(other));
|
||||
|
||||
using std::swap;
|
||||
swap(m_type, other.m_type);
|
||||
swap(m_value, other.m_value);
|
||||
|
||||
185
test/src/unit-metadata.cpp
Normal file
185
test/src/unit-metadata.cpp
Normal file
@ -0,0 +1,185 @@
|
||||
/*
|
||||
__ _____ _____ _____
|
||||
__| | __| | | | JSON for Modern C++ (test suite)
|
||||
| | |__ | | | | | | version 3.10.2
|
||||
|_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||
|
||||
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
||||
SPDX-License-Identifier: MIT
|
||||
Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "doctest_compatibility.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
template<class T>
|
||||
using json_with_metadata =
|
||||
nlohmann::basic_json <
|
||||
std::map,
|
||||
std::vector,
|
||||
std::string,
|
||||
bool,
|
||||
std::int64_t,
|
||||
std::uint64_t,
|
||||
double,
|
||||
std::allocator,
|
||||
nlohmann::adl_serializer,
|
||||
std::vector<std::uint8_t>,
|
||||
T
|
||||
>;
|
||||
|
||||
TEST_CASE("JSON Node Metadata")
|
||||
{
|
||||
SECTION("type int")
|
||||
{
|
||||
using json = json_with_metadata<int>;
|
||||
json null;
|
||||
auto obj = json::object();
|
||||
auto array = json::array();
|
||||
|
||||
null.metadata() = 1;
|
||||
obj.metadata() = 2;
|
||||
array.metadata() = 3;
|
||||
auto copy = array;
|
||||
|
||||
CHECK(null.metadata() == 1);
|
||||
CHECK(obj.metadata() == 2);
|
||||
CHECK(array.metadata() == 3);
|
||||
CHECK(copy.metadata() == 3);
|
||||
}
|
||||
SECTION("type vector<int>")
|
||||
{
|
||||
using json = json_with_metadata<std::vector<int>>;
|
||||
json value;
|
||||
value.metadata().emplace_back(1);
|
||||
auto copy = value;
|
||||
value.metadata().emplace_back(2);
|
||||
|
||||
CHECK(copy.metadata().size() == 1);
|
||||
CHECK(copy.metadata().at(0) == 1);
|
||||
CHECK(value.metadata().size() == 2);
|
||||
CHECK(value.metadata().at(0) == 1);
|
||||
CHECK(value.metadata().at(1) == 2);
|
||||
}
|
||||
SECTION("copy ctor")
|
||||
{
|
||||
using json = json_with_metadata<std::vector<int>>;
|
||||
json value;
|
||||
value.metadata().emplace_back(1);
|
||||
value.metadata().emplace_back(2);
|
||||
|
||||
json copy = value;
|
||||
|
||||
CHECK(copy.metadata().size() == 2);
|
||||
CHECK(copy.metadata().at(0) == 1);
|
||||
CHECK(copy.metadata().at(1) == 2);
|
||||
CHECK(value.metadata().size() == 2);
|
||||
CHECK(value.metadata().at(0) == 1);
|
||||
CHECK(value.metadata().at(1) == 2);
|
||||
|
||||
value.metadata().clear();
|
||||
CHECK(copy.metadata().size() == 2);
|
||||
CHECK(value.metadata().size() == 0);
|
||||
}
|
||||
SECTION("move ctor")
|
||||
{
|
||||
using json = json_with_metadata<std::vector<int>>;
|
||||
json value;
|
||||
value.metadata().emplace_back(1);
|
||||
value.metadata().emplace_back(2);
|
||||
|
||||
const json moved = std::move(value);
|
||||
|
||||
CHECK(moved.metadata().size() == 2);
|
||||
CHECK(moved.metadata().at(0) == 1);
|
||||
CHECK(moved.metadata().at(1) == 2);
|
||||
CHECK(value.metadata().size() == 0);
|
||||
}
|
||||
SECTION("move assign")
|
||||
{
|
||||
using json = json_with_metadata<std::vector<int>>;
|
||||
json value;
|
||||
value.metadata().emplace_back(1);
|
||||
value.metadata().emplace_back(2);
|
||||
|
||||
json moved;
|
||||
moved = std::move(value);
|
||||
|
||||
CHECK(moved.metadata().size() == 2);
|
||||
CHECK(moved.metadata().at(0) == 1);
|
||||
CHECK(moved.metadata().at(1) == 2);
|
||||
CHECK(value.metadata().size() == 0);
|
||||
}
|
||||
SECTION("copy assign")
|
||||
{
|
||||
using json = json_with_metadata<std::vector<int>>;
|
||||
json value;
|
||||
value.metadata().emplace_back(1);
|
||||
value.metadata().emplace_back(2);
|
||||
|
||||
json copy;
|
||||
copy = value;
|
||||
|
||||
CHECK(copy.metadata().size() == 2);
|
||||
CHECK(copy.metadata().at(0) == 1);
|
||||
CHECK(copy.metadata().at(1) == 2);
|
||||
CHECK(value.metadata().size() == 2);
|
||||
CHECK(value.metadata().at(0) == 1);
|
||||
CHECK(value.metadata().at(1) == 2);
|
||||
|
||||
value.metadata().clear();
|
||||
CHECK(copy.metadata().size() == 2);
|
||||
CHECK(value.metadata().size() == 0);
|
||||
}
|
||||
SECTION("type unique_ptr<int>")
|
||||
{
|
||||
using json = json_with_metadata<std::unique_ptr<int>>;
|
||||
json value;
|
||||
value.metadata().reset(new int);
|
||||
(*value.metadata()) = 42;
|
||||
auto moved = std::move(value);
|
||||
|
||||
CHECK(value.metadata() == nullptr);
|
||||
CHECK(moved.metadata() != nullptr);
|
||||
CHECK(*moved.metadata() == 42);
|
||||
}
|
||||
SECTION("type vector<int> in json array")
|
||||
{
|
||||
using json = json_with_metadata<std::vector<int>>;
|
||||
json value;
|
||||
value.metadata().emplace_back(1);
|
||||
value.metadata().emplace_back(2);
|
||||
|
||||
json array(10, value);
|
||||
|
||||
CHECK(value.metadata().size() == 2);
|
||||
CHECK(value.metadata().at(0) == 1);
|
||||
CHECK(value.metadata().at(1) == 2);
|
||||
|
||||
for (const auto& val : array)
|
||||
{
|
||||
CHECK(val.metadata().size() == 2);
|
||||
CHECK(val.metadata().at(0) == 1);
|
||||
CHECK(val.metadata().at(1) == 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user