Skip to Content

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 = sytabix .

*         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.

To report this post you need to login first.

10 Comments

You must be Logged on to comment or reply to a post.

    1. Chris Paine

      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.

      (0) 
  1. Mike Howles

    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 ‘&amp;’


    At least this was my case, the JSON wouldn’t parse otherwise.

    (0) 

Leave a Reply