Created
July 14, 2014 07:55
-
-
Save georgexsh/2a5eb64ac0541f433cd1 to your computer and use it in GitHub Desktop.
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
commit d264be90f3605f19e903e96e25c8d1d7c15969ca | |
Author: Xie Shi <[email protected]> | |
Date: Mon Jul 14 13:50:38 2014 +0800 | |
convert object w/ an unknown type into string repr | |
ref: https://github.com/getsentry/raven-python/issues/474 | |
diff --git a/raven/utils/json.py b/raven/utils/json.py | |
index 654f019..901e9d2 100644 | |
--- a/raven/utils/json.py | |
+++ b/raven/utils/json.py | |
@@ -32,7 +32,8 @@ class BetterJSONEncoder(json.JSONEncoder): | |
return list(obj) | |
elif isinstance(obj, bytes): | |
return obj.decode('utf-8', errors='replace') | |
- return super(BetterJSONEncoder, self).default(obj) | |
+ else: | |
+ return repr(obj) | |
def better_decoder(data): | |
diff --git a/tests/utils/json/tests.py b/tests/utils/json/tests.py | |
index 9715033..9a1a63e 100644 | |
--- a/tests/utils/json/tests.py | |
+++ b/tests/utils/json/tests.py | |
@@ -8,6 +8,7 @@ from raven.utils import json | |
class JSONTest(TestCase): | |
+ | |
def test_uuid(self): | |
res = uuid.uuid4() | |
json.dumps(res) == '"%s"' % res.hex | |
@@ -23,3 +24,12 @@ class JSONTest(TestCase): | |
def test_frozenset(self): | |
res = frozenset(['foo', 'bar']) | |
assert json.dumps(res) in ('["foo", "bar"]', '["bar", "foo"]') | |
+ | |
+ def test_unknown_type(self): | |
+ | |
+ class Unknown(object): | |
+ def __repr__(self): | |
+ return 'Unknown object' | |
+ | |
+ obj = Unknown() | |
+ assert json.dumps(obj) == '"Unknown object"' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment