msgpack/msgpack-python

How to use msgpack str type for serializing Python strings?

danijar opened this issue · 1 comments

It seems that Python strings are always serialized into the msgpack raw type rather than the msgpack str type. Is it possible to change this behavior?

I'm aware that I could store both Python string and bytes objects as msgpack raw type, but then I cannot the two types apart anymore when deserializing (thus unpackb(raw=True/False) is not helpful here).

At the moment, the only workaround seems to be to manually (and recursively) walk the Python structure to replace Python string and bytes objects with more complex structures that carry type information, which adds in quite some extra complexity.

Not reproducible. Please make reproducible report when submitting issue.

$ pip install msgpack
Collecting msgpack
  Downloading msgpack-1.0.4-cp39-cp39-macosx_11_0_arm64.whl (69 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 69.8/69.8 kB 5.7 MB/s eta 0:00:00
Installing collected packages: msgpack
Successfully installed msgpack-1.0.4

$ python3
Python 3.9.6 (default, Oct 18 2022, 12:41:40)
[Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import msgpack
>>> msgpack.packb("str")
b'\xa3str'
>>> msgpack.unpackb(_)
'str'
>>> msgpack.packb(b"bin")
b'\xc4\x03bin'
>>> msgpack.unpackb(_)
b'bin'
>>>