Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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 4 commits into from
Mar 11, 2015
Merged

Override JSONEncoder.iterencode #203

merged 4 commits into from
Mar 11, 2015

Conversation

theengineear
Copy link
Contributor

We do this to handle NaN, Inf, and -Inf

Fixes: #202

Todo:

  • convert these types to 'null'
  • update broken tests (that have NaN, Inf, etc)
  • write new core test to specify this functionality

We do this to handle NaN, Inf, and -Inf
@theengineear
Copy link
Contributor Author

@chriddyp , this one looked like a quickie, so I just grabbed it.

# we want stricter JSON, so convert NaN, Inf, -Inf --> 'null'
nan_str = inf_str = neg_inf_str = 'null'

# uses code from official python json.encoder module. Same licence applies.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woah gnarly

Copy link
Member

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#L224

Copy link
Contributor Author

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:

import json
import numpy

class Foo(object):
    pass

class NullEncoder(json.JSONEncoder):
    def encode_as_true(self, obj):
        return None

    def default(self, obj):
        return self.encode_as_true(obj)

json.dumps(Foo(), cls=NullEncoder)  # here, it works
# 'null'
json.dumps(numpy.ma.core.masked, cls=NullEncoder)  # and again here
# 'null'
json.dumps(float('NaN'), cls=NullEncoder)  # no shot! this doesn't work as expected!
# 'NaN'

Copy link
Member

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!

Python uses extended JSON which allows for NaN, Infinity, -Infinity.
This means that a `JSON.parse()` on one of our encoded JSON strings will
fail. This is no longer the case.
@theengineear
Copy link
Contributor Author

@chriddyp , i'm ready to roll this out when tests pass. nice catch on this!

@chriddyp
Copy link
Member

boom!

Sent from my iPhone

On Mar 11, 2015, at 3:40 AM, Andrew [email protected] wrote:

@chriddyp , i'm ready to roll this out when tests pass. nice catch on this!


Reply to this email directly or view it on GitHub.

theengineear added a commit that referenced this pull request Mar 11, 2015
Override `JSONEncoder.iterencode`
@theengineear theengineear merged commit c4a38a4 into master Mar 11, 2015
@theengineear theengineear deleted the encode-nan-inf branch March 11, 2015 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

json encoder should handle NaN and Infinity
2 participants