Created
January 17, 2014 21:21
-
-
Save coderanger/8481817 to your computer and use it in GitHub Desktop.
For when you absolutely, positively must get something that looks like JSON.
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
lol