Skip to content

Commit 4a0eac7

Browse files
committed
add JSONEncoder for native Sage number types (float reals and integers)
1 parent 813dbbb commit 4a0eac7

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Diff for: plotly/plotly.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,21 @@ def pandasJSONEncoder(self, obj):
176176
except:
177177
pass
178178
return None
179+
def sageJSONEncoder(self, obj):
180+
try:
181+
from sage.all import RR, ZZ
182+
if obj in RR:
183+
return float(obj)
184+
elif obj in ZZ:
185+
return int(obj)
186+
except:
187+
pass
188+
return None
179189
def default(self, obj):
180190
try:
181191
return json.dumps(obj)
182192
except TypeError as e:
183-
encoders = (self.datetimeJSONEncoder, self.numpyJSONEncoder, self.pandasJSONEncoder)
193+
encoders = (self.datetimeJSONEncoder, self.numpyJSONEncoder, self.pandasJSONEncoder, self.sageJSONEncoder)
184194
for encoder in encoders:
185195
s = encoder(obj)
186196
if s is not None:

0 commit comments

Comments
 (0)