mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-02 01:48:13 +08:00
improve handling of non-finite floating point values (#490)
* improve HTTP error reporting * generate standards-compatible JSON * add --nan-to-num work-around for non-finite floating point values * lint * update tests * correctly set Infinities to min/max * REAMDE update for --nan-to-num * define constant for repetitive warning message * clarify where NaN errors will occure
This commit is contained in:
@@ -7,6 +7,17 @@ from server.app.util.errors import MimeTypeError
|
||||
|
||||
|
||||
class Float32JSONEncoder(json.JSONEncoder):
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""
|
||||
NaN/Infinities are illegal in standard JSON. Python extends JSON with
|
||||
non-standard symbols that most JavaScript JSON parsers do not understand.
|
||||
The `allow_nan` parameter will force Python simplejson to throw an ValueError
|
||||
if it runs into non-finite floating point values which are unsupported by
|
||||
standard JSON.
|
||||
"""
|
||||
kwargs['allow_nan'] = False
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def default(self, obj):
|
||||
if isinstance(obj, float32):
|
||||
return float(obj)
|
||||
|
||||
Reference in New Issue
Block a user