DjangoJSONEncoder does not aware of UUID

20 views
Skip to first unread message

VladT

unread,
Jan 25, 2019, 8:03:41 AM1/25/19
to Django users
though it looks that it suggested to.
In source for DjangoJSONEncoder we see

class DjangoJSONEncoder(json.JSONEncoder):
    ...
    elif isinstance(o, (decimal.Decimal, uuid.UUID, Promise)):
       
return str(o)
    ...

and it should handle only types that are passed through _value_from_field:

def _value_from_field(self, obj, field):
    value
= field.value_from_object(obj)
   
# Protected types (i.e., primitives like None, numbers, dates,
    # and Decimals) are passed through as is. All other values are
    # converted to string first.
    return value if is_protected_type(value) else field.value_to_string(obj)

But in _PROTECTED_TYPES we don't see uuid.UUID included, so I can't hook and override UUID serialization.

Am I missing something or is it a bug?
Reply all
Reply to author
Forward
0 new messages