python-jose icon indicating copy to clipboard operation
python-jose copied to clipboard

Allow custom JSON encoder for converting claims dict

Open chbndrhnns opened this issue 3 years ago • 4 comments

I am using this snippet to allow a custom JSON encoder to be used (which is patching parts of python-jose):

from jose import jws, jwt
from jose.utils import base64url_encode
from my_encoder import MyEncoder

def _encode_with_my_encoder(payload):
    """Encode the payload with custom JSON encoder which knows how
    to handle the custom types
    """
    if isinstance(payload, Mapping):
        try:
            payload = json.dumps(
                payload,
                separators=(',', ':'),
                cls=MyEncoder
            ).encode('utf-8')
        except ValueError:
            pass

    return base64url_encode(payload)


jws._encode_payload = _encode_with_juice_encoder

Would it be feasible to support passing in a custom encoder class into the encode method?

chbndrhnns avatar Mar 03 '21 17:03 chbndrhnns

Is this kind of change unwanted? I would love some feedback.

chbndrhnns avatar Sep 12 '22 08:09 chbndrhnns

I need it

MarkParker5 avatar Apr 13 '23 00:04 MarkParker5

IMO would also be good to add the option to jwt.encode()

Leithal3 avatar Nov 08 '23 01:11 Leithal3