2020 Jul 30 12:04 PM
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.
2020 Jul 30 3:21 PM
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).
2020 Aug 04 2:38 PM
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-
2021 Feb 10 8:11 AM
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?
2021 Jun 10 7:27 AM
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.
2023 Sep 07 4:27 PM
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.
2024 May 21 5:06 PM
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!
2021 Jun 10 11:01 PM
Use the Data Element as BOOLEAN and override the Key name with the name_mappings parameter and both these should work fine
2022 Sep 15 3:11 PM
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.
2022 Dec 15 2:58 PM
DD- Type BOOLE_D works for me. Take a look at attibut MC_BOOL_TYPES of /UI2/CL_JSON