Question
· Jun 23

Long JSON values

I have JSON object which contains file references. I need to replace the file reference with base64 encoded file which is up to 10MB.

I tried the following but it did not work as expected:

do dynObj.%Set("data",pStream.ReadLineIntoStream(.tSC))

Product version: IRIS 2022.3
$ZV: IRIS for Windows (x86-64) 2022.3 (Build 606U) Mon Jan 30 2023 09:08:55 EST
Discussion (9)2
Log in or sign up to continue

Why do you say the stream is typically a binary stream?

This is the method where I get the Stream:

ClassMethod GetStream(pFile As %String = "/tmp/shortObjectFile.txt") As %Stream.Object

{

    Set pStream = ##class(%Stream.FileCharacter).%New()

    Set pStream.Filename = pFile

    Quit pStream

}

The code to produce the encoded file will be in my next response.

Here is my lookFile method which gets the stream for %Set:

ClassMethod lookFile(pFile As %String = "https://jira.devops.myserver.com/my-jira/plugins/servlet/raven/attachmen... 1343.docx", pFileNew As %String = "") As %Stream.Object

{

    Set q = """"

    Set tData = q_pFile_q

    Set tFileOutlog = "/ICS/jira/wlogDATA"

    Set tFileOutput = "/ICS/jira/wgetDATA1343.docx"

    Set tSC = ##class(Oliver.ZF).JiraGet(tData,tFileOutput,tFileOutlog)

    ;

    Set tEncodeFN = ..GetEncodeFilename(pFile)

    Set tEncodePath = "/ICS/jira/"

    Set pEncode = tEncodePath_tEncodeFN

    ;

    Set tEncodedFilename = ##class(Oliver.Base64).B64EncodeWordDoc(tFileOutput,pEncode)

    Set pStream = ..GetStream(pEncode)

    Quit pStream

Why do you say the stream is typically a binary stream?

Because a Charecter Stream may introduce additional character set conversion.
In your case it seems your stream is a MS Word docx document file, that's a binary file (it's a zip file) and no character set conversion is needed nor wanted, you just take the "raw" content (i.e. binary content) and encode it in base64 when included in JSON/Dynamic Object.

Please note that the code:
Do obj.%Set("data",pStream,"stream>base64")

already encode the pStream content to base64! If you pass a base64 encoded stream, you'll end up with double base64 encoding!

Its' my understanding that all you need is to load a docx file into json/dynamiy object, then all you need is:

Set obj = {}
Set pFile = "/ICS/jira/wgetDATA1343.docx"
Set pStream = ##class(%Stream.FileBinary).%OpenId(pFile)
Do obj.%Set("data",pStream,"stream>base64")

The base64 encoding is performed using the "stream>base64" type parameter, here is the class reference documentation for the %Set() method.
This is much more efficient and easier to read.

In my opinion, if you need to encode a file/stream "as is", without any conversion, so that the counterpart receiver get EXACTLY what your source file/stream is/was, then use %Stream.FileBinary.

If you need some character conversion (say, Unicode/UTF8 or others), then use %Stream.FileCharacter (with appropriate parameters...) that can handle the conversion.