Application Development and Automation 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: 
Read only

Need Help ABAP to JSON Boolean & null Conversion using /UI2/CL_JSON

14,865

How do i convert a boolean & null value from abap to json using /UI2/CL_JSON class.

DUMMY_TYPE is a method available in the class but unable to use it.

Please help with this conversion.

9 REPLIES 9
Read only

Sandra_Rossi
Active Contributor
11,414

Maybe alexey.arseniev can help you on this question.

For a boolean, I guess it should be automatic if the ABAP component has the data element FLAG or type ABAP_BOOL. But for NULL, I don't know (because null doesn't exist in ABAP).

Read only

alexey_arseniev
Product and Topic Expert
Product and Topic Expert
11,414

Hi

/ui2/cl_json supports native conversion into JSON boolean or null reference.

To get value serialized into boolean, use on ABAP side ABAP_BOOL type.

For null reference, use initial REF TO ...

There is no DUMMY_TYPE, but DUMP_TYPE. The method is protected, because is not intended to be called directly, but only by extending classes.

For more information please see the blog https://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer

and ask question directly there.

BR, Alexey-

Read only

Former Member
0 Likes
11,414

There is no ABAP_BOOL type in the data dictionary so you can't use it in DD stuctures.

It's unfortunate the developers of /ui2/cl_json did not know about ABAP_BOOL which defines false as ' ' and undefined as '-'. The implementation of /ui2/cl_json does exactly the opposite and serializes '' to null and '-' to false.

Anyone have a DD-compatible solution?

Read only

0 Likes
11,414

Do we have any solution to have Boolean values transformed using DD-compatible solution.
The Json that I have is a very large structure and have atleast 8 sublevels with multiple arrays and objects so having to create the same one locally is a pain and could cause issues with the key names.

Read only

11,414

Since I myself was brought to this post by Google because I faced the same problem: the class /ui2/cl_json does actually have the solution. Maybe it was changed, but the class differentiates between boolean (X for true and blank for false) and triboolean (x for true, - for false, and blank for undefined) types. A data type from the DDIC it recognizes correctly is e.g. XSDBOOLEAN, meaning if you give the serialize method a structure containing a component with data type xsdboolean and fill it with abap_true or abap_false it will correctly map to true or false in the JSON string.

Read only

0 Likes
10,548

Thanks for this post, came from google too! Using type XSDBOOLEAN solved my issue too (all other abap types were ignored and left it as string), and 'X' or '' turned correctly into true or false

Thanks!

Read only

former_member750684
Discoverer
0 Likes
11,414

Use the Data Element as BOOLEAN and override the Key name with the name_mappings parameter and both these should work fine

Read only

vladimir_culic
Explorer
0 Likes
11,414

It is old question, but I needed the answer now. I copied /ui2/cl_json to zcl_json and in

Loc. Definitions/Implementations

elseif mc_bool_3state cs &2->absolute_name and data is initial.
"to
elseif mc_bool_3state cs &2->absolute_name and &1 = `-`.

Be aware that data type used for boolean need to be either "BOOLEAN" not abap_bool

  class-data MC_BOOL_TYPES type STRING read-only value `\TYPE-POOL=ABAP\TYPE=ABAP_BOOL\TYPE=BOOLEAN\TYPE=BOOLE_D\TYPE=XFELD` ##NO_TEXT.
class-data MC_BOOL_3STATE type STRING read-only value `\TYPE=BOOLEAN` ##NO_TEXT.
Read only

frank_vonderbrelje
Discoverer
11,414

DD- Type BOOLE_D works for me. Take a look at attibut MC_BOOL_TYPES of /UI2/CL_JSON