
After reading john.moy3 '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' .
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
8 | |
7 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
5 | |
4 |