Exemple #1
0
def custom_iterencode(value):
    from simplejson.encoder import (
        _make_iterencode,
        encode_basestring,
        FLOAT_REPR,
        JSONEncoder,
        PosInf,
    )

    j = JSONEncoder()

    def floatstr(o,
                 allow_nan=j.allow_nan,
                 ignore_nan=j.ignore_nan,
                 _repr=FLOAT_REPR,
                 _inf=PosInf,
                 _neginf=-PosInf):  # pragma: no cover
        if o != o:
            text = 'NaN'
        elif o == _inf:
            text = 'Infinity'
        elif o == _neginf:
            text = '-Infinity'
        else:
            return str(round(o, DEGREE_DECIMAL_PLACES))
        if ignore_nan:
            text = 'null'
        elif not allow_nan:
            raise ValueError(
                "Out of range float values are not JSON compliant: " + repr(o))

        return text

    markers = {}
    _encoder = encode_basestring
    _one_shot = False
    _iterencode = _make_iterencode(
        markers,
        j.default,
        _encoder,
        j.indent,
        floatstr,
        j.key_separator,
        j.item_separator,
        j.sort_keys,
        j.skipkeys,
        _one_shot,
        j.use_decimal,
        j.namedtuple_as_object,
        j.tuple_as_array,
        j.int_as_string_bitcount,
        j.item_sort_key,
        j.encoding,
        j.for_json,
    )

    return _iterencode(value, 0)
Exemple #2
0
def custom_iterencode(value):  # pragma: no cover
    from simplejson.encoder import _make_iterencode, encode_basestring, FLOAT_REPR, JSONEncoder, PosInf

    jenc = JSONEncoder()

    def floatstr(
        o, allow_nan=jenc.allow_nan, ignore_nan=jenc.ignore_nan, _repr=FLOAT_REPR, _inf=PosInf, _neginf=-PosInf
    ):  # pragma: no cover
        if o != o:
            text = "NaN"
        elif o == _inf:
            text = "Infinity"
        elif o == _neginf:
            text = "-Infinity"
        else:
            if type(o) != float:
                # See #118, do not trust custom str/repr
                o = float(o)
            # use str(round()) instead of repr()
            return str(round(o, DEGREE_DECIMAL_PLACES))

        if ignore_nan:
            text = "null"
        elif not allow_nan:
            raise ValueError("Out of range float values are not JSON compliant: " + repr(o))

        return text

    markers = {}
    _encoder = encode_basestring
    _one_shot = False
    _iterencode = _make_iterencode(
        markers,
        jenc.default,
        _encoder,
        jenc.indent,
        floatstr,
        jenc.key_separator,
        jenc.item_separator,
        jenc.sort_keys,
        jenc.skipkeys,
        _one_shot,
        jenc.use_decimal,
        jenc.namedtuple_as_object,
        jenc.tuple_as_array,
        jenc.int_as_string_bitcount,
        jenc.item_sort_key,
        jenc.encoding,
        jenc.for_json,
        jenc.iterable_as_array,
    )

    return _iterencode(value, 0)
Exemple #3
0
def custom_iterencode(value):
    from simplejson.encoder import (
        _make_iterencode,
        encode_basestring,
        FLOAT_REPR,
        JSONEncoder,
        PosInf,
    )

    j = JSONEncoder()

    def floatstr(o, allow_nan=j.allow_nan, ignore_nan=j.ignore_nan,
                 _repr=FLOAT_REPR, _inf=PosInf,
                 _neginf=-PosInf):  # pragma: no cover
        if o != o:
            text = 'NaN'
        elif o == _inf:
            text = 'Infinity'
        elif o == _neginf:
            text = '-Infinity'
        else:
            return str(round(o, DEGREE_DECIMAL_PLACES))
        if ignore_nan:
            text = 'null'
        elif not allow_nan:
            raise ValueError(
                'Out of range float values are not JSON compliant: ' +
                repr(o))

        return text

    markers = {}
    _encoder = encode_basestring
    _one_shot = False
    _iterencode = _make_iterencode(
        markers, j.default, _encoder, j.indent, floatstr,
        j.key_separator, j.item_separator, j.sort_keys,
        j.skipkeys, _one_shot, j.use_decimal,
        j.namedtuple_as_object, j.tuple_as_array,
        j.int_as_string_bitcount, j.item_sort_key,
        j.encoding, j.for_json,
    )

    return _iterencode(value, 0)