Serialize ABAP data into JSON format
After reading John Moy ‘s “JqueryMobile and SAP” blog series, i looked for a JSON converter. In community there are several examples and SAP also serves a class: “CL_TREX_JSON_SERIALIZER”.
In my workouts on CL_TREX_JSON_SERIALIZER, i realized that there are two deficiency in standard implementation. Both of them are fixed easily.
1 – There is a space that causes arithmetic problems, at the end of the converted numeric values.
To fixed this after line 40 in RECURSE method add
CONDENSE l_value.
2 – Created JSON isn’t valid. CL_TREX_JSON_SERIALIZER doesn’t put the attribute in double quotes.
To fixed this, comment line 52 in method RECURSE; and add;
CONCATENATE ‘”‘ <abapcomp>–name ‘”‘ c_colon INTO l_value.
Example use of CL_TREX_JSON_SERIALIZER:
DATA: it_usr01 TYPE STANDARD TABLE OF usr01,
lo_json_data TYPE REF TO zcl_trex_json_serializer,
json TYPE string.
SELECT *
INTO TABLE it_usr01
FROM usr01.
CREATE OBJECT lo_json_data
EXPORTING
DATA = it_usr01.
* serialize data
lo_json_data->serialize( ).
* get serialized json data string
json = lo_json_data->get_data( ).
WRITE json.
New implementation of RECURSE method:
method RECURSE.
data:
l_type type c ,
l_comps type i ,
l_lines type i ,
l_index type i ,
l_value type string .
field-symbols:
<itab> type any table ,
<comp> type any .
describe field data type l_type components l_comps .
if l_type = cl_abap_typedescr=>typekind_table .
* itab -> array
append ‘[‘ to me->fragments .
assign data to <itab> .
l_lines = lines( <itab> ) .
loop at <itab> assigning <comp> .
add 1 to l_index .
recurse( <comp> ) .
if l_index < l_lines .
append c_comma to me->fragments .
endif .
endloop .
append ‘]’ to fragments .
else .
if l_comps is initial .
* field -> scalar
* todo: format
l_value = data .
replace all occurrences of ‘\’ in l_value with ‘\\’ .
replace all occurrences of ”” in l_value with ‘\’” .
replace all occurrences of ‘”‘ in l_value with ‘\”‘ .
replace all occurrences of ‘&’ in l_value with ‘\&’ .
replace all occurrences of cl_abap_char_utilities=>cr_lf in l_value with ‘\r\n’ .
replace all occurrences of cl_abap_char_utilities=>newline in l_value with ‘\n’ .
replace all occurrences of cl_abap_char_utilities=>horizontal_tab in l_value with ‘\t’ .
replace all occurrences of cl_abap_char_utilities=>backspace in l_value with ‘\b’ .
replace all occurrences of cl_abap_char_utilities=>form_feed in l_value with ‘\f’ .
* space at the end of numbers
CONDENSE l_value.
concatenate ‘”‘ l_value ‘”‘ into l_value .
append l_value to me->fragments .
else .
* structure -> object
data l_typedescr type ref to cl_abap_structdescr .
field-symbols <abapcomp> type abap_compdescr .
append ‘{‘ to me->fragments .
l_typedescr ?= cl_abap_typedescr=>describe_by_data( data ) .
loop at l_typedescr->components assigning <abapcomp> .
l_index = sy–tabix .
* concatenate <abapcomp>-name c_colon into l_value .
CONCATENATE ‘”‘ <abapcomp>–name ‘”‘ c_colon INTO l_value .
translate l_value to lower case .
append l_value to me->fragments .
assign component <abapcomp>–name of structure data to <comp> .
recurse( <comp> ) .
if l_index < l_comps .
append c_comma to me->fragments .
endif .
endloop .
append ‘}’ to me->fragments .
endif .
endif .
endmethod.
Good one. Thanks. π
Seems like a similar bug to one that we fixed in ZJSON about a year ago π
Which is one reason I like the open-sourced solutions, bugs get fixed so much quicker.
Check the ABAP Json document class project on Code Exchange by Uwe Fetzer https://cw.sdn.sap.com/cw/groups/zjson
I suggest to use zjson instead of cl_trex_json_serializer
It was great - looking for comments to the class and finding directly the solution for our Problem π ➕ ➕
Hi Veronika,
it is very nice to hear that you found solution directly, Power of SCN π
Hello Basar sir,
how can we deseralize json format webservice in abap
Hello Muhammad,
you can check ZJSON- JSON and ABAP - Code Gallery - SCN Wiki
Thanks - Exactly my problem (missing quotes on attributes). You rock.
EDIT: Also, I think this line should change from:
replace all occurrences of '&' in l_value with '\&'
to:
replace all occurrences of '&' in l_value with '&'
At least this was my case, the JSON wouldn't parse otherwise.
helped me a lot π
Hi everybody.
how about the classΒ cl_trex_json_deserializer, does anybody knows how to fix its bugs?
tks
Felipe