Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1710 from rehevkor5/issue-813
Render infinite values to valid JSON values. Fixes #813
- Loading branch information
Showing
5 changed files
with
118 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import json | ||
|
||
|
||
class FloatEncoder(json.JSONEncoder): | ||
def __init__(self, nan_str="null", **kwargs): | ||
super(FloatEncoder, self).__init__(**kwargs) | ||
self.nan_str = nan_str | ||
|
||
def iterencode(self, o, _one_shot=False): | ||
"""Encode the given object and yield each string | ||
representation as available. | ||
For example:: | ||
for chunk in JSONEncoder().iterencode(bigobject): | ||
mysocket.write(chunk) | ||
""" | ||
if self.check_circular: | ||
markers = {} | ||
else: | ||
markers = None | ||
if self.ensure_ascii: | ||
_encoder = json.encoder.encode_basestring_ascii | ||
else: | ||
_encoder = json.encoder.encode_basestring | ||
if self.encoding != 'utf-8': | ||
def _encoder(o, _orig_encoder=_encoder, _encoding=self.encoding): | ||
if isinstance(o, str): | ||
o = o.decode(_encoding) | ||
return _orig_encoder(o) | ||
|
||
def floatstr(o, allow_nan=self.allow_nan, _repr=json.encoder.FLOAT_REPR, | ||
_inf=json.encoder.INFINITY, _neginf=-json.encoder.INFINITY, | ||
nan_str=self.nan_str): | ||
# Check for specials. Note that this type of test is processor | ||
# and/or platform-specific, so do tests which don't depend on the | ||
# internals. | ||
|
||
if o != o: | ||
text = nan_str | ||
elif o == _inf: | ||
text = '1e9999' | ||
elif o == _neginf: | ||
text = '-1e9999' | ||
else: | ||
return _repr(o) | ||
|
||
if not allow_nan: | ||
raise ValueError( | ||
"Out of range float values are not JSON compliant: " + | ||
repr(o)) | ||
|
||
return text | ||
|
||
_iterencode = json.encoder._make_iterencode( | ||
markers, self.default, _encoder, self.indent, floatstr, | ||
self.key_separator, self.item_separator, self.sort_keys, | ||
self.skipkeys, _one_shot) | ||
return _iterencode(o, 0) |
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
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