jsonObject and raw data-type

Posted by DenDuze on 09-Jan-2020 13:46

I have a DataSet with some buffers, 1 of these buffers has a field with datatype RAW.
I write that dataset to a jsonObject with write-json.

When I read that json into another dataset (so with read-json)  the content of that RAW-field is not the same anymore?
Is that expected? Can I do it some other way so that the content of that RAW fields stays the same?

All Replies

Posted by frank.meulblok on 09-Jan-2020 14:45

When a RAW field gets written out to with WRITE-JSON, it gets base64-encoded.

READ-JSON into a known schema will base64-decode the data if a JSON element mnaps to a RAW field.

Following from that, as long as both your datasets have the same schema the RAW field in both datasets should have the same content.

If the schemas don't match, outcomes will vary.

If you are creating a new dynamic dataset and let the READ-JSON generate the schema, you are guaranteed to have a schema mismatch. The parser sees a quoted string, and creates a CHARACTER field for it because it doesn't have anything to go on to make a better decision. (And the JSON format doesn't support any metadata to make it more intelligent either).

Some examples to prove the above:

DEFINE TEMP-TABLE ding NO-UNDO
FIELD ding AS RAW.

DEFINE TEMP-TABLE dong NO-UNDO SERIALIZE-NAME "ding"
FIELD ding AS CHARACTER.

DEFINE VARIABLE lcding AS LONGCHAR NO-UNDO.

CREATE ding.
PUT-INT64(ding.ding,1) = 123467890.

TEMP-TABLE ding:WRITE-JSON("longchar",lcding).
FIND FIRST ding.
DISPLAY length(ding.ding) lcding  VIEW-AS EDITOR LARGE SIZE 50 BY 10 length(lcding,"RAW") WITH FRAME X.

EMPTY TEMP-TABLE ding.
TEMP-TABLE ding:READ-JSON("longchar",lcding).

FIND FIRST ding.
MESSAGE length(ding.ding) GET-INT64(ding,1).

TEMP-TABLE dong:READ-JSON("longchar",lcding).

FIND FIRST dong.
MESSAGE length(dong.ding).

DEFINE VARIABLE hding AS HANDLE NO-UNDO.
CREATE TEMP-TABLE hding.
hding:READ-JSON("longchar",lcding).
hding:DEFAULT-BUFFER-HANDLE:FIND-FIRST().
MESSAGE hding:DEFAULT-BUFFER-HANDLE:BUFFER-FIELD("ding"):DATA-TYPE length(hding:DEFAULT-BUFFER-HANDLE:BUFFER-FIELD("ding"):buffer-value,"RAW").

Posted by DenDuze on 09-Jan-2020 14:55

Yes, that is what I also noticed.

The problem is that the RAW gets converted to a character in the json-write

Do you know about some way to handle this situation?

I have an idea that I'm testing now but I do not really like that solution (but if I have no other ...)

Posted by Peter Judge on 09-Jan-2020 15:10

JSON doesn't have a binary data type, so you will always get some form of character data.
 
As Frank mentions, the simplest way is to make sure that the dataset/temp-tables have the appropriate schema (so a RAW field).
 
If you want to dynamically create the temp-tables, you will have to use a schema (XML or hand-written JSON).
 
-- peter
 

Posted by DenDuze on 09-Jan-2020 15:30

Ok, I'm not happy with that but if it's the only way ... (it destroys the dynamic way of that program)

This thread is closed