-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Override JSONEncoder.iterencode
#203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f5df133
Override `JSONEncoder.iterencode`
theengineear c54f643
Expect Infinity/NaN —> null in tests now.
theengineear d459929
Add a core_test for this functionality.
theengineear 9af744b
version bump —> 1.6.11 (NaN/Inf —> null in JSON)
theengineear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,3 @@ | ||
def setup_package(): | ||
import warnings | ||
warnings.filterwarnings('ignore') |
This file contains hidden or 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,13 @@ | ||
import json | ||
from unittest import TestCase | ||
|
||
from plotly.utils import PlotlyJSONEncoder | ||
|
||
|
||
class TestJSONEncoder(TestCase): | ||
|
||
def test_nan_to_null(self): | ||
array = [1, float('NaN'), float('Inf'), float('-Inf'), 'platypus'] | ||
result = json.dumps(array, cls=PlotlyJSONEncoder) | ||
expected_result = '[1, null, null, null, "platypus"]' | ||
self.assertEqual(result, expected_result) |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '1.6.10' | ||
__version__ = '1.6.11' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
woah gnarly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this may be naive, but can't we just extend our current encoder with a check for is
NaN
,Inf
? like how we do with datetimes https://github.com/plotly/python-api/blob/master/plotly/utils.py#L224There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
situation kinda stinks, I actually documented it because this same thing was confusing to me as well:
https://github.com/plotly/python-api/blob/master/plotly/utils.py#L135-150
We only get a crack at stuff if the JSONEncoder can't encode it. What's more, as is mentioned in the SO post, when
allow_nan=False
, we also don't get a crack at it in our encoder. bummer!Here's an example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow, that's pretty wild. well, that looks good then, nice diggin!