如何在python中将任意文件类型序列化为json字符串 [英] how to serialize arbitrary file types to json string in python
问题描述
我的服务器将通过套接字将JSON(序列化为字符串)发送到另一台客户端计算机.我将使用我的最终json并执行以下操作:
My server is going to be sending a JSON, serialized as a string, through a socket to another client machine. I'll take my final json and do this:
import json
python_dict_obj = { "id" : 1001, "name" : "something", "file" : <???> }
serialized_json_str = json.dumps(python_dict_obj)
我想让JSON中的字段之一具有作为文件编码为字符串的值.
I'd like to have one of the fields in my JSON have the value that is a file, encoded as a string.
从性能角度(也从互操作性角度出发),使用python编码文件的最佳方法是什么? Base64?二进制?只是原始字符串文本?
Performance-wise (but also interoperability-wise) what is the best way to encode a file using python? Base64? Binary? Just the raw string text?
编辑-对于那些建议base64,是这样的吗?
EDIT - For those suggestion base64, something like this?
# 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'])
推荐答案
我将使用base64
. JSON并非旨在传递二进制数据.因此,除非您的文件内容是纯文本,否则应该"将其编码为使用纯文本.几乎所有内容都可以对base64
进行编码和解码.如果改用(例如)Python的repr(file_content)
,它也会产生纯文本",但是接收端将需要知道如何解码Python的repr()
使用的字符串.
I'd use base64
. JSON isn't designed to communicate binary data. So unless your file's content is vanilla text, it "should be" encoded to use vanilla text. Virtually everything can encode and decode 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中将任意文件类型序列化为json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!