Sunday, June 18, 2017

Convert internal table into binary Xstring (via JSON) and back

There is a lot of cases where binary data representation becomes handy. E.g. for some data transfers or saving data into database via rawstring format. No need to mention advantages of JSON over XML. The conversion within ABAP is described here.


There is a list of some conversion tools (the list is not comprehensive)

  1. CALL TRANSFORMATION ID - kernel general functionality
  2. CL_TREX_JSON_SERIALIZER & CL_TREX_JSON_DESERIALIZER - it seems it would be later absorbed by part of HANA = uncertain destiny
  3. /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


This is general way working with two patches  starting from version of ABAP 7.02 on. Here you relay on kernel functionality - some internal binary black box coding :) Nevertheless it is working well. Of course you can write down your own XSLT transformation. Because as I wanted to have it simple as possible, finally I discovered a way without a need of custom transformation for deserialization.



DATAlo_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 ROWS
INTO TABLE lt_flight.

" Serialize data - into binary JSON
lo_writer cl_sxml_string_writer=>createif_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


These classes are simple to use, pretty straight forward and easy to understand. The question is the future of them.

Get some data to be serialized


DATAlo_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 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.


DATAlt_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


CREATE OBJECT lo_json_deserializer.
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:





No comments:

Post a Comment