ABAP Keyword Documentation → ABAP - Reference → Data Interfaces and Communication Interfaces → ABAP and XML → Transformations for XML → asXML - Canonical XML Representation → asXML - Mapping of ABAP Data Types → asXML - Mapping of Reference Variables and Objects → asXML - Named Reference Variables
Serializing Data References
This example demonstrates serializations of data references to asXML and to asJSON.
Other versions:
7.31 | 7.40 | 7.54
Source Code
DATA(out) = cl_demo_output=>new(
)->begin_section( `Serialization of Data References` ).
DATA xml TYPE xstring.
DATA writer TYPE REF TO cl_sxml_string_writer.
"Data reference with named type
out->begin_section( `Data Reference with Named Type` ).
TYPES:
BEGIN OF struc,
comp1 TYPE i,
comp2 TYPE i,
END OF struc.
DATA dref1 TYPE REF TO struc.
dref1 = NEW #( comp1 = 111 comp2 = 222 ).
out->begin_section( `asXML for Heap Reference` ).
CALL TRANSFORMATION id SOURCE dref = dref1
RESULT XML xml.
out->write_xml( xml
)->next_section( `asJSON for Heap Reference` ).
writer = cl_sxml_string_writer=>create(
type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE dref = dref1
RESULT XML writer.
out->write_json( writer->get_output( )
)->end_section( ).
"Data reference with technical type
out->next_section( `Data Reference with Technical Type` ).
DATA:
BEGIN OF struc,
comp1 TYPE i,
comp2 TYPE i,
END OF struc,
dref2 LIKE REF TO struc.
dref2 = NEW #( comp1 = 111 comp2 = 222 ).
out->begin_section( `asXML for Heap Reference`
)->begin_section( `Without Transformation Option` ).
TRY.
CALL TRANSFORMATION id SOURCE dref = dref2
RESULT XML xml.
out->write_xml( xml ).
CATCH cx_xslt_serialization_error INTO DATA(xml_err).
out->write( xml_err->previous->get_text( ) ).
ENDTRY.
out->next_section( `With Transformation Option` ).
CALL TRANSFORMATION id SOURCE dref = dref2
RESULT XML xml
OPTIONS technical_types = 'ignore'.
out->write_xml( xml
)->end_section(
)->next_section( `asJSON for Heap Reference`
)->begin_section( `Without Transformation Option` ).
TRY.
writer = cl_sxml_string_writer=>create(
type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE dref = dref2
RESULT XML writer.
out->write_json( writer->get_output( ) ).
CATCH cx_xslt_serialization_error INTO DATA(json_err).
out->write( json_err->previous->get_text( ) ).
ENDTRY.
out->next_section( `With Transformation Option` ).
writer = cl_sxml_string_writer=>create(
type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE dref = dref2
RESULT XML writer
OPTIONS technical_types = 'ignore'.
out->write_json( writer->get_output( )
)->end_section(
)->end_section( ).
"Stack reference
out->next_section( `Stack Reference` ).
DATA dref3 TYPE REF TO i.
dref3 = REF #( 333 ).
out->begin_section( `asXML for Stack Reference`
)->begin_section( `Without Transformation Option` ).
CALL TRANSFORMATION id SOURCE dref = dref3
RESULT XML xml.
out->write_xml( xml
)->next_section( `With Transformation Option` ).
CALL TRANSFORMATION id SOURCE dref = dref3
RESULT XML xml
OPTIONS data_refs = 'heap-or-create'.
out->write_xml( xml
)->end_section(
)->next_section( `asJSON for Stack Reference`
)->begin_section( `Without Transformation Option` ).
writer = cl_sxml_string_writer=>create(
type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE dref = dref3
RESULT XML writer.
out->write_json( writer->get_output( )
)->next_section( `With Transformation Option` ).
writer = cl_sxml_string_writer=>create(
type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE dref = dref3
RESULT XML writer
OPTIONS data_refs = 'heap-or-create'.
out->write_json( writer->get_output( )
)->display( ).
Description
This example demonstrates the serialization of data reference variables using the identity transformation ID to the canonical formats asXML and asJSON.
- First a data reference is serialized that points to an anonymous data object in the heap and whose
dynamic type
struc
was created in the same program using theTYPES
statement. No problems should arise from this serialization.
- Next, however, a data reference variable is serialized that points to an anonymous data object in
the heap whose dynamic type does not have a name. This is the technical type of a structure and reference
to this type is made using
LIKE
. A data reference variable of this type cannot be serialized without taking further actions. This exception can be prevented using the transformation optiontechnical_types
, however the data reference variable is ignored.
- Finally, a data reference variable is serialized that points to a data object in the stack. The
data reference variable is ignored by default, but can be serialized using the transformation option
data_refs
.