From 8165707990e4895aa85b3c508d45eb1883411869 Mon Sep 17 00:00:00 2001 From: Guillaume Racicot Date: Thu, 15 Mar 2018 15:35:00 -0400 Subject: [PATCH] basic_json now supports getting many type of strings --- .../nlohmann/detail/conversions/from_json.hpp | 19 +++++++++++++++++++ include/nlohmann/detail/meta.hpp | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index eccc04f1..b066b2b4 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -70,6 +70,25 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) s = *j.template get_ptr(); } +template < + typename BasicJsonType, typename CompatibleStringType, + enable_if_t < + is_compatible_string_type::value and + not std::is_same::value and + std::is_constructible < + BasicJsonType, typename CompatibleStringType::value_type >::value, + int > = 0 > +void from_json(const BasicJsonType& j, CompatibleStringType& s) +{ + if (JSON_UNLIKELY(not j.is_string())) + { + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()))); + } + + s = *j.template get_ptr(); +} + template void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val) { diff --git a/include/nlohmann/detail/meta.hpp b/include/nlohmann/detail/meta.hpp index b251afb6..780efba8 100644 --- a/include/nlohmann/detail/meta.hpp +++ b/include/nlohmann/detail/meta.hpp @@ -120,6 +120,16 @@ struct is_compatible_object_type_impl std::is_constructible::value; }; +template +struct is_compatible_string_type_impl : std::false_type {}; + +template +struct is_compatible_string_type_impl +{ + static constexpr auto value = + std::is_same::value; +}; + template struct is_compatible_object_type { @@ -130,6 +140,15 @@ struct is_compatible_object_type typename BasicJsonType::object_t, CompatibleObjectType >::value; }; +template +struct is_compatible_string_type +{ + static auto constexpr value = is_compatible_string_type_impl < + conjunction>, + has_value_type>::value, + typename BasicJsonType::string_t, CompatibleStringType >::value; +}; + template struct is_basic_json_nested_type {