使用 jsonpickle 从 python 中的 json 序列化中排除字段

发布于 2024-08-24 00:00:29 字数 204 浏览 11 评论 0原文

我正在使用 jsonpickle 将对象序列化为 json。该对象具有指向其他对象的某些字段。我想有选择地将它们包含在序列化中,以便生成的 json 文件本质上是纯人类可读的文本,没有任何有趣的对象表示。有没有办法让 jsonpickle 在序列化时忽略某些对象字段?或者更一般地说,仅包含“原始”字段或易于序列化的字段,例如字典、整数、字典列表等。

谢谢。

I am using jsonpickle to serialize an object to json. The object has certain fields that point to other objects. I'd like to selectively not include those in the serialization, so that the resulting json file is essentially pure human-readable text without any funny representations of objects. Is there a way to make jsonpickle ignore certain object fields when serialization? Or more generally, include only fields that are "primitive" or easily serializable fields, like dictionaries, ints, lists of dicts, etc.

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-08-31 00:00:29

我认为您可能正在寻找的是不可挑选的参数(请参阅 此文档 了解详细信息)。简而言之,如果此参数设置为 False,jsonpickle 不会将自定义 python 类输出到 JSON。它应该只输出 JSON 本机类型,例如字符串、整数、布尔值和列表。

I think what you might be looking for is the unpicklable argument (see this doc for details). In short, if this argument is set to False, jsonpickle will not output custom python classes to JSON. It should only output JSON native types e.g strings, ints, bools and lists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文