diff --git a/docs/traits.md b/docs/traits.md index f0ddeeb..a8971ce 100644 --- a/docs/traits.md +++ b/docs/traits.md @@ -1,10 +1,10 @@ # JSON Traits -Traits define the compatability mapping for JWT-CPP required functionality to the JSON implementation of choice. +Traits define the compatibility mapping for JWT-CPP required functionality to the JSON implementation of choice. ## Providing your own JSON Traits -There are several key items that need to be provided to a `jwt::basic_claim` in order for it to be interoptable with you JSON library of choice. +There are several key items that need to be provided to a `jwt::basic_claim` in order for it to be interoperable with you JSON library of choice. * type specifications * conversion from generic "value type" to a specific type @@ -12,7 +12,7 @@ There are several key items that need to be provided to a `jwt::basic_claim` in If ever you are not sure, the traits are heavily checked against static asserts to make sure you provide everything that's required. -> :warning: Not all JSON libraries are a like, you may need to extend certain types such that it can be used. See this [provided implemtation](https://github.com/Thalhammer/jwt-cpp/blob/e6b92cca0b7088027269c481fa244e5c39df88ff/include/jwt-cpp/traits/danielaparker-jsoncons/traits.h#L18). +> :warning: Not all JSON libraries are a like, you may need to extend certain types such that it can be used. See this [provided implementation](https://github.com/Thalhammer/jwt-cpp/blob/e6b92cca0b7088027269c481fa244e5c39df88ff/include/jwt-cpp/traits/danielaparker-jsoncons/traits.h#L18). ```cpp struct my_favorite_json_library_traits { @@ -21,7 +21,7 @@ struct my_favorite_json_library_traits { using object_type = json::object_t; // The "map type" string to value using array_type = json::array_t; // The "list type" array of values using string_type = std::string; // The "list of chars", must be a narrow char - using number_type = double; // The "percision type" + using number_type = double; // The "precision type" using integer_type = int64_t; // The "integral type" using boolean_type = bool; // The "boolean type" @@ -50,10 +50,10 @@ struct my_favorite_json_library_traits { static array_type as_array(const value_type &val); static string_type as_string(const value_type &val); static number_type as_number(const value_type &val); - static integer_type as_int(const value_type &val); - static boolean_type as_bool(const value_type &val); + static integer_type as_integer(const value_type &val); + static boolean_type as_boolean(const value_type &val); - // serilization and parsing + // serialization and parsing static bool parse(value_type &val, string_type str); static string_type serialize(const value_type &val); // with no extra whitespace, padding or indentation }; diff --git a/include/jwt-cpp/jwt.h b/include/jwt-cpp/jwt.h index 5c622b6..235326e 100644 --- a/include/jwt-cpp/jwt.h +++ b/include/jwt-cpp/jwt.h @@ -1812,9 +1812,6 @@ namespace jwt { #ifdef __cpp_lib_experimental_detect template class _Op, typename... _Args> using is_detected = std::experimental::is_detected<_Op, _Args...>; - - template class _Op, typename... _Args> - using is_detected_t = std::experimental::detected_t<_Op, _Args...>; #else struct nonesuch { nonesuch() = delete; @@ -1840,139 +1837,58 @@ namespace jwt { template class Op, class... Args> using is_detected = typename detector::value; - - template class Op, class... Args> - using is_detected_t = typename detector::type; #endif - template - using get_type_function = decltype(traits_type::get_type); + template + using is_signature = typename std::is_same; - template - using is_get_type_signature = - typename std::is_same, json::type(const value_type&)>; + template class Op, typename Signature> + struct is_function_signature_detected { + using type = Op; + static constexpr auto value = is_detected::value && std::is_function::value && + is_signature::value; + }; template struct supports_get_type { - static constexpr auto value = is_detected::value && - std::is_function>::value && - is_get_type_signature::value; + template + using get_type_t = decltype(T::get_type); + + static constexpr auto value = + is_function_signature_detected::value; + + // Internal assertions for better feedback + static_assert(value, "traits implementation must provide `jwt::json::type get_type(const value_type&)`"); }; - template - using as_object_function = decltype(traits_type::as_object); +#define JWT_CPP_JSON_TYPE_TYPE(TYPE) json_##TYPE_type +#define JWT_CPP_AS_TYPE_T(TYPE) as_##TYPE_t +#define JWT_CPP_SUPPORTS_AS(TYPE) \ + template \ + struct supports_as_##TYPE { \ + template \ + using JWT_CPP_AS_TYPE_T(TYPE) = decltype(T::as_##TYPE); \ + \ + static constexpr auto value = \ + is_function_signature_detected::value; \ + \ + static_assert(value, "traits implementation must provide `" #TYPE "_type as_" #TYPE "(const value_type&)`"); \ + } - template - using is_as_object_signature = - typename std::is_same, object_type(const value_type&)>; + JWT_CPP_SUPPORTS_AS(object); + JWT_CPP_SUPPORTS_AS(array); + JWT_CPP_SUPPORTS_AS(string); + JWT_CPP_SUPPORTS_AS(number); + JWT_CPP_SUPPORTS_AS(integer); + JWT_CPP_SUPPORTS_AS(boolean); - template - struct supports_as_object { - static constexpr auto value = std::is_constructible::value && - is_detected::value && - std::is_function>::value && - is_as_object_signature::value; - }; - - template - using as_array_function = decltype(traits_type::as_array); - - template - using is_as_array_signature = - typename std::is_same, array_type(const value_type&)>; - - template - struct supports_as_array { - static constexpr auto value = std::is_constructible::value && - is_detected::value && - std::is_function>::value && - is_as_array_signature::value; - }; - - template - using as_string_function = decltype(traits_type::as_string); - - template - using is_as_string_signature = - typename std::is_same, string_type(const value_type&)>; - - template - struct supports_as_string { - static constexpr auto value = std::is_constructible::value && - is_detected::value && - std::is_function>::value && - is_as_string_signature::value; - }; - - template - using as_number_function = decltype(traits_type::as_number); - - template - using is_as_number_signature = - typename std::is_same, number_type(const value_type&)>; - - template - struct supports_as_number { - static constexpr auto value = std::is_floating_point::value && - std::is_constructible::value && - is_detected::value && - std::is_function>::value && - is_as_number_signature::value; - }; - - template - using as_integer_function = decltype(traits_type::as_int); - - template - using is_as_integer_signature = - typename std::is_same, integer_type(const value_type&)>; - - template - struct supports_as_integer { - static constexpr auto value = std::is_signed::value && - !std::is_floating_point::value && - std::is_constructible::value && - is_detected::value && - std::is_function>::value && - is_as_integer_signature::value; - }; - - template - using as_boolean_function = decltype(traits_type::as_bool); - - template - using is_as_boolean_signature = - typename std::is_same, boolean_type(const value_type&)>; - - template - struct supports_as_boolean { - static constexpr auto value = std::is_convertible::value && - std::is_constructible::value && - is_detected::value && - std::is_function>::value && - is_as_boolean_signature::value; - }; +#undef JWT_CPP_JSON_TYPE_TYPE +#undef JWT_CPP_AS_TYPE_T +#undef JWT_CPP_SUPPORTS_AS template struct is_valid_traits { - // Internal assertions for better feedback - static_assert(supports_get_type::value, - "traits must provide `jwt::json::type get_type(const value_type&)`"); - static_assert(supports_as_object::value, - "traits must provide `object_type as_object(const value_type&)`"); - static_assert(supports_as_array::value, - "traits must provide `array_type as_array(const value_type&)`"); - static_assert(supports_as_string::value, - "traits must provide `string_type as_string(const value_type&)`"); - static_assert(supports_as_number::value, - "traits must provide `number_type as_number(const value_type&)`"); - static_assert( - supports_as_integer::value, - "traits must provide `integer_type as_int(const value_type&)`"); - static_assert( - supports_as_boolean::value, - "traits must provide `boolean_type as_bool(const value_type&)`"); - static constexpr auto value = supports_get_type::value && supports_as_object::value && @@ -1993,76 +1909,41 @@ namespace jwt { // TODO(prince-chrismc): Stream operators }; - template - using has_mapped_type = typename traits_type::mapped_type; + // https://stackoverflow.com/a/53967057/8480874 + template + struct is_iterable : std::false_type {}; - template - using has_key_type = typename traits_type::key_type; - - template - using has_value_type = typename traits_type::value_type; - - template - using has_iterator = typename object_type::iterator; - - template - using has_const_iterator = typename object_type::const_iterator; - - template - using is_begin_signature = - typename std::is_same().begin()), has_iterator>; - - template - using is_begin_const_signature = - typename std::is_same().begin()), has_const_iterator>; - - template - struct supports_begin { - static constexpr auto value = - is_detected::value && is_detected::value && - is_begin_signature::value && is_begin_const_signature::value; + template + struct is_iterable())), decltype(std::end(std::declval())), +#if __cplusplus > 201402L + decltype(std::cbegin(std::declval())), decltype(std::cend(std::declval())) +#else + decltype(std::begin(std::declval())), + decltype(std::end(std::declval())) +#endif + >> : std::true_type { }; - template - using is_end_signature = - typename std::is_same().end()), has_iterator>; - - template - using is_end_const_signature = - typename std::is_same().end()), has_const_iterator>; - - template - struct supports_end { - static constexpr auto value = - is_detected::value && is_detected::value && - is_end_signature::value && is_end_const_signature::value; - }; +#if __cplusplus > 201703L + template + inline constexpr bool is_iterable_v = is_iterable::value; +#endif template using is_count_signature = typename std::is_integral().count( std::declval()))>; + template + struct is_subcription_operator_signature : std::false_type {}; + template - struct has_subcription_operator { - template - struct sfinae_true : std::true_type {}; - - template - static auto test_operator_plus(int) - -> sfinae_true().operator[](std::declval()))>; - template - static auto test_operator_plus(long) -> std::false_type; - - static constexpr auto value = decltype(test_operator_plus(0)){}; - }; - - template - struct is_subcription_operator_signature { - static constexpr auto has_subscription_operator = has_subcription_operator::value; - static_assert(has_subscription_operator, - "object_type must implementate the subscription operator '[]' for this library"); - - static constexpr auto value = has_subscription_operator; + struct is_subcription_operator_signature< + object_type, string_type, + void_t().operator[](std::declval()))>> : std::true_type { + // TODO(prince-chrismc): I am not convienced this is meaningful anymore + static_assert( + value, + "object_type must implementate the subscription operator '[]' taking string_type as an arguement"); }; template @@ -2072,21 +1953,37 @@ namespace jwt { template struct is_valid_json_object { + template + using mapped_type_t = typename T::mapped_type; + template + using key_type_t = typename T::key_type; + template + using iterator_t = typename T::iterator; + template + using const_iterator_t = typename T::const_iterator; + static constexpr auto value = - is_detected::value && + std::is_constructible::value && + is_detected::value && std::is_same::value && - is_detected::value && + is_detected::value && (std::is_same::value || std::is_constructible::value) && - supports_begin::value && supports_end::value && - is_count_signature::value && - is_subcription_operator_signature::value && + is_detected::value && is_detected::value && + is_iterable::value && is_count_signature::value && + is_subcription_operator_signature::value && is_at_const_signature::value; }; template struct is_valid_json_array { - static constexpr auto value = std::is_same::value; + template + using value_type_t = typename T::value_type; + + static constexpr auto value = std::is_constructible::value && + is_iterable::value && + is_detected::value && + std::is_same::value; }; template @@ -2100,42 +1997,46 @@ namespace jwt { typename std::is_same().substr(std::declval())), string_type>; - template - struct has_operate_plus_method { // https://stackoverflow.com/a/9154394/8480874 - template - struct sfinae_true : std::true_type {}; - - template - static auto test_operator_plus(int) - -> sfinae_true().operator+(std::declval()))>; - template - static auto test_operator_plus(long) -> std::false_type; - - static constexpr auto value = decltype(test_operator_plus(0)){}; - }; - template using is_std_operate_plus_signature = typename std::is_same(), std::declval())), string_type>; - template + template struct is_valid_json_string { static constexpr auto substr = is_substr_start_end_index_signature::value && is_substr_start_index_signature::value; static_assert(substr, "string_type must have a substr method taking only a start index and an overload " "taking a start and end index, both must return a string_type"); - static constexpr auto operator_plus = - has_operate_plus_method::value || is_std_operate_plus_signature::value; + static constexpr auto operator_plus = is_std_operate_plus_signature::value; static_assert(operator_plus, "string_type must have a '+' operator implemented which returns the concatenated string"); - static constexpr auto value = substr && operator_plus; + static constexpr auto value = + std::is_constructible::value && substr && operator_plus; }; - template + template + struct is_valid_json_number { + static constexpr auto value = + std::is_floating_point::value && std::is_constructible::value; + }; + + template + struct is_valid_json_integer { + static constexpr auto value = std::is_signed::value && + !std::is_floating_point::value && + std::is_constructible::value; + }; + template + struct is_valid_json_boolean { + static constexpr auto value = std::is_convertible::value && + std::is_constructible::value; + }; + + template struct is_valid_json_types { // Internal assertions for better feedback static_assert(is_valid_json_value::value, @@ -2145,10 +2046,13 @@ namespace jwt { static_assert(is_valid_json_array::value, "array_type must be a container of value_type"); - static constexpr auto value = is_valid_json_object::value && - is_valid_json_value::value && + static constexpr auto value = is_valid_json_value::value && + is_valid_json_object::value && is_valid_json_array::value && - is_valid_json_string::value; + is_valid_json_string::value && + is_valid_json_number::value && + is_valid_json_integer::value && + is_valid_json_boolean::value; }; } // namespace details @@ -2173,9 +2077,10 @@ namespace jwt { "string_type must be a std::string, convertible to a std::string, or construct a std::string."); static_assert( - details::is_valid_json_types::value, + details::is_valid_json_types::value, "must staisfy json container requirements"); static_assert(details::is_valid_traits::value, "traits must satisfy requirements"); @@ -2243,7 +2148,7 @@ namespace jwt { date as_date() const { using std::chrono::system_clock; if (get_type() == json::type::number) return system_clock::from_time_t(std::round(as_number())); - return system_clock::from_time_t(as_int()); + return system_clock::from_time_t(as_integer()); } /** @@ -2271,14 +2176,14 @@ namespace jwt { * \return content as int * \throw std::bad_cast Content was not an int */ - typename json_traits::integer_type as_int() const { return json_traits::as_int(val); } + typename json_traits::integer_type as_integer() const { return json_traits::as_integer(val); } /** * Get the contained JSON value as a bool * \return content as bool * \throw std::bad_cast Content was not a bool */ - typename json_traits::boolean_type as_bool() const { return json_traits::as_bool(val); } + typename json_traits::boolean_type as_boolean() const { return json_traits::as_boolean(val); } /** * Get the contained JSON value as a number @@ -2982,8 +2887,8 @@ namespace jwt { if (ec) return; const bool matches = [&]() { switch (expected.get_type()) { - case json::type::boolean: return expected.as_bool() == jc.as_bool(); - case json::type::integer: return expected.as_int() == jc.as_int(); + case json::type::boolean: return expected.as_boolean() == jc.as_boolean(); + case json::type::integer: return expected.as_integer() == jc.as_integer(); case json::type::number: return expected.as_number() == jc.as_number(); case json::type::string: return expected.as_string() == jc.as_string(); case json::type::array: diff --git a/include/jwt-cpp/traits/boost-json/traits.h b/include/jwt-cpp/traits/boost-json/traits.h index 9e1160d..3b27d5f 100644 --- a/include/jwt-cpp/traits/boost-json/traits.h +++ b/include/jwt-cpp/traits/boost-json/traits.h @@ -49,7 +49,7 @@ namespace jwt { return string_type{val.get_string()}; } - static integer_type as_int(const value_type& val) { + static integer_type as_integer(const value_type& val) { switch (val.kind()) { case json::kind::int64: return val.get_int64(); case json::kind::uint64: return static_cast(val.get_uint64()); @@ -57,7 +57,7 @@ namespace jwt { } } - static boolean_type as_bool(const value_type& val) { + static boolean_type as_boolean(const value_type& val) { if (val.kind() != json::kind::bool_) throw std::bad_cast(); return val.get_bool(); } diff --git a/include/jwt-cpp/traits/danielaparker-jsoncons/traits.h b/include/jwt-cpp/traits/danielaparker-jsoncons/traits.h index 0aada2e..be56740 100644 --- a/include/jwt-cpp/traits/danielaparker-jsoncons/traits.h +++ b/include/jwt-cpp/traits/danielaparker-jsoncons/traits.h @@ -98,12 +98,12 @@ namespace jwt { return val.as_double(); } - static integer_type as_int(const json& val) { + static integer_type as_integer(const json& val) { if (get_type(val) != jwt::json::type::integer) throw std::bad_cast(); return val.as(); } - static boolean_type as_bool(const json& val) { + static boolean_type as_boolean(const json& val) { if (val.type() != jsoncons::json_type::bool_value) throw std::bad_cast(); return val.as_bool(); } diff --git a/include/jwt-cpp/traits/kazuho-picojson/traits.h b/include/jwt-cpp/traits/kazuho-picojson/traits.h index 179cfde..2f96bfc 100644 --- a/include/jwt-cpp/traits/kazuho-picojson/traits.h +++ b/include/jwt-cpp/traits/kazuho-picojson/traits.h @@ -49,12 +49,12 @@ namespace jwt { return val.get(); } - static int64_t as_int(const picojson::value& val) { + static int64_t as_integer(const picojson::value& val) { if (!val.is()) throw std::bad_cast(); return val.get(); } - static bool as_bool(const picojson::value& val) { + static bool as_boolean(const picojson::value& val) { if (!val.is()) throw std::bad_cast(); return val.get(); } diff --git a/include/jwt-cpp/traits/nlohmann-json/traits.h b/include/jwt-cpp/traits/nlohmann-json/traits.h index 23c2d7d..7cf4869 100644 --- a/include/jwt-cpp/traits/nlohmann-json/traits.h +++ b/include/jwt-cpp/traits/nlohmann-json/traits.h @@ -46,7 +46,7 @@ namespace jwt { return val.get(); } - static int64_t as_int(const json& val) { + static int64_t as_integer(const json& val) { switch (val.type()) { case json::value_t::number_integer: case json::value_t::number_unsigned: return val.get(); @@ -54,7 +54,7 @@ namespace jwt { } } - static bool as_bool(const json& val) { + static bool as_boolean(const json& val) { if (val.type() != json::value_t::boolean) throw std::bad_cast(); return val.get(); } diff --git a/tests/ClaimTest.cpp b/tests/ClaimTest.cpp index 9ad2cc5..e7cccc3 100644 --- a/tests/ClaimTest.cpp +++ b/tests/ClaimTest.cpp @@ -90,7 +90,7 @@ TEST(ClaimTest, SetAlgorithm) { TEST(ClaimTest, AsInt) { jwt::claim c(picojson::value(static_cast(10))); - ASSERT_EQ(c.as_int(), 10); + ASSERT_EQ(c.as_integer(), 10); } TEST(ClaimTest, AsDate) { @@ -101,8 +101,8 @@ TEST(ClaimTest, AsDate) { TEST(ClaimTest, PicoJSONTraitsAccessorsThrow) { jwt::traits::kazuho_picojson::value_type val; ASSERT_THROW(jwt::traits::kazuho_picojson::as_array(val), std::bad_cast); - ASSERT_THROW(jwt::traits::kazuho_picojson::as_bool(val), std::bad_cast); - ASSERT_THROW(jwt::traits::kazuho_picojson::as_int(val), std::bad_cast); + ASSERT_THROW(jwt::traits::kazuho_picojson::as_boolean(val), std::bad_cast); + ASSERT_THROW(jwt::traits::kazuho_picojson::as_integer(val), std::bad_cast); ASSERT_THROW(jwt::traits::kazuho_picojson::as_number(val), std::bad_cast); ASSERT_THROW(jwt::traits::kazuho_picojson::as_object(val), std::bad_cast); ASSERT_THROW(jwt::traits::kazuho_picojson::as_string(val), std::bad_cast); @@ -111,7 +111,7 @@ TEST(ClaimTest, PicoJSONTraitsAccessorsThrow) { TEST(ClaimTest, PicoJSONTraitsAsBool) { jwt::traits::kazuho_picojson::value_type val(true); - ASSERT_EQ(jwt::traits::kazuho_picojson::as_bool(val), true); + ASSERT_EQ(jwt::traits::kazuho_picojson::as_boolean(val), true); ASSERT_EQ(jwt::traits::kazuho_picojson::get_type(val), jwt::json::type::boolean); } @@ -133,9 +133,9 @@ TEST(ClaimTest, MapOfClaim) { ASSERT_TRUE(claims.has_claim("bool")); ASSERT_FALSE(claims.has_claim("__missing__")); - ASSERT_EQ(map::basic_claim_t{claims.get_claim("array").as_array().at(0)}.as_int(), (int)1); + ASSERT_EQ(map::basic_claim_t{claims.get_claim("array").as_array().at(0)}.as_integer(), (int)1); ASSERT_EQ(claims.get_claim("string").as_string(), "hello world"); ASSERT_EQ(claims.get_claim("number").as_number(), 9.9); - ASSERT_EQ(claims.get_claim("bool").as_bool(), true); + ASSERT_EQ(claims.get_claim("bool").as_boolean(), true); ASSERT_THROW(claims.get_claim("__missing__"), jwt::error::claim_not_present_exception); }