Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Serialize object to xml - Object ID

Martin_Bschen
Participant
0 Likes
1,492

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?

6 REPLIES 6
Read only

Sandra_Rossi
Active Contributor
0 Likes
1,290

You may use CALL TRANSFORMATION with for instance (refer to the documentation for more information) :

OPTIONS data_refs = 'heap-or-create'
Read only

0 Likes
1,290

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>

Read only

0 Likes
1,290

You may transform as you wish by creating a new transformation

Read only

pokrakam
Active Contributor
0 Likes
1,290

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.

Read only

0 Likes
1,290

Basically I want to do an equality check on the objects. Two objects should be considered equal, if they have the same attributes.

Read only

pokrakam
Active Contributor
0 Likes
1,290

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.