Skip to Content

Here is a little sample program to create JSON from ABAP and to convert JSON back to ABAP Types. I got the requirement as i was experimenting with UI5 where i needed a class for a SICF service implementation http handler, that provides JSON from ABAP and vice versa.

i’ve read, that there was an issue with the json serializer/deserializer classes and that ther were SAP notes available for a correction.

Here’s the code snippet:


DATA:
lo_json_serializer   TYPE REF TO cl_trex_json_serializer,
lo_json_deserializer TYPE REF TO cl_trex_json_deserializer,
lt_bseg              TYPE STANDARD TABLE OF bseg,    
ls_bseg              TYPE bseg,    
lt_bseg_deserialized TYPE STANDARD TABLE OF bseg,    
lv_json_string       TYPE string. "this string contains json
* Select some data from BSEG table
SELECT * FROM bseg INTO CORRESPONDING FIELDS OF TABLE lt_bseg UP TO 10 ROWS.
* Create JSON Serializer Object
CREATE OBJECT lo_json_serializer
EXPORTING     data = lt_bseg.
* Call serialization method
CALL METHOD lo_json_serializer->serialize.
* Get the serialized data
CALL METHOD lo_json_serializer->get_data
RECEIVING     rval = lv_json_string.
WRITE /'SERIALIZED'.
* Create JSON Deserializer Object
CREATE OBJECT lo_json_deserializer.
* Call deserialization method
CALL METHOD lo_json_deserializer->deserialize
EXPORTING     json = lv_json_string
IMPORTING     abap = lt_bseg_deserialized.
WRITE /'DESERIALIZED'.
* Compare pre and post serialization data
IF lt_bseg[] = lt_bseg_deserialized[].
WRITE /'SERIALIZE & DESERIALIZE SUCCESSFULL'.
ENDIF.
To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply