如何对序列集进行JSON序列化? [英] How to JSON serialize sets?

查看:315
本文介绍了如何对序列集进行JSON序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python set,其中包含具有__hash____eq__方法的对象,以确保该集合中没有重复项.

I have a Python set that contains objects with __hash__ and __eq__ methods in order to make certain no duplicates are included in the collection.

我需要对该结果set进行json编码,但是即使将空的set传递给json.dumps方法也会引发TypeError.

I need to json encode this result set, but passing even an empty set to the json.dumps method raises a TypeError.

  File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 178, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: set([]) is not JSON serializable

我知道我可以为具有自定义default方法的json.JSONEncoder类创建扩展,但是我什至不知道从set转换的起点.是否应该在默认方法中的set值之外创建字典,然后返回该字典的编码?理想情况下,我想使默认方法能够处理原始编码器阻塞的所有数据类型(我将Mongo用作数据源,因此日期似乎也引发了此错误)

I know I can create an extension to the json.JSONEncoder class that has a custom default method, but I'm not even sure where to begin in converting over the set. Should I create a dictionary out of the set values within the default method, and then return the encoding on that? Ideally, I'd like to make the default method able to handle all the datatypes that the original encoder chokes on (I'm using Mongo as a data source so dates seem to raise this error too)

任何朝着正确方向的提示将不胜感激.

Any hint in the right direction would be appreciated.

感谢您的回答!也许我应该更精确一些.

Thanks for the answer! Perhaps I should have been more precise.

我利用(并赞成)这里的答案来解决set被翻译的局限性,但是也存在内部键问题.

I utilized (and upvoted) the answers here to get around the limitations of the set being translated, but there are internal keys that are an issue as well.

set中的对象是转换为__dict__的复杂对象,但它们本身也可以包含其属性值,这些值可能不符合json编码器中的基本类型.

The objects in the set are complex objects that translate to __dict__, but they themselves can also contain values for their properties that could be ineligible for the basic types in the json encoder.

set有很多不同的类型,并且哈希基本上为实体计算了唯一的ID,但是按照NoSQL的真正精神,没有确切说明子对象包含什么.

There's a lot of different types coming into this set, and the hash basically calculates a unique id for the entity, but in the true spirit of NoSQL there's no telling exactly what the child object contains.

一个对象可能包含starts的日期值,而另一个对象可能具有其他模式,该模式不包含包含非原始"对象的键.

One object might contain a date value for starts, whereas another may have some other schema that includes no keys containing "non-primitive" objects.

这就是为什么我唯一能想到的解决方案是扩展JSONEncoder来替换default方法以打开不同的情况-但是我不确定如何解决这个问题,并且文档不明确.在嵌套对象中,从default返回的值是按键传递的,还是只是查看整个对象的通用包含/丢弃?该方法如何容纳嵌套值?我已经看过先前的问题,似乎找不到最佳的针对特定情况的编码的方法(不幸的是,这似乎是我在这里需要做的事情).

That is why the only solution I could think of was to extend the JSONEncoder to replace the default method to turn on different cases - but I'm not sure how to go about this and the documentation is ambiguous. In nested objects, does the value returned from default go by key, or is it just a generic include/discard that looks at the whole object? How does that method accommodate nested values? I've looked through previous questions and can't seem to find the best approach to case-specific encoding (which unfortunately seems like what I'm going to need to do here).

推荐答案

JSON 表示法只有少数几个本机数据类型(对象,数组,字符串,数字,布尔值和null),因此以JSON序列化的任何内容都必须表示为这些类型之一.

JSON notation has only a handful of native datatypes (objects, arrays, strings, numbers, booleans, and null), so anything serialized in JSON needs to be expressed as one of these types.

json模块文档所示,此转换可以通过以下方式自动完成:一个 JSONEncoder JSONDecoder ,但是您将放弃可能需要的其他一些结构(如果将集合转换为列表,则将无法恢复常规列表;如果使用dict.fromkeys(s)将集合转换为字典,则将失去恢复字典的能力.

As shown in the json module docs, this conversion can be done automatically by a JSONEncoder and JSONDecoder, but then you would be giving up some other structure you might need (if you convert sets to a list, then you lose the ability to recover regular lists; if you convert sets to a dictionary using dict.fromkeys(s) then you lose the ability to recover dictionaries).

更复杂的解决方案是构建可与其他本机JSON类型共存的自定义类型.这使您可以存储包括列表,集合,字典,小数,日期时间对象等的嵌套结构:

A more sophisticated solution is to build-out a custom type that can coexist with other native JSON types. This lets you store nested structures that include lists, sets, dicts, decimals, datetime objects, etc.:

from json import dumps, loads, JSONEncoder, JSONDecoder
import pickle

class PythonObjectEncoder(JSONEncoder):
    def default(self, obj):
        if isinstance(obj, (list, dict, str, unicode, int, float, bool, type(None))):
            return JSONEncoder.default(self, obj)
        return {'_python_object': pickle.dumps(obj)}

def as_python_object(dct):
    if '_python_object' in dct:
        return pickle.loads(str(dct['_python_object']))
    return dct

这是一个示例会话,显示它可以处理列表,字典和集合:

Here is a sample session showing that it can handle lists, dicts, and sets:

>>> data = [1,2,3, set(['knights', 'who', 'say', 'ni']), {'key':'value'}, Decimal('3.14')]

>>> j = dumps(data, cls=PythonObjectEncoder)

>>> loads(j, object_hook=as_python_object)
[1, 2, 3, set(['knights', 'say', 'who', 'ni']), {u'key': u'value'}, Decimal('3.14')]

或者,使用更通用的序列化技术(例如 YAML

Alternatively, it may be useful to use a more general purpose serialization technique such as YAML, Twisted Jelly, or Python's pickle module. These each support a much greater range of datatypes.

这篇关于如何对序列集进行JSON序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆