Written by

Integration Engineer at Cognosante
Question Oliver Wilms · Jun 23, 2024

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

Comments

Enrico Parisi · Jun 24, 2024

Do dynObj.%Set("data",pStream,"stream>base64")

0
Enrico Parisi · Jun 24, 2024

Community AI is on spot!

👏👏👏👏

0
Enrico Parisi  Jun 24, 2024 to Enrico Parisi

Well...I did not read the AI answer with attention, there are a few mistakes in the answer.

Base64Stream should be a stream (typically a binary stream), not a string as in the AI sample, let alone the fact that in the sample code Base64Stream is NOT base64 encoded.

But.....almost correct. 😊

0
Oliver Wilms  Jun 24, 2024 to Enrico Parisi

Enrico, thank you very much for your help!

I use this line of code to get a base64 encoded Stream:

Set pStream = ..lookFile(value)

I use this line to set the stream into the dynamic object:

Do obj.%Set("data",pStream,"stream>base64")

0
Oliver Wilms  Jun 24, 2024 to Enrico Parisi

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.

0
Oliver Wilms  Jun 24, 2024 to Enrico Parisi

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/attachme… 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

0
Enrico Parisi · Jun 24, 2024

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.

0
Oliver Wilms  Jun 24, 2024 to Enrico Parisi

Thank you for clarifying the double encoding I was doing.

I just tested my code with a HTML document. Will it work with %Stream.FileBinary?

0
Enrico Parisi  Jun 24, 2024 to Oliver Wilms

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.

0