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

problem with Serialization JSON with children

0 Likes
1,002

Hello guys,

I need to deserialize a JSON that containts children or hierarchical/recursive data, to do this I am follow example from SAP WIKI: https://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer

In that link there is a example called: Serialization/deserialization of hierarchical/recursive data

I am triying to do that example but I dont understand this line:

lo_child LIKE lo_data.

I know its a variable declaration but object lo_data not exists on the example, and its not on dicionary data types, could anyone can explain me?

TYPES:
  BEGIN OF ts_node,
    id        TYPE i,
    children  TYPE STANDARD TABLE OF REF TO data WITH DEFAULT KEY,
  END OF ts_node.

DATA: lv_exp    TYPE string,
      lv_act    TYPE string,
      ls_data   TYPE ts_node,
      lr_data   LIKE REF TO ls_data.

ls_data-id = 1.

CREATE DATA lr_data.
lr_data->id = 2.
APPEND lr_data TO ls_data-children.

lv_exp = '{"ID":1,"CHILDREN":[{"ID":2,"CHILDREN":[]}]}'.
lv_act = /ui2/cl_json=>serialize( data = ls_data ).
cl_aunit_assert=>assert_equals( act = lv_act exp = lv_exp msg = 'Serialization of recursive data structure fails' ).


CLASS lcl_test DEFINITION FINAL.
  PUBLIC SECTION.
    DATA: id TYPE i.
    DATA: children TYPE STANDARD TABLE OF REF TO lcl_test.
ENDCLASS.


DATA: lo_act    TYPE REF TO lcl_test,
      lo_exp    TYPE REF TO lcl_test,
      lv_json   TYPE string,
      lo_child  LIKE lo_data.

CREATE OBJECT lo_exp.

lo_exp ->id = 1.

CREATE OBJECT lo_child.
lo_child->id = 2.
APPEND lo_child TO lo_exp->children.

lv_json = /ui2/cl_json=>serialize( data = lo_exp ).
ui2/cl_json=>deserialize( EXPORTING json = lv_json CHANGING data =  lo_act ).
1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
0 Likes
780

To make it valid, I replaced the line with:

      lo_child  TYPE REF TO lcl_test.
1 REPLY 1
Read only

Sandra_Rossi
Active Contributor
0 Likes
781

To make it valid, I replaced the line with:

      lo_child  TYPE REF TO lcl_test.