Skip to Content

Abstract

In this Weblog I will present the most simple solution to realize serialization in ABAP.

Contents

  • Prerequisites
  • Introduction
  • Technical Details
  • The Solution
  • Conclusion

Prerequisites

None

Introduction

In several .NET and Java projects I used a technique called serialization to store working data of programs to disk without having to struggle with file formats and so on. My actual projects is based on the ABAP programming language and after doing some research I found a very easy and convincing way to do serialization in ABAP.
Let me once again explain the idea of serialization:
Imagine you have some seriously complex working data in your program that you don’t want to save in a relational database. The main alternative way of achieving persistency is then to save the working data to files. But writing and reading complex structured data to / from files can be really cumbersome because file access is just serial and unstructured.
Serialization is the solution to this problem because it automatically transforms your most complex data structures into a serial form which can then be written to a file. But the mechanism is even more powerful because it let’s you read the data in it’s serial form back in and transforms it back into the concrete data structure that your program can use.
If you organize all your data in a central structure or object this gives you the possibility to store or restore all your programs data in one single step !

Technical Details

ABAP uses so called transformations to generate the serial form of data. Because these transformations are XSL-Transformations the output form is XML which is a perfect match for the task of serialization because it is a ‘one string’ representation of your data but includes structural information too.

The solution

In this section I will present the most simple way of serializing data, writing it to a file, reading it back in and finally deserializing it back to the data.
The part which requires most work is supposed to be the easiest: the initialization of the file we want to write our serialized XML-String to. The reason for this is the fact that special XML-Output streams are used as an interface to the file and these are not that easy to get. But don’t worry it’s not really difficult either.

Serialization

Before we start we need some data to serialize. I chose the famous example structure ‘SPFLI’:

First step is to create an instance of the base SAP-utility class that deals with XML:

Next we have to create a stream factory which will finally create the desired XML-output-stream for us:

Now we are ready to create the stream. The only important thing we have to supply to the factory method which creates the stream is the filename. I chose ‘test.xml’ for this example. As the parameter can handle general URIs (ftp://mysite.com would be another example for an URI) we have to supply our filename with the prefix ‘file://’ which denotes the path to a file:

Now that we have the stream prepared we are ready to transform / ‘serialize’ our data and write it to the file simultaneously:

For the technically interested: ‘data_node’ in the lines of code above denotes the name of the XML-node that is created for the object that is transformed. This name is not really important but it is important to remember it because we need it again when deserializing the data.

Deserialization

Now that we have stored the data in a file we want to read it back in into the example structure so we can use the data in our program. Let’s say we have executed the lines in the ‘Serialization’ section above successfully. Then we ended our program that the ‘example_data’ belonged to. Now we want to start the program again and use the ‘example_data’ that we saved to disk.
Once again we have to start with the data declaration:

Afterwards it’s time to create a stream again. Only this time it’s an XML-INPUT-stream.
Create an instance of the base SAP-utility class that deals with XML:

Create the stream factory which will create the XML-input-stream for us:

The creation of the XML-input-stream is easy: we just need to supply the correct filename with ‘file://’ prefix to the factory:

We are now ready to transform / ‘deserialize’ the XML-Data from the into our data structure so we can continue working with it:

We are already finished here. The ‘example_data’ structure is now filled with the values that we saved to disk before we restarted our program.

Conclusion

Serialization is a very powerful technique and obviously I just showed you a very basic application of it.
Remember that you can serialize every DDIC structure including tables and objects* !
In part two of this Weblog-mini-series on serialization I will even show you how to serialize ARBITRARY data i.e. data you don’t know the structure of. Obviously it is completely impossible to store such data in a relational database because it needs structured data to store.

*The classes of objects you want to serialize need to implement the ‘IF_SERIALIZABLE_OBJECT’ interface

To report this post you need to login first.

7 Comments

You must be Logged on to comment or reply to a post.

  1. Community User
    Hi Patrick,

    nice to see that you had the time to put this weblog together. I am still impressed how easy it is to serialize/deserialize an object. Oh and I am still wondering why you can’t read a similar example at help.sap.com 🙂

    Thomas

    (0) 
    1. Patrick Baer Post author
      Thanks Thomas !

      Also watch out for Part II which will appear maybe tomorrow or – more likely – till next wednesday. It opens up a whole new set of possibilities…

      Best regards,
      Patrick Baer

      (0) 
  2. Subramanian Venkateswaran
    I have been following the conversation in the ABAP forum between you and Thomas Jung. I am glad that you took the time and effort to make this wonderful weblog. Very interesting. Looking forward to your next weblog.

    Regards,
    Subramanian V.

    (0) 
  3. Anonymous
    Hi Patrick,
    Ur blog on Serialization has helped me a great deal of work. But the requirement in my project is that I pass the Serialized XML String to a Remote Function Module by calling the RFC. In the RFC, I need to deserialize the XML to corresponding class instances/Objects.

    CALL TRANSFORMATION id
            SOURCE XML i_xml
            RESULT  ref = O2 .

    But this stmt throws an exception in d RFC stating
      XML_INVALID_REF A non-existent objectID was referenced (property href).

    I have no ideas of getting this work. Please help me if u have any information about it. Looking fwd.. U can also mail me ur inputs to srikanth.chandru@sap.com

    Thanks,
    Srikanth

    (0) 
    1. Patrick Baer Post author
      Srikanth,

      have you tried your serialization locally i.e. without rfc ? From what information you provide I assume that some part of the data you serialize is not serializable. If that is the case the xml contains just an object id but not the objects data which could result in the exception you describe. Sadly I don’t have access to an R3 system for some time now so I can’t get into details.
      If your serialization works locally I can’t help you because I never investigated rfc with sap.
      If it doesn’t then check all classes/objects you serialize  if they implement the IF_SERIALIZABLE_OBJECT interface (see conclusion section of weblog post).

      I hope this solves it for you.

      Best regards
      Patrick Baer

      (0) 

Leave a Reply