Serializing Python bytestrings to JSON, preserving ordinal character values
Posted
by Doctor J
on Stack Overflow
See other posts from Stack Overflow
or by Doctor J
Published on 2010-04-21T02:31:57Z
Indexed on
2010/04/21
2:33 UTC
Read the original article
Hit count: 463
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?
© Stack Overflow or respective owner