File tree 2 files changed +15
-1
lines changed
2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -35,7 +35,10 @@ def default(self, obj):
35
35
try :
36
36
return super (BetterJSONEncoder , self ).default (obj )
37
37
except TypeError :
38
- return repr (obj )
38
+ try :
39
+ return repr (obj )
40
+ except Exception :
41
+ return object .__repr__ (obj )
39
42
return encoder (obj )
40
43
41
44
Original file line number Diff line number Diff line change @@ -34,6 +34,17 @@ def __repr__(self):
34
34
obj = Unknown ()
35
35
assert json .dumps (obj ) == '"Unknown object"'
36
36
37
+ def test_unknown_type_with_repr_error (self ):
38
+
39
+ class Unknown (object ):
40
+ def __repr__ (self ):
41
+ raise Exception
42
+
43
+ obj = Unknown ()
44
+ s = json .dumps (obj )
45
+ assert isinstance (s , str )
46
+ assert 'Unknown object at 0x' in s
47
+
37
48
def test_decimal (self ):
38
49
d = {'decimal' : Decimal ('123.45' )}
39
50
assert json .dumps (d ) == '{"decimal": "Decimal(\' 123.45\' )"}'
You can’t perform that action at this time.
0 commit comments