Efficient JSON encoding for data that may be binary, but is often text

Posted by Evgeny on Stack Overflow See other posts from Stack Overflow or by Evgeny
Published on 2010-04-14T00:30:18Z Indexed on 2010/04/14 0:33 UTC
Read the original article Hit count: 362

Filed under:
|
|
|
|

I need to send a JSON packet across the wire with the contents of an arbitrary file. This may be a binary file (like a ZIP file), but most often it will be plain ASCII text.

I'm currently using base64 encoding, which handles all files, but it increases the size of the data significantly - even if the file is ASCII to begin with. Is there a more efficient way I can encode the data, other than manually checking for any non-ASCII characters and then deciding whether or not to base64-encode it?

I'm currently writing this in Python, but will probably need to do the same in Java, C# and C++, so an easily portable solution would be preferable.

© Stack Overflow or respective owner

Related posts about JSON

Related posts about encoding