Serialization – Clone Dolly in ABAP

This article was written by José Vília:
The Dolly sheep was alive in ABAP and I didn’t know about it.
Having created a class instance, I’d like to share it with another totally independent program to use it as if the instance had been created there.
It’s an ABAP Dolly sheep factory we’re talking about here, people. Serialization in the ABAP world.
The first thing we need is a class that implements interface IF_SERIALIZABLE_OBJECT:
Let’s keep our class very simple: just one attribute, a constructor and a method:
The constructor takes a parameter which is used to set the attribute and the method uses that attribute to diplay a message:
We’ll be using a database table as a communication channel between the two programs. But it could also be an RFC call between two different systems. Or even an email!
The table:
What follows is the code of both programs: the sender and the receiver. It should be simple enough for you to understand.
The sender will:
- instantiate the class;
- serialize it;
- save the result of the serialization in the database table.
report zjrv_test_serialize.
types: ty_xline(1096) type x,
ty_ta_xline type standard table of ty_xline.
data: gt_tab_res type ty_ta_xline.
data: gs_tab like line of gt_tab_res.
data: gt_ser type standard table of ztest_serialize.
data: gs_ztest_serialize type ztest_serialize.
data: go_original type ref to ZCL_TEST_SHARE_MESSAGE.
data gv_string type string.
data gv_remaining type string.
data gv_int type i.
parameters:
p_msg type string default 'HERE GOES DOLLY THE SHEEP'.
start-of-selection.
* Create Object
create object go_original
exporting
i_text = p_msg.
* Transform the object into xstring.
call transformation id_indent
source obj = go_original
result xml gt_tab_res.
data gv_xstring type xstring.
loop at gt_tab_res into gs_tab.
concatenate gv_xstring gs_tab into gv_xstring in byte mode.
endloop.
* handle the xtring as a string and put it into a db table
gv_remaining = gv_xstring.
do .
gv_int = strlen( gv_remaining ).
if gv_int le 256.
exit.
endif.
gv_string = gv_remaining(256).
add 1 to gs_ztest_serialize-seq.
gs_ztest_serialize-str = gv_string.
append gs_ztest_serialize to gt_ser .
gv_remaining = gv_remaining+256.
enddo.
if gv_remaining is not initial.
add 1 to gs_ztest_serialize-seq.
gs_ztest_serialize-str = gv_remaining.
append gs_ztest_serialize to gt_ser .
endif.
modify ztest_serialize from table gt_ser .
commit work and wait.
The receiver will:
- Read the serialized instance from the database;
- Deserialize it, converting it again into an instance of the same class;
- Call the desired method
The method, which makes use of the class attribute, is the proof that the class state survived the cloning process.
report zjrv_test_deserialize.
start-of-selection.
data gv_xstring2 type xstring.
data gt_ser type standard table of ztest_serialize.
data gs_ser like line of gt_ser.
data go_copy type ref to zcl_test_share_message .
data gv_string type string.
select *
from ztest_serialize
into table gt_ser.
sort gt_ser by seq.
clear gv_string.
loop at gt_ser into gs_ser.
concatenate gv_string gs_ser-str into gv_string .
endloop.
gv_xstring2 = gv_string.
call transformation id_indent
source xml gv_xstring2
result obj = go_copy.
go_copy->send_message( ).
Serialization is very common in Java, C# and other more generic languages. But for some reason, ABAP uses it so seldomly that very few people know that it can be done. I hope this article lets more people know aobut this powerful and yet simple functionality. You’ll not be using it every day. But that day may come in which it will be the perfect solution for your problem.
Note: the class code cannot change between the two different moments of its life, when it runs in the sender and when it is cloned in the receiver. Likewise, if the instance is sent between two different systems, boths these systems must share the exact same class. Otherwise, something will go wrong.
Thank you so much José Vília for this excelent contribution.
Greetings from Abapinho.
30 de October de 2017 às 10:12 AM
Bom dia Nuno,
Obrigado pela publicação e, como é óbvio, por todas as outras publicações e artigos que todos os dias nos enriquecem um pouco mais. De facto, escreves muito bem e sabes sempre como fazer os temas pesados, mais interessantes. É um prazer ler o teu blog!
Ainda bem que gostaste desta funcionalidade. Espero que possa ajudar alguém com ela.
Um abraço,
José Vília
30 de October de 2017 às 10:15 AM
Olá José, eu é que agradeço!
19 de December de 2017 às 05:26 PM
Olá,
Existe alguma razão especial para que o campo STR na tabela tenha 256 caracteres? Algum problema se gravar gv_remaining completo sem fazer a quebra?
20 de December de 2017 às 12:32 PM
Olá Edson,
Sinceramente, já não me recordo porque motivo criei a tabela com um sequencial nem porque defini os 256 de comprimento máximo do campo string na tabela.
Se não me engano, foi por uma questão de visualização dos dados em debug. Depois acabei por deixar estar assim já que servia o propósito da demonstração.
Mas é uma questão de se fazer um teste:
1 – Modificamos a tabela para, no momento da definição do tipo incorporado deixar de ter os 256 e removemos o sequencial.
2 – Adaptamos os programas para remover a lógica adjacente às modificações do ponto 1.
3 – Se a Dolly “falar”, teste OK!
Obrigado,
José Vília
________________________________________________________
For the guys that read this blog in English:
Edson asked if there was any reason behind the specified length of the field STR in the table (256 chars).
Me:
There is no reason behind it, that I can remember. I believe it was a matter of visualization of the data that was being manipulated in debug mode.
Nevertheless, this can be tested:
1 – Change the table to remove the limitation of the 256 chars of length of the field STR and remove the sequential field.
2 – Adapt the program to disregard the logic related to the modifications done in the first point.
3 – Test it. If Dolly is still “speaking”: test OK!
Thank you,
José Vília
22 de December de 2017 às 02:22 PM
Olá José,
Fiz o teste e funciona perfeitamente. Obrigado pela dica (já estou usando num desenvolvimento aqui na empresa).
Abraço.