|
| 1 | +#ifndef JWT_CPP_JSONCPP_TRAITS_H |
| 2 | +#define JWT_CPP_JSONCPP_TRAITS_H |
| 3 | + |
| 4 | +#include "jwt-cpp/jwt.h" |
| 5 | +#include "json/json.h" |
| 6 | + |
| 7 | +namespace jwt { |
| 8 | + namespace traits { |
| 9 | + struct open_source_parsers_jsoncpp { |
| 10 | + using value_type = Json::Value; |
| 11 | + using string_type = std::string; |
| 12 | + class array_type : public Json::Value { |
| 13 | + public: |
| 14 | + using value_type = Json::Value; |
| 15 | + |
| 16 | + array_type() = default; |
| 17 | + array_type(const array_type&) = default; |
| 18 | + explicit array_type(const Json::Value& o) : Json::Value(o) {} |
| 19 | + array_type(array_type&&) = default; |
| 20 | + explicit array_type(Json::Value&& o) : Json::Value(o) {} |
| 21 | + template<typename Iterator> |
| 22 | + array_type(Iterator begin, Iterator end) { |
| 23 | + for (Iterator it = begin; it != end; ++it) { |
| 24 | + Json::Value value; |
| 25 | + value = *it; |
| 26 | + this->append(value); |
| 27 | + } |
| 28 | + } |
| 29 | + ~array_type() = default; |
| 30 | + array_type& operator=(const array_type& o) = default; |
| 31 | + array_type& operator=(array_type&& o) noexcept = default; |
| 32 | + }; |
| 33 | + using number_type = double; |
| 34 | + using integer_type = Json::Value::Int; |
| 35 | + using boolean_type = bool; |
| 36 | + class object_type : public Json::Value { |
| 37 | + public: |
| 38 | + using key_type = std::string; |
| 39 | + using mapped_type = Json::Value; |
| 40 | + using size_type = size_t; |
| 41 | + |
| 42 | + object_type() = default; |
| 43 | + object_type(const object_type&) = default; |
| 44 | + explicit object_type(const Json::Value& o) : Json::Value(o) {} |
| 45 | + object_type(object_type&&) = default; |
| 46 | + explicit object_type(Json::Value&& o) : Json::Value(o) {} |
| 47 | + ~object_type() = default; |
| 48 | + object_type& operator=(const object_type& o) = default; |
| 49 | + object_type& operator=(object_type&& o) noexcept = default; |
| 50 | + |
| 51 | + // Add missing C++11 element access |
| 52 | + const mapped_type& at(const key_type& key) const { |
| 53 | + Json::Value const* found = find(key.data(), key.data() + key.length()); |
| 54 | + if (!found) throw std::out_of_range("invalid key"); |
| 55 | + return *found; |
| 56 | + } |
| 57 | + |
| 58 | + size_type count(const key_type& key) const { return this->isMember(key) ? 1 : 0; } |
| 59 | + }; |
| 60 | + |
| 61 | + // Translation between the implementation notion of type, to the jwt::json::type equivilant |
| 62 | + static jwt::json::type get_type(const value_type& val) { |
| 63 | + using jwt::json::type; |
| 64 | + |
| 65 | + if (val.isArray()) |
| 66 | + return type::array; |
| 67 | + else if (val.isString()) |
| 68 | + return type::string; |
| 69 | + else if (val.isNumeric()) |
| 70 | + return type::number; |
| 71 | + else if (val.isInt()) |
| 72 | + return type::integer; |
| 73 | + else if (val.isBool()) |
| 74 | + return type::boolean; |
| 75 | + else if (val.isObject()) |
| 76 | + return type::object; |
| 77 | + |
| 78 | + throw std::logic_error("invalid type"); |
| 79 | + } |
| 80 | + |
| 81 | + static integer_type as_integer(const value_type& val) { |
| 82 | + switch (val.type()) { |
| 83 | + case Json::intValue: return val.asInt64(); |
| 84 | + case Json::uintValue: return static_cast<integer_type>(val.asUInt64()); |
| 85 | + default: throw std::bad_cast(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + static boolean_type as_boolean(const value_type& val) { |
| 90 | + if (!val.isBool()) throw std::bad_cast(); |
| 91 | + return val.asBool(); |
| 92 | + } |
| 93 | + |
| 94 | + static number_type as_number(const value_type& val) { |
| 95 | + if (!val.isNumeric()) throw std::bad_cast(); |
| 96 | + return val.asDouble(); |
| 97 | + } |
| 98 | + |
| 99 | + static string_type as_string(const value_type& val) { |
| 100 | + if (!val.isString()) throw std::bad_cast(); |
| 101 | + return val.asString(); |
| 102 | + } |
| 103 | + |
| 104 | + static object_type as_object(const value_type& val) { |
| 105 | + if (!val.isObject()) throw std::bad_cast(); |
| 106 | + return object_type(val); |
| 107 | + } |
| 108 | + |
| 109 | + static array_type as_array(const value_type& val) { |
| 110 | + if (!val.isArray()) throw std::bad_cast(); |
| 111 | + return array_type(val); |
| 112 | + } |
| 113 | + |
| 114 | + static bool parse(value_type& val, string_type str) { |
| 115 | + Json::Reader reader; |
| 116 | + return reader.parse(str, val); |
| 117 | + } |
| 118 | + |
| 119 | + static string_type serialize(const value_type& val) { |
| 120 | + Json::StreamWriterBuilder builder; |
| 121 | + builder["commentStyle"] = "None"; |
| 122 | + builder["indentation"] = ""; |
| 123 | + std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter()); |
| 124 | + return Json::writeString(builder, val); |
| 125 | + } |
| 126 | + }; |
| 127 | + } // namespace traits |
| 128 | +} // namespace jwt |
| 129 | + |
| 130 | +#endif |
0 commit comments