2018-01-10 17:18:31 +08:00
|
|
|
#pragma once
|
2017-08-14 22:40:38 +08:00
|
|
|
|
2019-03-15 21:55:13 +08:00
|
|
|
#include <algorithm> // copy
|
2017-08-15 01:28:01 +08:00
|
|
|
#include <iterator> // begin, end
|
2019-03-15 21:55:13 +08:00
|
|
|
#include <string> // string
|
2017-08-15 01:28:01 +08:00
|
|
|
#include <tuple> // tuple, get
|
|
|
|
#include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
|
|
|
|
#include <utility> // move, forward, declval, pair
|
|
|
|
#include <valarray> // valarray
|
|
|
|
#include <vector> // vector
|
2017-08-14 22:40:38 +08:00
|
|
|
|
2019-03-17 19:01:49 +08:00
|
|
|
#include <nlohmann/detail/iterators/iteration_proxy.hpp>
|
2018-07-24 19:22:44 +08:00
|
|
|
#include <nlohmann/detail/meta/cpp_future.hpp>
|
|
|
|
#include <nlohmann/detail/meta/type_traits.hpp>
|
2018-01-29 18:21:11 +08:00
|
|
|
#include <nlohmann/detail/value_t.hpp>
|
2017-08-14 22:40:38 +08:00
|
|
|
|
|
|
|
namespace nlohmann
|
|
|
|
{
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
//////////////////
|
|
|
|
// constructors //
|
|
|
|
//////////////////
|
|
|
|
|
|
|
|
template<value_t> struct external_constructor;
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct external_constructor<value_t::boolean>
|
|
|
|
{
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
|
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::boolean;
|
|
|
|
j.m_value = b;
|
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct external_constructor<value_t::string>
|
|
|
|
{
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
|
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::string;
|
|
|
|
j.m_value = s;
|
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
|
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::string;
|
|
|
|
j.m_value = std::move(s);
|
|
|
|
j.assert_invariant();
|
|
|
|
}
|
2018-06-04 11:49:13 +08:00
|
|
|
|
2020-06-03 20:20:36 +08:00
|
|
|
template < typename BasicJsonType, typename CompatibleStringType,
|
|
|
|
enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
|
|
|
|
int > = 0 >
|
2018-06-23 23:27:40 +08:00
|
|
|
static void construct(BasicJsonType& j, const CompatibleStringType& str)
|
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2018-06-23 23:27:40 +08:00
|
|
|
j.m_type = value_t::string;
|
|
|
|
j.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
|
|
|
|
j.assert_invariant();
|
|
|
|
}
|
2017-08-14 22:40:38 +08:00
|
|
|
};
|
|
|
|
|
2019-07-05 12:13:25 +08:00
|
|
|
template<>
|
|
|
|
struct external_constructor<value_t::binary>
|
|
|
|
{
|
|
|
|
template<typename BasicJsonType>
|
2020-05-18 04:50:27 +08:00
|
|
|
static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
|
2019-07-05 12:13:25 +08:00
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2019-07-05 12:13:25 +08:00
|
|
|
j.m_type = value_t::binary;
|
2021-03-24 14:15:18 +08:00
|
|
|
j.m_value = typename BasicJsonType::binary_t(b);
|
2019-07-05 12:13:25 +08:00
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
2020-05-18 04:50:27 +08:00
|
|
|
static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
|
2019-07-05 12:13:25 +08:00
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2019-07-05 12:13:25 +08:00
|
|
|
j.m_type = value_t::binary;
|
2021-03-24 14:15:18 +08:00
|
|
|
j.m_value = typename BasicJsonType::binary_t(std::move(b));;
|
2019-07-05 12:13:25 +08:00
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-14 22:40:38 +08:00
|
|
|
template<>
|
|
|
|
struct external_constructor<value_t::number_float>
|
|
|
|
{
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
|
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::number_float;
|
|
|
|
j.m_value = val;
|
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct external_constructor<value_t::number_unsigned>
|
|
|
|
{
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
|
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::number_unsigned;
|
|
|
|
j.m_value = val;
|
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct external_constructor<value_t::number_integer>
|
|
|
|
{
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
|
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::number_integer;
|
|
|
|
j.m_value = val;
|
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct external_constructor<value_t::array>
|
|
|
|
{
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
|
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::array;
|
|
|
|
j.m_value = arr;
|
2021-01-15 04:55:49 +08:00
|
|
|
j.set_parents();
|
2017-08-14 22:40:38 +08:00
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
|
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::array;
|
|
|
|
j.m_value = std::move(arr);
|
2021-01-15 04:55:49 +08:00
|
|
|
j.set_parents();
|
2017-08-14 22:40:38 +08:00
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
|
2020-06-03 20:20:36 +08:00
|
|
|
template < typename BasicJsonType, typename CompatibleArrayType,
|
|
|
|
enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
|
|
|
|
int > = 0 >
|
2017-08-14 22:40:38 +08:00
|
|
|
static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
|
|
|
|
{
|
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
2021-07-16 02:32:37 +08:00
|
|
|
|
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::array;
|
|
|
|
j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
|
2021-01-15 04:55:49 +08:00
|
|
|
j.set_parents();
|
2017-08-14 22:40:38 +08:00
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
static void construct(BasicJsonType& j, const std::vector<bool>& arr)
|
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::array;
|
|
|
|
j.m_value = value_t::array;
|
|
|
|
j.m_value.array->reserve(arr.size());
|
|
|
|
for (const bool x : arr)
|
|
|
|
{
|
|
|
|
j.m_value.array->push_back(x);
|
2021-01-15 05:05:08 +08:00
|
|
|
j.set_parent(j.m_value.array->back());
|
2017-08-14 22:40:38 +08:00
|
|
|
}
|
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType, typename T,
|
|
|
|
enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
|
|
|
|
static void construct(BasicJsonType& j, const std::valarray<T>& arr)
|
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::array;
|
|
|
|
j.m_value = value_t::array;
|
|
|
|
j.m_value.array->resize(arr.size());
|
external_constructor<std::valarray>: Handle empty array properly
Clang UBSAN complains with the following message when an empty std::valarray is passed in:
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/valarray:571:14 in
2/2 Test #68: test-regression_all ..............***Failed 4.68 sec
/usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/valarray:571:14: runtime error: reference binding to null pointer of type 'const do
uble'
#0 0x6fbe57 in std::valarray<double>::operator[](unsigned long) const /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/valarray:
571:7
#1 0x6fbe57 in double const* std::begin<double>(std::valarray<double> const&) /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/v
alarray:1207
#2 0x6fbe57 in void nlohmann::detail::external_constructor<(nlohmann::detail::value_t)2>::construct<nlohmann::basic_json<std::map, std::vector, s
td::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_seri
alizer>, double, 0>(nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool
, long, unsigned long, double, std::allocator, nlohmann::adl_serializer>&, std::valarray<double> const&) /home/firma/devel/json/include/nlohmann/deta
il/conversions/to_json.hpp:157
#3 0x5e3fe3 in void nlohmann::detail::to_json<nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>
, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer>, double, 0>(nlohmann::basic_json<std::map, std
::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohman
n::adl_serializer>&, std::valarray<double> const&) /home/firma/devel/json/include/nlohmann/detail/conversions/to_json.hpp:270:5
#4 0x5e3fe3 in decltype((to_json(fp, std::forward<std::valarray<double>&>(fp0))) , ((void)())) nlohmann::detail::to_json_fn::operator()<nlohmann:
:basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double
, std::allocator, nlohmann::adl_serializer>, std::valarray<double>&>(nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std
::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer>&, std::valarray<double>&) c
onst /home/firma/devel/json/include/nlohmann/detail/conversions/to_json.hpp:334
#5 0x5e3fe3 in decltype((nlohmann::(anonymous namespace)::to_json(fp, std::forward<std::valarray<double>&>(fp0))) , ((void)())) nlohmann::adl_ser
ializer<std::valarray<double>, void>::to_json<nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, st
d::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer>, std::valarray<double>&>(nlohmann::basic_json<std:
:map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator
, nlohmann::adl_serializer>&, std::valarray<double>&) /home/firma/devel/json/include/nlohmann/adl_serializer.hpp:45
#6 0x5e3fe3 in nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool,
long, unsigned long, double, std::allocator, nlohmann::adl_serializer>::basic_json<std::valarray<double>&, std::valarray<double>, 0>(std::valarray<d
ouble>&) /home/firma/devel/json/include/nlohmann/json.hpp:1257
#7 0x5e3fe3 in _DOCTEST_ANON_FUNC_2() /home/firma/devel/json/test/src/unit-regression.cpp:1377
#8 0x77313e in doctest::Context::run() /home/firma/devel/json/test/thirdparty/doctest/doctest.h:5938:21
#9 0x777ae0 in main /home/firma/devel/json/test/thirdparty/doctest/doctest.h:6016:71
#10 0x7fae220532e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
#11 0x4a6479 in _start (/home/firma/devel/json/build/test/test-regression+0x4a6479)
The important thing to note here is that a std::valarray is *not* a STL
container, so the usual containter and iterator semantics don't apply.
Therefore we have to check if the container is non-empty before.
2019-08-30 19:07:37 +08:00
|
|
|
if (arr.size() > 0)
|
|
|
|
{
|
|
|
|
std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
|
|
|
|
}
|
2021-01-15 04:55:49 +08:00
|
|
|
j.set_parents();
|
2017-08-14 22:40:38 +08:00
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct external_constructor<value_t::object>
|
|
|
|
{
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
|
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::object;
|
|
|
|
j.m_value = obj;
|
2021-01-15 04:55:49 +08:00
|
|
|
j.set_parents();
|
2017-08-14 22:40:38 +08:00
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
|
|
|
|
{
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::object;
|
|
|
|
j.m_value = std::move(obj);
|
2021-01-15 04:55:49 +08:00
|
|
|
j.set_parents();
|
2017-08-14 22:40:38 +08:00
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
|
2020-06-03 20:20:36 +08:00
|
|
|
template < typename BasicJsonType, typename CompatibleObjectType,
|
|
|
|
enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
|
2017-08-14 22:40:38 +08:00
|
|
|
static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
|
|
|
|
{
|
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
|
2021-07-16 02:32:37 +08:00
|
|
|
j.m_value.destroy(j.m_type);
|
2017-08-14 22:40:38 +08:00
|
|
|
j.m_type = value_t::object;
|
|
|
|
j.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
|
2021-01-15 04:55:49 +08:00
|
|
|
j.set_parents();
|
2017-08-14 22:40:38 +08:00
|
|
|
j.assert_invariant();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/////////////
|
|
|
|
// to_json //
|
|
|
|
/////////////
|
|
|
|
|
|
|
|
template<typename BasicJsonType, typename T,
|
|
|
|
enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
|
|
|
|
void to_json(BasicJsonType& j, T b) noexcept
|
|
|
|
{
|
|
|
|
external_constructor<value_t::boolean>::construct(j, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType, typename CompatibleString,
|
|
|
|
enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
|
|
|
|
void to_json(BasicJsonType& j, const CompatibleString& s)
|
|
|
|
{
|
|
|
|
external_constructor<value_t::string>::construct(j, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
|
|
|
|
{
|
|
|
|
external_constructor<value_t::string>::construct(j, std::move(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType, typename FloatType,
|
|
|
|
enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
|
|
|
|
void to_json(BasicJsonType& j, FloatType val) noexcept
|
|
|
|
{
|
|
|
|
external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
|
|
|
|
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
|
|
|
|
void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
|
|
|
|
{
|
|
|
|
external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType, typename CompatibleNumberIntegerType,
|
|
|
|
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
|
|
|
|
void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
|
|
|
|
{
|
|
|
|
external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType, typename EnumType,
|
|
|
|
enable_if_t<std::is_enum<EnumType>::value, int> = 0>
|
|
|
|
void to_json(BasicJsonType& j, EnumType e) noexcept
|
|
|
|
{
|
|
|
|
using underlying_type = typename std::underlying_type<EnumType>::type;
|
|
|
|
external_constructor<value_t::number_integer>::construct(j, static_cast<underlying_type>(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
void to_json(BasicJsonType& j, const std::vector<bool>& e)
|
|
|
|
{
|
|
|
|
external_constructor<value_t::array>::construct(j, e);
|
|
|
|
}
|
|
|
|
|
2020-06-03 20:20:36 +08:00
|
|
|
template < typename BasicJsonType, typename CompatibleArrayType,
|
|
|
|
enable_if_t < is_compatible_array_type<BasicJsonType,
|
|
|
|
CompatibleArrayType>::value&&
|
|
|
|
!is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
|
|
|
|
!is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
|
|
|
|
!std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
|
|
|
|
!is_basic_json<CompatibleArrayType>::value,
|
|
|
|
int > = 0 >
|
2017-08-14 22:40:38 +08:00
|
|
|
void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
|
|
|
|
{
|
|
|
|
external_constructor<value_t::array>::construct(j, arr);
|
|
|
|
}
|
|
|
|
|
2020-06-03 20:20:36 +08:00
|
|
|
template<typename BasicJsonType>
|
2020-05-18 04:50:27 +08:00
|
|
|
void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
|
2020-05-15 20:12:32 +08:00
|
|
|
{
|
|
|
|
external_constructor<value_t::binary>::construct(j, bin);
|
|
|
|
}
|
|
|
|
|
2017-08-14 22:40:38 +08:00
|
|
|
template<typename BasicJsonType, typename T,
|
|
|
|
enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
|
2018-06-23 16:47:55 +08:00
|
|
|
void to_json(BasicJsonType& j, const std::valarray<T>& arr)
|
2017-08-14 22:40:38 +08:00
|
|
|
{
|
|
|
|
external_constructor<value_t::array>::construct(j, std::move(arr));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
|
|
|
|
{
|
|
|
|
external_constructor<value_t::array>::construct(j, std::move(arr));
|
|
|
|
}
|
|
|
|
|
2020-06-03 20:20:36 +08:00
|
|
|
template < typename BasicJsonType, typename CompatibleObjectType,
|
|
|
|
enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
|
2017-08-14 22:40:38 +08:00
|
|
|
void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
|
|
|
|
{
|
|
|
|
external_constructor<value_t::object>::construct(j, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
|
|
|
|
{
|
|
|
|
external_constructor<value_t::object>::construct(j, std::move(obj));
|
|
|
|
}
|
|
|
|
|
2018-09-06 22:08:01 +08:00
|
|
|
template <
|
|
|
|
typename BasicJsonType, typename T, std::size_t N,
|
2020-06-03 20:20:36 +08:00
|
|
|
enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
|
2021-03-24 14:15:18 +08:00
|
|
|
const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
2020-06-03 20:20:36 +08:00
|
|
|
int > = 0 >
|
2021-03-24 14:15:18 +08:00
|
|
|
void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
2017-08-14 22:40:38 +08:00
|
|
|
{
|
|
|
|
external_constructor<value_t::array>::construct(j, arr);
|
|
|
|
}
|
|
|
|
|
2019-10-21 23:50:15 +08:00
|
|
|
template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
|
|
|
|
void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
|
2017-08-14 22:40:38 +08:00
|
|
|
{
|
2018-12-19 22:47:35 +08:00
|
|
|
j = { p.first, p.second };
|
2017-08-14 22:40:38 +08:00
|
|
|
}
|
|
|
|
|
2018-06-29 03:32:32 +08:00
|
|
|
// for https://github.com/nlohmann/json/pull/1134
|
2020-06-03 20:20:36 +08:00
|
|
|
template<typename BasicJsonType, typename T,
|
|
|
|
enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
|
2018-10-06 19:49:02 +08:00
|
|
|
void to_json(BasicJsonType& j, const T& b)
|
2018-06-16 17:01:49 +08:00
|
|
|
{
|
2018-12-19 22:47:35 +08:00
|
|
|
j = { {b.key(), b.value()} };
|
2018-06-16 17:01:49 +08:00
|
|
|
}
|
|
|
|
|
2017-08-14 22:40:38 +08:00
|
|
|
template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
|
2018-10-08 00:39:18 +08:00
|
|
|
void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
|
2017-08-14 22:40:38 +08:00
|
|
|
{
|
2018-12-19 22:47:35 +08:00
|
|
|
j = { std::get<Idx>(t)... };
|
2017-08-14 22:40:38 +08:00
|
|
|
}
|
|
|
|
|
2019-11-01 01:39:01 +08:00
|
|
|
template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
|
|
|
|
void to_json(BasicJsonType& j, const T& t)
|
2017-08-14 22:40:38 +08:00
|
|
|
{
|
2019-11-01 01:39:01 +08:00
|
|
|
to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
|
2017-08-14 22:40:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct to_json_fn
|
|
|
|
{
|
|
|
|
template<typename BasicJsonType, typename T>
|
2018-09-06 22:08:01 +08:00
|
|
|
auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
|
2017-08-14 22:40:38 +08:00
|
|
|
-> decltype(to_json(j, std::forward<T>(val)), void())
|
|
|
|
{
|
|
|
|
return to_json(j, std::forward<T>(val));
|
|
|
|
}
|
|
|
|
};
|
2018-10-08 00:39:18 +08:00
|
|
|
} // namespace detail
|
2017-08-14 22:40:38 +08:00
|
|
|
|
|
|
|
/// namespace to hold default `to_json` function
|
2021-03-24 14:15:18 +08:00
|
|
|
/// to see why this is required:
|
|
|
|
/// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
|
|
|
|
namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
|
2017-08-14 22:40:38 +08:00
|
|
|
{
|
2021-03-24 14:15:18 +08:00
|
|
|
constexpr const auto& to_json = detail::static_const<detail::to_json_fn>::value; // NOLINT(misc-definitions-in-headers)
|
2018-10-08 00:39:18 +08:00
|
|
|
} // namespace
|
2019-07-01 04:14:02 +08:00
|
|
|
} // namespace nlohmann
|