Skip to Content
0

Serialize object to xml - Object ID

Aug 09, 2017 at 11:13 AM

89

avatar image

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?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Mike Pokraka Aug 09, 2017 at 04:19 PM
0

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.

Show 2 Share
10 |10000 characters needed characters left characters exceeded

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

0

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.

0
Sandra Rossi Aug 09, 2017 at 11:30 AM
0

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

OPTIONS data_refs = 'heap-or-create'
Show 2 Share
10 |10000 characters needed characters left characters exceeded

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>

0

You may transform as you wish by creating a new transformation

0