gpt4 book ai didi

python - 是否有一个 python 解码器可以生成有效的 json 并处理 NaN

转载 作者:行者123 更新时间:2023-12-01 02:32:00 28 4
gpt4 key购买 nike

我想将 mongoDb 的输出序列化为 python 中的实际有效 json。我找到了几个库,但它们似乎都不能同时生成有效的 json处理与 null 不兼容的值(未定义也可以)。

输入示例:

{
"_id" : ObjectId("57f3e32c71285f85b46d6fb5"),
"rate" : Infinity
"value" : NaN
}

预期输出:

(尝试了 json.dumps 及其allow_nan选项,但它抛出错误并且根本没有帮助)

{
"_id" : { $oid: "57f3e32c71285f85b46d6fb5" },
"rate" : null,
"value" : null
}

有人知道实现此目的的方法/库吗?

举个例子,Javascript 的 JSON 可以很好地做到这一点:

Object {_id: "57f3e32c71285f85b46d6fb5", rate: Infinity, value: NaN}
JSON.stringify(tg)
"{"_id":"57f3e32c71285f85b46d6fb5","rate":null,"value":null}"

最佳答案

你总是可以制作自己的编码器(我从 https://gist.github.com/pauloalem/6244976 偷了这个)

import json


class FloatEncoder(json.JSONEncoder):
def __init__(self, nan_str="null", **kwargs):
super(FloatEncoder, self).__init__(**kwargs)
self.nan_str = nan_str

def iterencode(self, o, _one_shot=False):
"""Encode the given object and yield each string
representation as available.

For example::

for chunk in JSONEncoder().iterencode(bigobject):
mysocket.write(chunk)
"""
if self.check_circular:
markers = {}
else:
markers = None
if self.ensure_ascii:
_encoder = json.encoder.encode_basestring_ascii
else:
_encoder = json.encoder.encode_basestring
# if self.encoding != 'utf-8':
# def _encoder(o, _orig_encoder=_encoder, _encoding=self.encoding):
# if isinstance(o, str):
# o = o.decode(_encoding)
# return _orig_encoder(o)

def floatstr(o, allow_nan=self.allow_nan, _repr=json.encoder.encode_basestring,
_inf=json.encoder.INFINITY, _neginf=-json.encoder.INFINITY,
nan_str=self.nan_str):
# 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.

if o != o:
text = nan_str
elif o == _inf:
text = 'Infinity'
elif o == _neginf:
text = '-Infinity'
else:
return _repr(o)

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

return text

_iterencode = json.encoder._make_iterencode(
markers, self.default, _encoder, self.indent, floatstr,
self.key_separator, self.item_separator, self.sort_keys,
self.skipkeys, _one_shot)
return _iterencode(o, 0)


d = {"i_will_byte_you_in_the_ass": float("NaN")}
print( json.dumps(d, cls=FloatEncoder) )

关于python - 是否有一个 python 解码器可以生成有效的 json 并处理 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46712813/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com