08-09-2017 12:13 PM
I want to serialíze an object with the keyword
Call Transformation
I also want the same variables of the object always lead to the same serialization. But the serilization takes the object id into account, so that I get different values for different objects. Is there a way to achieve my desired behaviour?
08-09-2017 12:30 PM
You may use CALL TRANSFORMATION with for instance (refer to the documentation for more information) :
OPTIONS data_refs = 'heap-or-create'
08-09-2017 5:51 PM
That's not excatly what I want. For example, this code
REPORT zmartion_serialize.
CLASS lcl_serial DEFINITION FINAL CREATE PUBLIC .
PUBLIC SECTION.
METHODS constructor IMPORTING !id TYPE int4.
PRIVATE SECTION.
DATA gv_id TYPE int4.
ENDCLASS.
CLASS lcl_serial IMPLEMENTATION.
METHOD constructor.
gv_id = id.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: object TYPE REF TO lcl_serial,
serialized_object TYPE string.
CREATE OBJECT object EXPORTING id = 4.
CALL TRANSFORMATION id SOURCE model = object RESULT XML serialized_object.
WRITE: serialized_object.
gives the following serialization. I Don't want to see the "o8" in the result.
<asx:abap version="1.0" xmlns:asx="http://www.sap.com/abapxml">
<asx:values>
<MODEL href="#o8"/>
</asx:values>
<asx:heap xmlns:dic="http://www.sap.com/abapxml/types/dictionary" xmlns:cls="http://www.sap.com/abapxml/classes/global" xmlns:abap="http://www.sap.com/abapxml/types/built-in" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<prg:LCL_SERIAL id="o8" xmlns:prg="http://www.sap.com/abapxml/classes/program/ZMARTION_SERIALIZE">
<local.LCL_SERIAL>
<GV_ID>4</GV_ID>
</local.LCL_SERIAL>
</prg:LCL_SERIAL>
</asx:heap>
</asx:abap>
08-09-2017 6:16 PM
08-09-2017 5:19 PM
I suppose a big hammer approach would be to remove the ID from the XML. But may I ask why this is an issue?
data(o1) = new lcl_test( ).
data(o2) = o1.
data(o3) = new lcl_test( ).
1 and 2 are the same and 3 is a different object. If xml 1 and 2 are identical and 3 is different then this reflects the state of the application. This is good.
08-09-2017 5:34 PM
Basically I want to do an equality check on the objects. Two objects should be considered equal, if they have the same attributes.
08-09-2017 8:51 PM
You could compare just the heap sections. That's basically what Sandra's and my suggestions amount to. Either use the cl_xml* classes or create your own transformation.
If XML is not specifically a requirement then you could also use RTTI tools to enumerate and compare the classes' attributes.