ABAP Serialization – Part I: Quick-n-easy
In this Weblog I will present the most simple solution to realize serialization in ABAP.
- Technical Details
- The Solution
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 !
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.
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.
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.
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.
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