Saving and restoring binary data with the Xojo framework JSON

I have binary data saved in a Valentina database as a blob that I’d like to move into and out of a new framework JSON object. I’ve been trying to do this by putting the binary data in a string, setting the encoding of the string (I’ve tried a variety of encodings) and placing it in a xojo.core.dictionary, and then using GenerateJSON/ParseJSON. But I’ve been unable to get the exact original binary data in the retrieved dictionary. I’d like to ask if what I want to do is currently possible or not with the new framework. Thanks.

Doubt it has to do with new framework vs old
JSON doesn’t have a nice way to represent binary data
See json.org
At best hex encode the data

Or use base64

Actually, using JSONItem I could save and restore binary data in strings. The difference between it and the new framework is that in latter JSON only accepts string with defined encodings, and defining the encoding for the string containing binary data as encodings.ascii resulted in an error. Maybe this can be circumvented, but I couldn’t figure it out.

Thank you for suggesting encoding, I’ll do that. And thanks for the base64 tip, Christian.

Strings that hold binary data, particularly control characters should ALWAYS have been problematic.
At least if the old JSON properly followed the JSON spec

JSON.org says

string
    ""
    " chars "

chars
   char
   char chars

char
  any-Unicode-character- except-"-or-\\-or- control-character
  \"
  \\\\
  \\/
  \\b
  \\f
  \

  \\r
  \\t
  \\u four-hex-digits

The JSON spec essentially says it accepts text, not strings, so all string will end up being converted to UTF-8 or (depending on the implementation) UTF-16. Binary data will probably be munged during that process.

The fact that binary data worked in JSONItem should actually be considered an error and would probably cause other JSON implementations to choke.

Norman and Christian’s recommendations are the right ones. When binary data is concerned, do not try to fool it by faking the encoding, encode the data yourself in hex or Base64 first. If you need to do this arbitrarily, set up some kind of a system where such data is converted to and from a JSON object first.

{
  "NormalString" : "Some Text",
  "OtherString : {
    "Type" : "BinaryData",
    "Data" : <Hex- or Base64-encoded data>
  }
}
1 Like

Thank you. Yes, I’ve encoded the binary data now and it works great.