Para pasar el contenido de una estructura a JSON necesitaremos crear una copia de la clase estándar CL_TREX_JSON_SERIALIZER y crear los métodos ZRECURSE y ZSERIALIZE.
Es necesario realizar esta modificación para que el nombre del campo y el valor se incluyan entre comillas dobles, la clase estándar solo incluye entre comillas dobles el valor, con esta modificación obtendremos el siguiente resultado:
Antes
{ Nombre_campo: “Valor”}
Después
{ "Nombre_campo": “Valor”}
Crear copia de la clase estándar CL_TREX_JSON_SERIALIZER
Entraremos en la transacción SE24 y pulsar sobre el botón de copiar (Dos rectángulos blancos superpuestos) e informar un nuevo nombre para la clase, debe empezar por Z*.

Crearemos los métodos ZRECURSE y ZSERIALIZE con los mismos parámetros que los estándares.

Contenido del método ZRECURSE
METHOD ZRECURSE .
DATA:
l_type TYPE c,
l_comps TYPE i,
l_lines TYPE i,
l_index TYPE i,
l_value TYPE string.
FIELD-SYMBOLS:
TYPE ANY TABLE,
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 .
l_lines = lines( ) .
LOOP AT ASSIGNING .
ADD 1 TO l_index .
zrecurse( ) .
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 TYPE abap_compdescr .
APPEND '{' TO me->fragments .
l_typedescr ?= cl_abap_typedescr=>describe_by_data( data ) .
LOOP AT l_typedescr->components ASSIGNING .
l_index = sy-tabix .
* CONCATENATE -name c_colon INTO l_value .
CONCATENATE '"' -name '"' c_colon INTO l_value .
TRANSLATE l_value TO LOWER CASE .
APPEND l_value TO me->fragments .
ASSIGN COMPONENT -name OF STRUCTURE data TO .
zrecurse( ) .
IF l_index < l_comps . APPEND c_comma TO me->fragments .
ENDIF .
ENDLOOP .
APPEND '}' TO me->fragments .
ENDIF .
ENDIF .
ENDMETHOD.
Contenido del método ZSERIALIZE
METHOD ZSERIALIZE . FIELD-SYMBOLS TYPE data . ASSIGN me->data_ref->* TO . zrecurse( ) . ENDMETHOD.
Utilización de la nueva clase
Declararemos la clase y le pasaremos la estructura con el contenido del JSON, a continuación utilizaremos el método ZSERIALIZE y recuperaremos el valor en un string con el método GET_DATA.
DATA: lc_zserializer TYPE REF TO zcl_trex_json_serializer,
CREATE OBJECT lc_serializer
EXPORTING
data = lw_estructura_json.
lc_serializer->zserialize( ).
lv_string_json = lc_serializer->get_data( ).
Para ver mas tutoriales de SAP, pulse en el siguiente enlace: Listado de tutoriales