Files
jwt-cpp/docs/traits.md
Chris Mc 142d2d38e7 🖊️ Update documentation layout (#202)
* split docs into sub pages

* lets see if we can fix the logo size on doxy

* try usine a theme to make it more modern

* fix bad link

* fix broken link

* Update docs/faqs.md

Co-authored-by: Dominik Thalhammer <thalhammer.d@googlemail.com>

* Update faqs.md

Co-authored-by: Dominik Thalhammer <thalhammer.d@googlemail.com>

* Update Doxyfile

Co-authored-by: Dominik Thalhammer <thalhammer.d@googlemail.com>

* Update README.md

* clean up utc vs localtime FAQ

* fix the table image colors

following https://github.com/jothepro/doxygen-awesome-css#css-variables

* fix colors for class index

* enable graphics diagrams

* fix colors

* fix version number

we missed a patch release

* white space

* fix docs not matching code

* Improve SSL selection docs

Co-authored-by: Dominik Thalhammer <thalhammer.d@googlemail.com>
2022-01-30 16:31:20 -08:00

2.7 KiB

JSON Traits

Traits define the compatability 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.

  • type specifications
  • conversion from generic "value type" to a specific type
  • serialization and parsing

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

⚠️ Not all JSON libraries are a like, you may need to extend certain types such that it can be used. See this provided implemtation.

struct my_favorite_json_library_traits {
    // Type Specifications
    using value_type = json; // The generic "value type" implementation, most libraries have one
    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 integer_type = int64_t; // The "integral type"
    using boolean_type = bool; // The "boolean type"

    // Translation between the implementation notion of type, to the jwt::json::type equivilant
    static jwt::json::type get_type(const value_type &val) {
        using jwt::json::type;

        if (val.type() == json::value_t::object)
            return type::object;
        if (val.type() == json::value_t::array)
            return type::array;
        if (val.type() == json::value_t::string)
            return type::string;
        if (val.type() == json::value_t::number_float)
            return type::number;
        if (val.type() == json::value_t::number_integer)
            return type::integer;
        if (val.type() == json::value_t::boolean)
            return type::boolean;

        throw std::logic_error("invalid type");
    }

    // Conversion from generic value to specific type
    static object_type as_object(const value_type &val);
    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);

    // serilization 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
};