python - Deserialize json/yaml from binary stream which contains other data -
suppose have binary stream stream
, generate follows.
stream.write('lol'.encode()) yaml.dump(some_obj, stream) stream.write('awesome'.encode())
then have write custom parser of sort stream or can recover some_obj
follows.
stream.read(3) recovered = yaml.load(stream) stream.read(7)
if doesn't work yaml serialization, work json serialization?
you cannot want because yaml parser consumes complete stream if dummp explicit end (yaml.dump(some_obj, stream, explicit_end=true)
(which insert ...\n
before awesome
) , doesn't work when writing ---\nawesome
(the document separator). yaml parser consumes word awesome
¹ both when use yaml.load()
when use yaml.load_all()
.
the part front works fine, can consider doing like:
import ruamel.yaml yaml file_name = 'test.comb' some_obj = dict(a = [1, 2], b = {3: 42}) open(file_name, 'w') stream: stream.write('lol'.encode()) yaml.dump(some_obj, stream, explicit_end=true) stream.write('awesome'.encode()) open(file_name) stream: assert stream.read(3) == 'lol' stream_data = '' while true: stream_data += stream.read(1) if stream_data[-4:] == '...\n': break recovered = yaml.load(stream_data) assert stream.read(7) == 'awesome' print(recovered)
which gives (in python2):
{'a': [1, 2], 'b': {3: 42}}
and file contents are:
lola: [1, 2] b: {3: 42} ... awesome
i use similar technique, reading lines for line in stream
, cannot combined normal read()
operations, files have yaml header metadata, followed normal text (non-indented emacs can work on it).
¹ i consider reading past end-of-stream marker (...
) bug in python yaml parser i'll try , fix in next release.