Question
Chris Stewart · Nov 7, 2016

Binary Support in JSON?

I'm working on a REST service which will include a binary stream as a request property. I'm currently encoding the stream as BASE64 and passing it as a string value, but I was wondering if there is anything in the Caché JSON support that may be a better fit for encoding this data? Thanks Chris

1
0 734
Discussion (3)0
Log in or sign up to continue

No. Any direct binary data in json would need to extend the base grammar, see: http://json.org/

That being said, there are initiatives to allow for that, like BSON. Caché doesn't have support for it as of this moment. So your base64 approach actually is very solid!

A follow up question.  I was able to push across a small filestream using my REST services, with the file stream encoded in BASE 64.  I then tried a large file, and received a MAXSTRING error.  I clearly need to use Streams to get round this, and was looking at the class documentation.  I believe there aren't Stream capable versions of the following functions, but I thought I'd ask here before I started coding:

1. Base64Encode (and Decode)

2. toJSON() (and fromJSON)

You are correct in your assumption, you have to work with streams to operate on arbitrarely large JSON. %ToJSON and %FromJSON work with streams. Here is an example how streams can work with %FromJSON to read a file:

        set fileStream = ##class(%Stream.FileCharacter).%New()
        $$$THROWONERROR(tsc,fileStream.LinkToFile(<pFile>))
       
        set jsonObject = ##class(%DynamicObject).%FromJSON(fileStream)