There is a list of some conversion tools (the list is not comprehensive)
- CALL TRANSFORMATION ID - kernel general functionality
- CL_TREX_JSON_SERIALIZER & CL_TREX_JSON_DESERIALIZER - it seems it would be later absorbed by part of HANA = uncertain destiny
- /UI2/CL_JSON or CL_FDT_JSON - not reachable everywhere
I explored a bit the usage of Call transformation and Trex classes. You can observe my conclusions here below.
1) CALL TRANSFORMATION
DATA: lo_writer TYPE REF TO cl_sxml_string_writer,
lv_xstring_json TYPE xstring,
" source itab - for serialization
lt_flight TYPE STANDARD TABLE OF spfli,
" target itab - after deserialization
lt_flight2 TYPE STANDARD TABLE OF spfli.
" Get data from database into itab
SELECT *
FROM spfli
UP TO 3 ROWS
INTO TABLE lt_flight.
" Serialize data - into binary JSON
lo_writer = cl_sxml_string_writer=>create( if_sxml=>co_xt_json ).
TRY.
CALL TRANSFORMATION id
SOURCE itab = lt_flight
RESULT XML lo_writer.
CATCH cx_xslt_format_error.
MESSAGE 'Some serialization error occured.' TYPE 'E'.
ENDTRY.
" Now we have a serialized JSON data
lv_xstring_json = lo_writer->get_output( ).
The back way - get back the itab
" Deserialization of data - back into itab
TRY.
CALL TRANSFORMATION id
SOURCE XML lv_xstring_json
RESULT itab = lt_flight2.
CATCH cx_xslt_format_error.
MESSAGE 'Some deserialization error occured.' TYPE 'E'.
ENDTRY.
" Final confirmation
ASSERT lt_flight EQ lt_flight2.
2) TREX classes
Get some data to be serialized
DATA: lo_json_serializer TYPE REF TO cl_trex_json_serializer,
lo_json_deserializer TYPE REF TO cl_trex_json_deserializer,
lt_flight TYPE STANDARD TABLE OF spfli,
lv_json TYPE string,
lv_binary TYPE xstring.
SELECT *
FROM spfli
UP TO 3 ROWS
INTO TABLE lt_flight.
This is the source itab
Create JSON string out of itab
CREATE OBJECT lo_json_serializer
EXPORTING
data = lt_flight.
lo_json_serializer->serialize( ).
lv_json = lo_json_serializer->get_data( ).
Now we have a following JSON string.
Create a binary string out of JSON string
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = lv_json
* MIMETYPE = ' '
* ENCODING =
IMPORTING
buffer = lv_binary
EXCEPTIONS
failed = 1.
There is nice binary content ready for a journey.
And now, the back way. Get itab out of binary data.
Get JSON string out of binary string
Note: I used for purpose different target variables.DATA: lt_flight2 TYPE STANDARD TABLE OF spfli,
lv_json2 TYPE string.
CALL FUNCTION 'HR_KR_XSTRING_TO_STRING'
EXPORTING
in_xstring = lv_binary
IMPORTING
out_string = lv_json2.
Get itab out of JSON string
lo_json_deserializer->deserialize(
EXPORTING
json = lv_json2
IMPORTING
abap = lt_flight2
).
" Final confirmation
ASSERT lt_flight EQ lt_flight2.
Now we are at the very beginning, holding exactly the same itab as at the beginning. Choose your own way ;-)
Useful links:
- http://www.abap-developers.com/2014/07/what-is-xstring-data-type-and-how-to-convert-it-to-various-formats/
- https://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer
- https://blogs.sap.com/2016/11/17/displaying-json/
- https://blogs.sap.com/2013/02/21/three-different-ways-to-serialize-and-deserialize-complex-abap-data/
- http://www.onsap.com/questions/212/how-to-generate-json-content-from-abap
- https://blogs.sap.com/2013/04/15/abap-2-json-and-json-2-abap-with-st/