Skip to content

Commit fb8fe65

Browse files
Refactor traits validation to reduce code bloat (#261)
Co-authored-by: Dominik Thalhammer <dominik@thalhammer.it>
1 parent 03ad09b commit fb8fe65

File tree

7 files changed

+147
-242
lines changed

7 files changed

+147
-242
lines changed

docs/traits.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# JSON Traits
22

3-
Traits define the compatability mapping for JWT-CPP required functionality to the JSON implementation of choice.
3+
Traits define the compatibility mapping for JWT-CPP required functionality to the JSON implementation of choice.
44

55
## Providing your own JSON Traits
66

7-
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.
7+
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.
88

99
* type specifications
1010
* conversion from generic "value type" to a specific type
1111
* serialization and parsing
1212

1313
If ever you are not sure, the traits are heavily checked against static asserts to make sure you provide everything that's required.
1414

15-
> :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).
15+
> :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).
1616
1717
```cpp
1818
struct my_favorite_json_library_traits {
@@ -21,7 +21,7 @@ struct my_favorite_json_library_traits {
2121
using object_type = json::object_t; // The "map type" string to value
2222
using array_type = json::array_t; // The "list type" array of values
2323
using string_type = std::string; // The "list of chars", must be a narrow char
24-
using number_type = double; // The "percision type"
24+
using number_type = double; // The "precision type"
2525
using integer_type = int64_t; // The "integral type"
2626
using boolean_type = bool; // The "boolean type"
2727

@@ -50,10 +50,10 @@ struct my_favorite_json_library_traits {
5050
static array_type as_array(const value_type &val);
5151
static string_type as_string(const value_type &val);
5252
static number_type as_number(const value_type &val);
53-
static integer_type as_int(const value_type &val);
54-
static boolean_type as_bool(const value_type &val);
53+
static integer_type as_integer(const value_type &val);
54+
static boolean_type as_boolean(const value_type &val);
5555

56-
// serilization and parsing
56+
// serialization and parsing
5757
static bool parse(value_type &val, string_type str);
5858
static string_type serialize(const value_type &val); // with no extra whitespace, padding or indentation
5959
};

include/jwt-cpp/jwt.h

Lines changed: 126 additions & 221 deletions
Large diffs are not rendered by default.

include/jwt-cpp/traits/boost-json/traits.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ namespace jwt {
4949
return string_type{val.get_string()};
5050
}
5151

52-
static integer_type as_int(const value_type& val) {
52+
static integer_type as_integer(const value_type& val) {
5353
switch (val.kind()) {
5454
case json::kind::int64: return val.get_int64();
5555
case json::kind::uint64: return static_cast<int64_t>(val.get_uint64());
5656
default: throw std::bad_cast();
5757
}
5858
}
5959

60-
static boolean_type as_bool(const value_type& val) {
60+
static boolean_type as_boolean(const value_type& val) {
6161
if (val.kind() != json::kind::bool_) throw std::bad_cast();
6262
return val.get_bool();
6363
}

include/jwt-cpp/traits/danielaparker-jsoncons/traits.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ namespace jwt {
9898
return val.as_double();
9999
}
100100

101-
static integer_type as_int(const json& val) {
101+
static integer_type as_integer(const json& val) {
102102
if (get_type(val) != jwt::json::type::integer) throw std::bad_cast();
103103
return val.as<integer_type>();
104104
}
105105

106-
static boolean_type as_bool(const json& val) {
106+
static boolean_type as_boolean(const json& val) {
107107
if (val.type() != jsoncons::json_type::bool_value) throw std::bad_cast();
108108
return val.as_bool();
109109
}

include/jwt-cpp/traits/kazuho-picojson/traits.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ namespace jwt {
4949
return val.get<picojson::array>();
5050
}
5151

52-
static int64_t as_int(const picojson::value& val) {
52+
static int64_t as_integer(const picojson::value& val) {
5353
if (!val.is<int64_t>()) throw std::bad_cast();
5454
return val.get<int64_t>();
5555
}
5656

57-
static bool as_bool(const picojson::value& val) {
57+
static bool as_boolean(const picojson::value& val) {
5858
if (!val.is<bool>()) throw std::bad_cast();
5959
return val.get<bool>();
6060
}

include/jwt-cpp/traits/nlohmann-json/traits.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ namespace jwt {
4646
return val.get<json::array_t>();
4747
}
4848

49-
static int64_t as_int(const json& val) {
49+
static int64_t as_integer(const json& val) {
5050
switch (val.type()) {
5151
case json::value_t::number_integer:
5252
case json::value_t::number_unsigned: return val.get<int64_t>();
5353
default: throw std::bad_cast();
5454
}
5555
}
5656

57-
static bool as_bool(const json& val) {
57+
static bool as_boolean(const json& val) {
5858
if (val.type() != json::value_t::boolean) throw std::bad_cast();
5959
return val.get<bool>();
6060
}

tests/ClaimTest.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ TEST(ClaimTest, SetAlgorithm) {
9090

9191
TEST(ClaimTest, AsInt) {
9292
jwt::claim c(picojson::value(static_cast<int64_t>(10)));
93-
ASSERT_EQ(c.as_int(), 10);
93+
ASSERT_EQ(c.as_integer(), 10);
9494
}
9595

9696
TEST(ClaimTest, AsDate) {
@@ -101,8 +101,8 @@ TEST(ClaimTest, AsDate) {
101101
TEST(ClaimTest, PicoJSONTraitsAccessorsThrow) {
102102
jwt::traits::kazuho_picojson::value_type val;
103103
ASSERT_THROW(jwt::traits::kazuho_picojson::as_array(val), std::bad_cast);
104-
ASSERT_THROW(jwt::traits::kazuho_picojson::as_bool(val), std::bad_cast);
105-
ASSERT_THROW(jwt::traits::kazuho_picojson::as_int(val), std::bad_cast);
104+
ASSERT_THROW(jwt::traits::kazuho_picojson::as_boolean(val), std::bad_cast);
105+
ASSERT_THROW(jwt::traits::kazuho_picojson::as_integer(val), std::bad_cast);
106106
ASSERT_THROW(jwt::traits::kazuho_picojson::as_number(val), std::bad_cast);
107107
ASSERT_THROW(jwt::traits::kazuho_picojson::as_object(val), std::bad_cast);
108108
ASSERT_THROW(jwt::traits::kazuho_picojson::as_string(val), std::bad_cast);
@@ -111,7 +111,7 @@ TEST(ClaimTest, PicoJSONTraitsAccessorsThrow) {
111111

112112
TEST(ClaimTest, PicoJSONTraitsAsBool) {
113113
jwt::traits::kazuho_picojson::value_type val(true);
114-
ASSERT_EQ(jwt::traits::kazuho_picojson::as_bool(val), true);
114+
ASSERT_EQ(jwt::traits::kazuho_picojson::as_boolean(val), true);
115115
ASSERT_EQ(jwt::traits::kazuho_picojson::get_type(val), jwt::json::type::boolean);
116116
}
117117

@@ -133,9 +133,9 @@ TEST(ClaimTest, MapOfClaim) {
133133
ASSERT_TRUE(claims.has_claim("bool"));
134134
ASSERT_FALSE(claims.has_claim("__missing__"));
135135

136-
ASSERT_EQ(map::basic_claim_t{claims.get_claim("array").as_array().at(0)}.as_int(), (int)1);
136+
ASSERT_EQ(map::basic_claim_t{claims.get_claim("array").as_array().at(0)}.as_integer(), (int)1);
137137
ASSERT_EQ(claims.get_claim("string").as_string(), "hello world");
138138
ASSERT_EQ(claims.get_claim("number").as_number(), 9.9);
139-
ASSERT_EQ(claims.get_claim("bool").as_bool(), true);
139+
ASSERT_EQ(claims.get_claim("bool").as_boolean(), true);
140140
ASSERT_THROW(claims.get_claim("__missing__"), jwt::error::claim_not_present_exception);
141141
}

0 commit comments

Comments
 (0)