Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

/ui2/cl_json and abap_true

yuriy_dzhenyeyev
Explorer

Dear All,

here's a short report. It works fine for the char20 value, but cannot deserialize an abap_true value.

The results are respectively:

...which is OK.

and

...as of /ui2/cl_json PL 16.

Is it a bug or am I missing something?

Thank you!

REPORT zsandbox5.
START-OF-SELECTION.
* DATA source TYPE abap_bool VALUE abap_true.
DATA source TYPE char20 VALUE 'This is a test'.
WRITE: / 'Source:', / source.
DATA(json) = /ui2/cl_json=>serialize( data = source ).
WRITE: / 'Serialized:', / json.
DATA(c) = /ui2/cl_json=>generate( json = json ).
DATA result LIKE source.
/ui2/cl_json=>deserialize(
EXPORTING
json = json " JSON string
CHANGING
data = result " Data to serialize
).
WRITE: / 'Deserialized:', / result.

1 ACCEPTED SOLUTION

0 Kudos

Hi Yuriy,

Need to pass value true with double quotes as "true" to the deserialize method.


13 REPLIES 13

yuriy_dzhenyeyev
Explorer

...ok, looks like a bug in DESERIALIZE_INT:

abap_true is serialized as true. There are no quote marks, so this is all skipped within while_offset_not_cs macro.

Yes, it seems that it is a use-case not handled.

I don't think it hinders any practical use-cases though. JSON strings are either objects/arrays most of the time. TBH I didn't even know that a single bool value/literal is a valid JSON, but according to the newest specification it is.

https://stackoverflow.com/questions/39352207/are-booleans-valid-json

0 Kudos

You can mark your own answer as Accepted. It's better to not confuse visitors with a non-relevant answer marked as accepted 😉

I think JSON has not changed since the beginning. Each line is a valid JSON:

true
false
null
1
"text"
{}
{"property1":1,"property2":2}
[]
[1,2] {"property":{"property":[1,"text",null,true,false]}}

etc.

0 Kudos

...well, consequently, JSON

1

is not deserialized either.

0 Kudos

sandra.rossi

Older JSON specifications defined only object/array as a valid root element, as described in the StackOverflow post I linked.

0 Kudos

gabmarian Got it now, thanks for notifying me. I didn't think that such important changes could happen in a standard.

0 Kudos

Hi Yuriy,

Need to pass value true with double quotes as "true" to the deserialize method.


"true" and true has a different semantic meaning. A simple boolean value without quotation marks is a valid JSON as well.

alexey_arseniev
Advisor
Advisor
0 Kudos

Hi Yuriy,

/ui2/cl_json=>deserialize only accepts JSON objects, arrays or strings as an input. This is a reason for checks you see.

It is done, to block you from passing to it any trash expecting it to be processed. Each additional check for JSON consistency will cost performance, so that was a tradeoff.

/ui2/cl_json is not a "standard" and "official" serializer/deserializer for JSON in ABAP, but a convenience class I wrote and supporting as a hobby project. Though it is not a standard, it is a most comprehensive tool for working with JSON in ABAP.

BR, Alexey.

0 Kudos

alexey.arseniev great job!

My appeal was actually not to introduce more checks, but to allow primitive values to be processed - like true or 5 or 3.141592 (the list should be complete now). It is already done for string objects. For the mentioned cases, the chain

A == (serialize) ==> B == (deserialize) ==> A

breaks, and that is confusing.

As this is your hobby project, it would be great if you consider this post as a feature request.

BR, Yuriy

alexey_arseniev
Advisor
Advisor

Hi Yuriy,

I have extended the check to accept also booleans and numbers. The fix will be available in the next patch note.

Till then, you can work with a local copy. The correction will be to change "while_offset_not_cs `"{[` unescaped" with "while_offset_not_cs `"{[aeflnrstu0123456789+-eE.` unescaped" in DESERIALIZE_INT and GENERATE.

BR, Alexey.

alexey.arseniev many thanks, Looking forward to give it a try!