XML and JSON have boolean data type.
In case of XML, xsd:boolean is the data type. The xml nodes would have case sensitive values like true and false. 1 and 0 are also allowed.
Coming to JSON, same true and false values are used. Example { "sale":true }. On the other hand, { "sale":"true" } would correspond to string type.
However in case of ABAP, there has not been a dedicated boolean data type. Even if abap_bool or xsdboolean are the closest match, they can store any char1 value.
The difference is visible when serialization is done to generate xml or json.
Below code is copied from the keyword documentation. XSDBOOL function is available since ABAP 740 SP08.
NOTE:The result would be very different if boolc were used instead of xsdbool. Firstly, the transformations would have a different result (since the values "X" and " " are not transformed to true or false); secondly, the logical expression gui_flag = abap_false would always be false (since abap_false loses its blank when converted to the type string).
DATA(gui_flag) = xsdbool( cl_demo_sap_gui=>check( ) ).
CALL TRANSFORMATION id SOURCE gui_flag = gui_flag
RESULT XML DATA(xml).
DATA(writer) =
cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE gui_flag = gui_flag
RESULT XML writer.
DATA(json) = writer->get_output( ).
cl_demo_output=>write_xml( xml ).
cl_demo_output=>write_json( json ).
IF gui_flag = abap_false.
cl_demo_output=>get( ).
ELSE.
cl_demo_output=>display( ).
ENDIF.
No comments:
Post a Comment