Skip to content

Instantly share code, notes, and snippets.

@coderanger
Created January 17, 2014 21:21
Show Gist options
  • Save coderanger/8481817 to your computer and use it in GitHub Desktop.
Save coderanger/8481817 to your computer and use it in GitHub Desktop.
For when you absolutely, positively must get something that looks like JSON.
MEMORY = '<memory>'
class FuckItEncoder(json.JSONEncoder):
def default(self, obj):
try:
return json.JSONEncoder.default(self, obj)
except RuntimeError:
return '<recur>'
except MemoryError:
return MEMORY
except TypeError:
if hasattr(obj, '__dict__'):
return obj.__dict__
else:
return repr(obj)
json.dump(config, open('config.json', 'wb'), cls=FuckItEncoder, indent=2)
@mahmoudimus
Copy link

lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment