Last active
November 6, 2015 16:27
-
-
Save eloyz/ef6684c840aa489ee4ba to your computer and use it in GitHub Desktop.
JSON with UUID serialization support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from utils import json | |
from uuid import uuid4 | |
json.dumps({'id': uuid4()}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from uuid import UUID | |
def json_with_uuid_support(): | |
"""Adds UUID serialization support to native json""" | |
old = json.JSONEncoder.default | |
def fn(self, o): | |
if isinstance(o, UUID): return str(o) | |
return old(self, o) | |
json.JSONEncoder.default = fn | |
return json | |
json = json_with_uuid_support() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment