Hi,
unfortunately it's not possible to store bytearrays in json-files. If i use the string-representation of an bytearray, e .g (b\xe5V\xb5\x01<: i get (bxe5V\xb5x01<: after loading the file.
If i escape the backslashes (b\\xe5V\\xb5\\x01<: i get the same string (with double-backslashes) after loading the file.
How to get single backslashes or is there any other proper way to store bytearrays in python.
Thank you
Read an bytearray from json
Re: Read an bytearray from json
I don't think that there exists any standard how binary data needs to be encoded in JSON.
An approach I have seen often is base64-encod binary data in JSON strings.
An approach I have seen often is base64-encod binary data in JSON strings.
A few hours of debugging might save you from minutes of reading the documentation!
My repositories: https://github.com/karfas
My repositories: https://github.com/karfas
-
- Posts: 1
- Joined: Tue Oct 06, 2020 2:54 am
- Contact:
Re: Read an bytearray from json
Good!karfas wrote: ↑Thu May 13, 2021 10:13 pmcookie clicker
I don't think that there exists any standard how binary data needs to be encoded in JSON.
An approach I have seen often is base64-encod binary data in JSON strings.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Read an bytearray from json
You can always "cheat" and copy the data to a list:
Not specially efficient, but then JSON never is. But the recipient has to do this explicit conversion. The same drawback applies to using base64, of course.
There are other serialisation options such as pickle.
Code: Select all
b = bytearray(x for x in range(256)) # Test with all possible values
s = ujson.dumps([x for x in b])
z = bytearray(x for x in ujson.loads(s))
There are other serialisation options such as pickle.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.