简体   繁体   English

我应该 base64 编码 pickle 发送 over.network/store 到 db 吗?

[英]Should I base64 encode the pickle to send over network/store into db?

Pickle is a way to serialize the python object. Pickle 是一种序列化 python object 的方法。

Suppose I want to send this over the.network or store in a database, then can I send/store (respectively) the pickle value or do I need to base64 encode the pickle value?假设我想通过网络发送或存储在数据库中,那么我可以(分别)发送/存储 pickle 值还是需要 base64 编码 pickle 值?

JSON representations are strings. JSON 表示是字符串。 But even when I have JSON-serialized strings that contain Unicode characters that are not in the printable ASCII character set range, Python chooses a representation of these characters that are in the ASCII-printable range by default ( ensure_ascii=True is the default value for the json.dumps call).但是,即使我的 JSON 序列化字符串包含不在可打印 ASCII 字符集范围内的 Unicode 个字符,Python 默认情况下也会选择这些字符在 ASCII 可打印范围内的表示( ensure_ascii=True是默认值json.dumps调用)。 For example:例如:

print(json.dumps('圣诞节\n\000'));

Prints:印刷:

"\u5723\u8bde\u8282\n\u0000"

There is nothing in the above serialized representation that requires special encoding.上述序列化表示中没有任何内容需要特殊编码。 Now the issue is whether the object that you wish to serialize requires special encoding.现在的问题是您要序列化的 object 是否需要特殊编码。 For example, if the object has an attribute that is of type decimal.Decimal , which is not one of the supported JSON-serializable types, you would want to store the the string representation of this attribute.例如,如果 object 具有decimal.Decimal类型的属性,这不是受支持的 JSON 可序列化类型之一,您可能希望存储此属性的字符串表示形式。

For example:例如:

from decimal import Decimal
import json

class MyClass:
    def __init__(self, a: int, b: Decimal):
        self.a = a
        self.b = b

    def __repr__(self):
        return f'a = {self.a}, b = {self.b}'

    def serialize(self):
        return json.dumps({'a': self.a, 'b': str(self.b)})

    @classmethod
    def deserialize(cls, s):
        state = json.loads(s)
        return cls(state['a'], Decimal(state['b']))

obj1 = MyClass(3, Decimal('7.93'))
print('obj1:', obj1)
saved_state = obj1.serialize()
print('Saved state:', saved_state)
obj2 = MyClass.deserialize(saved_state)
print('obj2:', obj2)

Prints:印刷:

obj1: a = 3, b = 7.93
Saved state: {"a": 3, "b": "7.93"}
obj2: a = 3, b = 7.93

Note笔记

If you named your methods __getstate__ (instead of serialize ) and __setstate__ (instead of deserialize ) and made the latter a regular instance method rather than a class method, then the class would also be compatible with the pickle module, though you need to think about whether you would want to use the standard pickling methodology for when you do not need to send the object across the wire or have it saved in a database or don't need the data-representation stability.如果您将方法命名为__getstate__ (而不是serialize )和__setstate__ (而不是deserialize )并使后者成为常规实例方法而不是 class 方法,那么 class 也将与pickle模块兼容,尽管您需要考虑当您不需要通过网络发送 object 或将其保存在数据库中或不需要数据表示稳定性时,是否要使用标准酸洗方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM