I have some binary data produced as base-256 bytestrings in Python (2.x). I need to read these into JavaScript, preserving the ordinal value of each byte (char) in the string. If you'll allow me to mix languages, I want to encode a string s
in Python such that ord(s[i]) == s.charCodeAt(i)
after I've read it back into JavaScript.
The cleanest way to do this seems to be to serialize my Python strings to JSON. However, json.dump
doesn't like my bytestrings, despite fiddling with the ensure_ascii
and encoding
parameters. Is there a way to encode bytestrings to Unicode strings that preserves ordinal character values? Otherwise I think I need to encode the characters above the ASCII range into JSON-style \u1234
escapes; but a codec like this does not seem to be among Python's codecs
.
Is there an easy way to serialize Python bytestrings to JSON, preserving char values, or do I need to write my own encoder?