indeedeng / django-ptrack

Tracking pixel library for Django
Apache License 2.0
40 stars 17 forks source link

handle uuid via json encoder #15

Open thenewguy opened 3 years ago

thenewguy commented 3 years ago

It would be helpful if the default encoder used the DjangoJsonEncoder class for the JSON conversion to automatically handle types like uuid.

This is what happens when you pass a UUID pk through ptrack:

   File "/home/vagrant/.tox/py/lib/python3.7/site-packages/ptrack/templatetags/ptrack.py", line 20, in ptrack
     encoded_dict = {'ptrack_encoded_data': ptrack_encoder.encrypt(*args, **kwargs)}
   File "/home/vagrant/.tox/py/lib/python3.7/site-packages/ptrack/encoder.py", line 46, in encrypt
     data = json.dumps((args, kwargs))
   File "/usr/local/lib/python3.7/json/__init__.py", line 231, in dumps
     return _default_encoder.encode(obj)
   File "/usr/local/lib/python3.7/json/encoder.py", line 199, in encode
     chunks = self.iterencode(o, _one_shot=True)
   File "/usr/local/lib/python3.7/json/encoder.py", line 257, in iterencode
     return _iterencode(o, 0)
   File "/usr/local/lib/python3.7/json/encoder.py", line 179, in default
     raise TypeError(f'Object of type {o.__class__.__name__} '
 TypeError: Object of type UUID is not JSON serializable

It would be a small change to this line: https://github.com/indeedeng/django-ptrack/blob/c3b164b9d1860325cdab4ee5bc9b689d0f5ea3ff/ptrack/encoder.py#L46