From 1542f34f0e0fc53324f6fdc5905f4b77b252a789 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 26 Nov 2018 09:06:31 +0100 Subject: update nlohmann modernjson to v3.4 (with bson support) --- include/lib/modernjson/detail/meta/cpp_future.hpp | 4 +- include/lib/modernjson/detail/meta/detected.hpp | 4 +- include/lib/modernjson/detail/meta/is_sax.hpp | 4 +- include/lib/modernjson/detail/meta/type_traits.hpp | 196 ++++++++++++++++----- include/lib/modernjson/detail/meta/void_t.hpp | 4 +- 5 files changed, 161 insertions(+), 51 deletions(-) (limited to 'include/lib/modernjson/detail/meta') diff --git a/include/lib/modernjson/detail/meta/cpp_future.hpp b/include/lib/modernjson/detail/meta/cpp_future.hpp index fa7478b..948cd4f 100644 --- a/include/lib/modernjson/detail/meta/cpp_future.hpp +++ b/include/lib/modernjson/detail/meta/cpp_future.hpp @@ -59,5 +59,5 @@ struct static_const template constexpr T static_const::value; -} -} +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/meta/detected.hpp b/include/lib/modernjson/detail/meta/detected.hpp index 78b25d5..8fb318a 100644 --- a/include/lib/modernjson/detail/meta/detected.hpp +++ b/include/lib/modernjson/detail/meta/detected.hpp @@ -52,5 +52,5 @@ using is_detected_exact = std::is_same>; template class Op, class... Args> using is_detected_convertible = std::is_convertible, To>; -} -} +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/meta/is_sax.hpp b/include/lib/modernjson/detail/meta/is_sax.hpp index 63f9793..af63386 100644 --- a/include/lib/modernjson/detail/meta/is_sax.hpp +++ b/include/lib/modernjson/detail/meta/is_sax.hpp @@ -137,5 +137,5 @@ public: "Missing/invalid function: bool parse_error(std::size_t, const " "std::string&, const exception&)"); }; -} -} +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/meta/type_traits.hpp b/include/lib/modernjson/detail/meta/type_traits.hpp index e5b6986..2f0af28 100644 --- a/include/lib/modernjson/detail/meta/type_traits.hpp +++ b/include/lib/modernjson/detail/meta/type_traits.hpp @@ -26,6 +26,15 @@ namespace detail // helpers // ///////////// +// Note to maintainers: +// +// Every trait in this file expects a non CV-qualified type. +// The only exceptions are in the 'aliases for detected' section +// (i.e. those of the form: decltype(T::member_function(std::declval()))) +// +// In this case, T has to be properly CV-qualified to constraint the function arguments +// (e.g. to_json(BasicJsonType&, const T&)) + template struct is_basic_json : std::false_type {}; NLOHMANN_BASIC_JSON_TPL_DECLARATION @@ -65,6 +74,55 @@ using to_json_function = decltype(T::to_json(std::declval()...)); template using from_json_function = decltype(T::from_json(std::declval()...)); +template +using get_template_function = decltype(std::declval().template get()); + +// trait checking if JSONSerializer::from_json(json const&, udt&) exists +template +struct has_from_json : std::false_type {}; + +template +struct has_from_json::value>> +{ + using serializer = typename BasicJsonType::template json_serializer; + + static constexpr bool value = + is_detected_exact::value; +}; + +// This trait checks if JSONSerializer::from_json(json const&) exists +// this overload is used for non-default-constructible user-defined-types +template +struct has_non_default_from_json : std::false_type {}; + +template +struct has_non_default_from_json::value>> +{ + using serializer = typename BasicJsonType::template json_serializer; + + static constexpr bool value = + is_detected_exact::value; +}; + +// This trait checks if BasicJsonType::json_serializer::to_json exists +// Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion. +template +struct has_to_json : std::false_type {}; + +template +struct has_to_json::value>> +{ + using serializer = typename BasicJsonType::template json_serializer; + + static constexpr bool value = + is_detected_exact::value; +}; + + /////////////////// // is_ functions // /////////////////// @@ -120,6 +178,30 @@ template struct is_compatible_object_type : is_compatible_object_type_impl {}; +template +struct is_constructible_object_type_impl : std::false_type {}; + +template +struct is_constructible_object_type_impl < + BasicJsonType, ConstructibleObjectType, + enable_if_t::value and + is_detected::value >> +{ + using object_t = typename BasicJsonType::object_t; + + static constexpr bool value = + (std::is_constructible::value and + std::is_same::value) or + (has_from_json::value or + has_non_default_from_json::value); +}; + +template +struct is_constructible_object_type + : is_constructible_object_type_impl {}; + template struct is_compatible_string_type_impl : std::false_type {}; @@ -134,9 +216,28 @@ struct is_compatible_string_type_impl < std::is_constructible::value; }; -template +template struct is_compatible_string_type - : is_compatible_string_type_impl {}; + : is_compatible_string_type_impl {}; + +template +struct is_constructible_string_type_impl : std::false_type {}; + +template +struct is_constructible_string_type_impl < + BasicJsonType, ConstructibleStringType, + enable_if_t::value >> +{ + static constexpr auto value = + std::is_constructible::value; +}; + +template +struct is_constructible_string_type + : is_constructible_string_type_impl {}; template struct is_compatible_array_type_impl : std::false_type {}; @@ -145,18 +246,61 @@ template struct is_compatible_array_type_impl < BasicJsonType, CompatibleArrayType, enable_if_t::value and - is_detected::value >> + is_detected::value and +// This is needed because json_reverse_iterator has a ::iterator type... +// Therefore it is detected as a CompatibleArrayType. +// The real fix would be to have an Iterable concept. + not is_iterator_traits< + std::iterator_traits>::value >> { - // This is needed because json_reverse_iterator has a ::iterator type... - // Therefore it is detected as a CompatibleArrayType. - // The real fix would be to have an Iterable concept. - static constexpr bool value = not is_iterator_traits>::value; + static constexpr bool value = + std::is_constructible::value; }; template struct is_compatible_array_type : is_compatible_array_type_impl {}; +template +struct is_constructible_array_type_impl : std::false_type {}; + +template +struct is_constructible_array_type_impl < + BasicJsonType, ConstructibleArrayType, + enable_if_t::value >> + : std::true_type {}; + +template +struct is_constructible_array_type_impl < + BasicJsonType, ConstructibleArrayType, + enable_if_t::value and + is_detected::value and + is_detected::value and + is_complete_type< + detected_t>::value >> +{ + static constexpr bool value = + // This is needed because json_reverse_iterator has a ::iterator type, + // furthermore, std::back_insert_iterator (and other iterators) have a base class `iterator`... + // Therefore it is detected as a ConstructibleArrayType. + // The real fix would be to have an Iterable concept. + not is_iterator_traits < + std::iterator_traits>::value and + + (std::is_same::value or + has_from_json::value or + has_non_default_from_json < + BasicJsonType, typename ConstructibleArrayType::value_type >::value); +}; + +template +struct is_constructible_array_type + : is_constructible_array_type_impl {}; + template struct is_compatible_integer_type_impl : std::false_type {}; @@ -184,40 +328,6 @@ struct is_compatible_integer_type : is_compatible_integer_type_impl {}; -// trait checking if JSONSerializer::from_json(json const&, udt&) exists -template -struct has_from_json -{ - using serializer = typename BasicJsonType::template json_serializer; - - static constexpr bool value = - is_detected_exact::value; -}; - -// This trait checks if JSONSerializer::from_json(json const&) exists -// this overload is used for non-default-constructible user-defined-types -template -struct has_non_default_from_json -{ - using serializer = typename BasicJsonType::template json_serializer; - - static constexpr bool value = - is_detected_exact::value; -}; - -// This trait checks if BasicJsonType::json_serializer::to_json exists -template -struct has_to_json -{ - using serializer = typename BasicJsonType::template json_serializer; - - static constexpr bool value = - is_detected_exact::value; -}; - template struct is_compatible_type_impl: std::false_type {}; @@ -233,5 +343,5 @@ struct is_compatible_type_impl < template struct is_compatible_type : is_compatible_type_impl {}; -} -} +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/meta/void_t.hpp b/include/lib/modernjson/detail/meta/void_t.hpp index 2528ea8..a256f85 100644 --- a/include/lib/modernjson/detail/meta/void_t.hpp +++ b/include/lib/modernjson/detail/meta/void_t.hpp @@ -9,5 +9,5 @@ template struct make_void using type = void; }; template using void_t = typename make_void::type; -} -} +} // namespace detail +} // namespace nlohmann -- cgit v1.2.3