简体   繁体   English

如何在python中将任意文件类型序列化为json字符串

[英]how to serialize arbitrary file types to json string in python

My server is going to be sending a JSON, serialized as a string, through a socket to another client machine. 我的服务器将发送一个JSON,序列化为字符串,通过套接字发送到另一台客户端机器。 I'll take my final json and do this: 我会拿最后的json做这个:

import json
python_dict_obj = { "id" : 1001, "name" : "something", "file" : <???> }
serialized_json_str = json.dumps(python_dict_obj)

I'd like to have one of the fields in my JSON have the value that is a file, encoded as a string. 我希望我的JSON中的一个字段具有作为文件的值,编码为字符串。

Performance-wise (but also interoperability-wise) what is the best way to encode a file using python? 性能方面(但也是互操作性)使用python编码文件的最佳方法是什么? Base64? Base64编码? Binary? 二进制? Just the raw string text? 只是原始的字符串文本?

EDIT - For those suggestion base64, something like this? 编辑 - 对于那些建议base64,这样的事情?

# get file
import base64
import json

with open(filename, 'r') as f:
    filecontents = f.read()
encoded = base64.b64encode(filecontents)
python_dict_obj['file'] = encoded
serialized_json_str = json.dumps(python_dict_obj)

# ... sent to client via socket

# decrpyting
json_again = json.loads(serialized)
filecontents_again = base64.b64decode(json_again['file'])

I'd use base64 . 我用的是base64 JSON isn't designed to communicate binary data. JSON不是为了传递二进制数据而设计的。 So unless your file's content is vanilla text, it "should be" encoded to use vanilla text. 因此,除非您的文件内容是香草文本​​,否则它应“编码”以使用香草文本。 Virtually everything can encode and decode base64 . 几乎所有东西都可以对base64进行编码和解码。 If you instead use (for example) Python's repr(file_content) , that also produces "plain text", but the receiving end would need to know how to decode the string escapes Python's repr() uses. 如果您改为使用(例如)Python的repr(file_content) ,它也会生成“纯文本”,但接收端需要知道如何解码Python的repr()使用的字符串转义。

JSON cannot handle binary. JSON无法处理二进制文件。 You will need to encode the data as text before serializing, and the easiest to encode it as is Base64. 在序列化之前,您需要将数据编码为文本,最简单的方法是将其编码为Base64。 You do not need to use the URL-safe form of encoding unless there are requirements for it further down the processing chain. 您不需要使用URL安全的编码形式,除非在处理链中有进一步的要求。

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

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