Skip to content

Instantly share code, notes, and snippets.

@eloyz
Last active November 6, 2015 16:27
Show Gist options
  • Save eloyz/ef6684c840aa489ee4ba to your computer and use it in GitHub Desktop.
Save eloyz/ef6684c840aa489ee4ba to your computer and use it in GitHub Desktop.
JSON with UUID serialization support
from utils import json
from uuid import uuid4
json.dumps({'id': uuid4()})
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