diff --git a/simplejson/encoder.py b/simplejson/encoder.py index ba010af8a92eb563d53e19a7b7d2d9a3a91dffc3_c2ltcGxlanNvbi9lbmNvZGVyLnB5..2078acfdf5bdb4dbab1092c7e658969a490d88eb_c2ltcGxlanNvbi9lbmNvZGVyLnB5 100644 --- a/simplejson/encoder.py +++ b/simplejson/encoder.py @@ -20,10 +20,21 @@ ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,)) def floatstr(o, allow_nan=True): - s = str(o) - # If the first non-sign is a digit then it's not a special value - if (o < 0.0 and s[1].isdigit()) or s[0].isdigit(): - return s - elif not allow_nan: + # 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. + + # assume this produces an infinity on all machines (probably not guaranteed) + INFINITY = 1e66666 + + if o != o: + text = 'NaN' + elif o == INFINITY: + text = 'Infinity' + elif o == -INFINITY: + text = '-Infinity' + else: + return str(o) + + if not allow_nan: raise ValueError("Out of range float values are not JSON compliant: %r" % (o,)) @@ -28,19 +39,8 @@ raise ValueError("Out of range float values are not JSON compliant: %r" % (o,)) - # These are the string representations on the platforms I've tried - if s == 'nan': - return 'NaN' - if s == 'inf': - return 'Infinity' - if s == '-inf': - return '-Infinity' - # NaN should either be inequal to itself, or equal to everything - if o != o or o == 0.0: - return 'NaN' - # Last ditch effort, assume inf - if o < 0: - return '-Infinity' - return 'Infinity' + + return text + def encode_basestring(s): """