Sometimes, it is more convenient to have the mapping information directly in code instead of relying on customiziation.
The contents of the field name mapping - /EPO1/FIELD_MAP can also be defined in code, but the conversion ABAP <-> JSON has to be called manually (using the methods /EPO1/CL_TOOLS=>ABAP_TO_JSON or /EPO1/CL_TOOLS=>JSON_TO_ABAP).
Prepare the mapping table, the first method call should clear existing mapping entries. Like in the customizing of table /EPO1/FIELDMAP, a default - mapping can be specified:
/epo1/cl_field_name_mapping=>set_static_mapping(
iv_clear_existing_mapping = 'X'
iv_fieldname_abap = ''
iv_fieldname_external = '[camelCase]' ).
/epo1/cl_field_name_mapping=>set_static_mapping(
iv_fieldname_abap = 'ABAP_FIELDNAME'
iv_fieldname_external = 'JSON-field_name' ).
Drawback is, that the conversion between ABAP and JSON has to be done manually, when calling a webservice with the class /EPO1/CL_JSON_BASE_OUT.
example outbound - ABAP to JSON
There is already a filled ABAP structure, which should be converted to JSON as request to a consumed webservice. Use the dummy-service identifier '[static_mapping]' instead of a real EPO service name.
" data definition
DATA:
ls_abap_request TYPE ..., " define the ABAP structure
lv_json_request TYPE xstring,
lo_json_out TYPE REF TO /epo1/cl_json_base_out,
ls_callstatus TYPE /epo1/callstatus.
" fill the mapping table
" (see example above)
" map ABAP to JSON
/epo1/cl_tools=>abap_to_json(
EXPORTING
i_any = ls_abap
i_service_id = '[static_mapping]' " !!! use the prepared mapping table
IMPORTING
es_callstatus = ls_callstatus
e_json_x = lv_json ).
" check for conversion errors
IF ls_callstatus-type = 'E'.
" error handling..
ENDIF.
" invoke the webservice
CREATE OBJECT lo_json_out
EXPORTING
iv_service_id_outbound = ... " EPO service ID (outbound)
iv_service_operation = ... " EPO service-operation
.
lo_json_out->call_service(
EXPORTING
iv_request_data = lv_json
iv_http_method = 'POST'
IMPORTING
es_response_data = ... " ABAP structure for the response
es_callstatus = cs_callstatus ).
IF cs_callstatus-type = 'E'.
" error handling..
ENDIF.
example inbound - JSON to ABAP
There is a JSON string/xstring (response from a webservice), which should be converted to an ABAP structure. Use the dummy-service identifier '[static_mapping]' instead of a real EPO service name.
Instead of calling the method 'CALL_SERVICE', use the similar method 'CALL_SERVICE_DOWNLOAD', which returns the binary response.
" data definition
DATA:
ls_abap_response TYPE ..., " define the ABAP structure
lv_json_response TYPE xstring,
lo_json_out TYPE REF TO /epo1/cl_json_base_out,
ls_callstatus TYPE /epo1/callstatus.
" invoke the webservice
CREATE OBJECT lo_json_out
EXPORTING
iv_service_id_outbound = ... " EPO service ID (outbound)
iv_service_operation = ... " EPO service-operation
.
lo_json_out->call_service_download(
EXPORTING
" is_request_data = ... " ABAP structure
" iv_request_data = ... " JSON data
iv_http_method = ... " 'GET', 'POST',..
IMPORTING
ev_response = lv_json_response
es_callstatus = cs_callstatus ).
IF cs_callstatus-type = 'E'.
" error handling..
ENDIF.
" fill the mapping table
" (see example above)
" map JSON to ABAP
/epo1/cl_tools=>json_to_abap(
EXPORTING
i_json_x = lv_json_response
i_service_id = '[static_mapping]' " !!! use the prepared mapping table
IMPORTING
e_any = ... " ABAP structure
es_callstatus = ls_callstatus ).
" check for conversion errors
IF ls_callstatus-type = 'E'.
" error handling..
ENDIF.