This is a static method, and therefore can be called without creating an object of the class:
/epo1/cl_tools=>abap_strip(.. ).
Meaning of the ABAP-STRIP - parameters:
Note: elements and sub-elements are to be written as in ABAP (e.g. ELEMENT-SUB_ELEMENT-SUB_SUB_ELEMENT-...)
The following structure will be used for all examples below:
TYPES:
BEGIN OF lty_sub_struc,
name TYPE string,
count TYPE i,
END OF lty_sub_struc,
BEGIN OF lty_struc,
sub_1 TYPE lty_sub_struc,
sub_2 TYPE lty_sub_struc,
name TYPE string,
count TYPE i,
char TYPE char10,
list TYPE STANDARD TABLE OF string WITH NON-UNIQUE KEY table_line,
END OF lty_struc.
The following data definition will be used for the examples below:
FIELD-SYMBOLS:
<stripped> TYPE any.
DATA:
ls_struc TYPE lty_struc,
lr_stripped TYPE REF TO data,
lt_keep TYPE /epo1/ec_string_table,
lt_strip TYPE /epo1/ec_string_table.
Assign the referenced data to a field symbol:
ASSIGN lr_stripped->* TO <stripped>.
Afterwards, the field symbol may be used like any other structure (e.g. to create a JSON string).
Fill the structure only partially, every empty element (of the main structure) should be removed. This might be the most wanted use case:
" fill the data
ls_struc-sub_2-name = 'substructure #2'.
ls_struc-sub_2-count = 2.
APPEND 'teststring' TO ls_struc-list.
Data of the structure:
So we would remove the whole SUB_1 structure (but not SUB_2) and the elements NAME, COUNT and CHAR (but not LIST).
Method call:
/epo1/cl_tools=>abap_strip(
EXPORTING
IS_ANY = ls_struc
IV_STRIP_INITIAL = 'X'
IMPORTING
ER_ANY = lr_stripped ).
ASSIGN lr_stripped->* TO <stripped>.
And the result (assigned to the field symbol '<STRIPPED>')
SHOULD NEVER HAPPEN..
When creating a structure with the runtime, there must be at least one element. Whenever a structure is to be created, where there is no element left, we create a dummy element with the name '_'. This may also occur in sub-structures which must be kept, but all elements are to be stripped.
Data of the (empty) structure:
Method call:
/epo1/cl_tools=>abap_strip(
EXPORTING
IS_ANY = ls_struc
IV_STRIP_INITIAL = 'X'
IMPORTING
ER_ANY = lr_stripped ).
ASSIGN lr_stripped->* TO <stripped>.
So we would have to remove all of the structure elements, which is not possible. A dummy element is created:
Elements, which are listed in IT_KEEP are handled with highest priority - they are never stripped. Use the ABAP - format to specify any element.
Fill the structure only partially, the main element COUNT should be kept, and the element COUNT of the structure SUB_1. Because the structure SUB_1 is listed in IT_KEEP, the elements of this structure are also evaluated for 'initial' - thus, the element SUB_1-NAME will be removed:
" fill the data
ls_struc-sub_2-name = 'substructure #2'.
APPEND 'teststring' TO ls_struc-list.
" elements to keep
APPEND 'COUNT' TO lt_keep.
APPEND 'SUB_1-COUNT' TO lt_keep.
Data of the structure:
Method call:
/epo1/cl_tools=>abap_strip(
EXPORTING
IS_ANY = ls_struc
IT_KEEP = lt_keep
IV_STRIP_INITIAL = 'X'
IMPORTING
ER_ANY = lr_stripped ).
And the result (assigned to the field symbol '<STRIPPED>')
Result:
The flag IV_RECURSIVE is only useful with IV_STRIP_INITIAL, as it causes the recursive removal of empty elements.
Fill the structure only partially, the main element COUNT should be kept, and the element COUNT of the structure SUB_1. Because of the flag IV_RECURSIVE, all substructures will be evaluated for empty elements.:
" fill the data
ls_struc-sub_2-name = 'substructure #2'.
APPEND 'teststring' TO ls_struc-list.
" elements to keep
APPEND 'COUNT' TO lt_keep.
APPEND 'SUB_1-COUNT' TO lt_keep.
Data of the structure:
So we would remove NAME, COUNT, CHAR and NAME of structure SUB_1.
Method call:
/epo1/cl_tools=>abap_strip(
EXPORTING
IS_ANY = ls_struc
IT_KEEP = lt_keep
IV_STRIP_INITIAL = 'X'
IV_RECURSIVE = 'X'
IMPORTING
ER_ANY = lr_stripped ).
And the result (assigned to the field symbol '<STRIPPED>')
Result:
The table 'IT_STRIP' can be used to remove very special elements of a structure. This is independing, if that elements are empty or not.
This might be useful, when only a few elements should be removed.
" fill the data
ls_struc-sub_2-name = 'substructure #2'.
APPEND 'teststring' TO ls_struc-list.
" elements to strip
APPEND 'COUNT' TO lt_strip.
APPEND 'SUB_1-COUNT' TO lt_strip.
APPEND 'SUB_2' TO lt_strip.
Data of the structure:
We expect, that the listed elements are removed (even if not initial)
Method call:
/epo1/cl_tools=>abap_strip(
EXPORTING
IS_ANY = ls_struc
IT_STRIP = lt_strip
IMPORTING
ER_ANY = lr_stripped ).
Result:
The elements, listed in IT_STRIP, has been removed without touching the initial elements NAME, CHAR, etc.
The flag IV_STRIP_EVERYTHING is meant to really remove all elements. This is useful in combination with IT_KEEP, where few structure elements should be kept - and everything else removed.
Fill the data:
" fill the data
ls_struc-sub_2-name = 'substructure #2'.
ls_struc-sub_2-count = 2.
APPEND 'teststring' TO ls_struc-list.
" elements to keep
APPEND 'SUB_2' TO lt_keep.
APPEND 'COUNT' TO lt_keep.
Data of the structure:
..strip everything (red), which is not contained in IT_KEEP (green)
Method call:
/epo1/cl_tools=>abap_strip(
EXPORTING
IS_ANY = ls_struc
IT_KEEP = it_keep
IV_STRIP_EVERYTHING = 'X'
IMPORTING
ER_ANY = lr_stripped )
And the result:
Just to have a really complete coding (for simple copy/paste - and playing around) of the report 'Z_ABAP_STRIP'; the final breakpoint will invoke the debugger, where You can inspect the results:
*&---------------------------------------------------------------------*
*& Report Z_ABAP_STRIP
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_ABAP_STRIP.
TYPES:
BEGIN OF lty_sub_struc,
name TYPE string,
count TYPE i,
END OF lty_sub_struc,
BEGIN OF lty_struc,
sub_1 TYPE lty_sub_struc,
sub_2 TYPE lty_sub_struc,
name TYPE string,
count TYPE i,
char TYPE char10,
list TYPE STANDARD TABLE OF string WITH NON-UNIQUE KEY table_line,
END OF lty_struc.
FIELD-SYMBOLS:
<stripped> TYPE any.
DATA:
ls_struc TYPE lty_struc,
lr_stripped TYPE REF TO data,
lt_keep TYPE /epo1/ec_string_table,
lt_strip TYPE /epo1/ec_string_table.
" fill the data
ls_struc-name = 'TEST'.
ls_struc-count = 5.
ls_struc-eins-name = 'ONE'.
ls_struc-eins-count = 1.
ls_struc-sub_2-name = 'substructure #2'.
ls_struc-sub_2-count = 2.
APPEND 'teststring' TO ls_struc-list.
" elements to keep
APPEND 'COUNT' TO lt_keep.
APPEND 'SUB_1-COUNT' TO lt_keep.
APPEND 'SUB_2' TO lt_keep.
APPEND 'COUNT' TO lt_keep.
" elements to strip
APPEND 'COUNT' TO lt_strip.
APPEND 'SUB_1-COUNT' TO lt_strip.
APPEND 'SUB_2' TO lt_strip.
" get the stripped structure
/epo1/cl_tools=>abap_strip(
EXPORTING
IS_ANY = ls_struc
IT_KEEP = lt_keep
IT_STRIP = lt_strip
IV_STRIP_INITIAL = 'X'
IV_STRIP_EVERYTHING = 'X'
IV_RECURSIVE = 'X'
IMPORTING
ER_ANY = lr_stripped ).
ASSIGN lr_stripped->* TO <stripped>.
As an example, the stripped ABAP structure could be used to create a JSON string, using the static method /EPO1/CL_TOOLS=>ABAP_TO_JSON. For details, see our WIKI page JSON_API_in_SAP_ABAP.
In this example a JSON REST cloud service is called. Before the send call, the request json is stripped.
The class used for sending (CALL METHOD me->call_service) is /EPO1/CL_JSON_BASE_OUT.
METHOD send.
DATA ls_outgoinginvoice TYPE zcreateoutgoinginvoice.
DATA ls_bapiret2 TYPE bapiret2.
DATA lr_data TYPE REF TO data.
FIELD-SYMBOLS <request> TYPE any.
me->mv_service_id = 'SEND2CLOUD'.
me->mv_service_operation = 'CreateOutgoingInvoice'.
CALL METHOD me->fill_outgoinginvoice_send
EXPORTING
is_invoice = is_invoice
is_ubl21 = is_ubl21
is_ublcn21 = is_ublcn21
is_file_invoice = is_file_invoice
is_file_attachment = is_file_attachment
IMPORTING
es_outgoinginvoice = ls_outgoinginvoice
es_bapiret2 = ls_bapiret2.
IF ls_bapiret2 IS INITIAL.
CALL METHOD /epo1/cl_tools=>abap_strip
EXPORTING
is_any = ls_outgoinginvoice
it_keep =
it_strip = lt_strip
iv_strip_initial = 'X'
iv_recursive = 'X'
iv_strip_everything =
IMPORTING
ev_changed =
er_any = lr_data.
ASSIGN lr_data->* TO <request>.
CALL METHOD me->call_service
EXPORTING
is_outgoinginvoice = <request>
IMPORTING
es_bapiret2 = es_bapiret2.
ELSE.
es_bapiret2-id = 'ZINV'.
es_bapiret2-number = '000'.
es_bapiret2-type = 'E'.
es_bapiret2-message = 'error: No Invoice found'..
es_bapiret2-message_v1 = es_bapiret2-message.
ENDIF.
ENDMETHOD.