系列文章索引
SAP 如何提供 RESTful Web 服务?
SAP 如何提供 RESTful Web 服务(2) - ABAP 与 JSON
SAP 如何提供 RESTful Web 服务(3) - Rest 路径处理
上一篇介绍了 SAP ABAP 提供 RESTful web service 从技术层面需要解决的三个问题:
- SAP 对外提供 Http Service
- 在 SAP 端解决诸如 authentication,cookie, session 等 Http server 必须解决的问题
- SAP 端实现对响应 (response)以 json 格式返回给客户端,对客户端 json 格式的请求(request)解析并处理
本篇接着上一篇,介绍如何实现 ABAP 对象 与 JSON 互转。ABAP 与 JSON 互转有多种方法,本篇介绍两个类进行序列化和反序列化的方法:
-
CL_TREX_JSON_SERIALIZER
:将 ABAP 内表转换为 json 格式 -
CL_TREX_JSON_DESERIALIZER
:json 转换为 ABAP 内表
这两个类有一点点问题,序列化生成的格式 key 没有引号,可能导致外部解析的失败。比如
[
{
id: "001",
name: "Stone"
},
{
id: "002",
name: "Brown"
}
]
而我们需要的是下面的格式:
[
{
"id": "001",
"name": "Stone"
},
{
"id": "002",
"name": "Brown"
}
]
所以需要对两个类进行改造,以符合自己的需求。首先我们使用事务码 SE24 将 CL_TREX_JSON_SERIALIZER
类拷贝一个新类,另存为 ZCL_TREX_JSON_SERIALIZER
,然后将 RECURSE()
方法做两点改变:
填加语句:
注释掉 concatenate
语句,改写为下面的语句。就是在 <abapcomp>-name
两边加上双引号 :
完整的 RECURSE()
方法代码如下:
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' .
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.
相对应地, 将 CL_TREX_JSON_DESERIALIZER
拷贝成为 ZCL_TREX_JSON_DESERIALIZER
,并且对 deserialize_object
方法做两点变更 (网上有一个做好的图片,我就直接贴图了):
图片来源:https://archive.sap.com/image/665574
完成后,deserialize_object
方法的完整代码如下:
METHOD deserialize_object.
DATA:
l_node_type TYPE REF TO cl_abap_typedescr ,
l_ref TYPE REF TO object .
ADD 1 TO offset . "skip {
l_node_type = cl_abap_typedescr=>describe_by_data( node ) .
* prepare for dynamic access
CASE l_node_type->kind .
WHEN cl_abap_typedescr=>kind_ref .
l_ref = node .
WHEN cl_abap_typedescr=>kind_struct .
WHEN OTHERS .
RAISE EXCEPTION TYPE cx_trex_serialization .
ENDCASE .
DATA:
l_done TYPE abap_bool ,
l_len TYPE i ,
l_name TYPE string .
* handle each component
WHILE l_done = abap_false .
"find next key
FIND REGEX '"(\w+)\s*":' IN SECTION OFFSET offset OF json
MATCH OFFSET offset MATCH LENGTH l_len
SUBMATCHES l_name .
IF sy-subrc <> 0 .
RAISE EXCEPTION TYPE cx_trex_serialization .
ENDIF .
ADD l_len TO offset .
FIELD-SYMBOLS <comp> TYPE ANY .
* dynamic binding to component
TRANSLATE l_name TO UPPER CASE .
CASE l_node_type->kind .
WHEN cl_abap_typedescr=>kind_ref .
ASSIGN l_ref->(l_name) TO <comp> .
WHEN cl_abap_typedescr=>kind_struct .
ASSIGN COMPONENT l_name OF STRUCTURE node TO <comp> .
IF sy-subrc <> 0.
CONTINUE.
ENDIF.
WHEN OTHERS .
RAISE EXCEPTION TYPE cx_trex_serialization .
ENDCASE .
DATA:
l_comp_type TYPE REF TO cl_abap_typedescr ,
l_ref_type TYPE REF TO cl_abap_refdescr .
* check component type
l_comp_type = cl_abap_typedescr=>describe_by_data( <comp> ) .
CASE l_comp_type->kind .
* create instance if it's an oref
WHEN cl_abap_typedescr=>kind_ref .
l_ref_type ?= l_comp_type .
l_comp_type = l_ref_type->get_referenced_type( ) .
CREATE OBJECT <comp> TYPE (l_comp_type->absolute_name) .
ENDCASE .
* deserialize current component
deserialize_node(
EXPORTING
json = json
CHANGING
offset = offset
node = <comp> ) .
FIND REGEX ',|\}' IN SECTION OFFSET offset OF json MATCH OFFSET offset .
IF sy-subrc <> 0 .
RAISE EXCEPTION TYPE cx_trex_serialization .
ENDIF .
IF json+offset(1) = '}' .
l_done = abap_true .
ENDIF .
ADD 1 TO offset .
ENDWHILE .
ENDMETHOD.
示例
调用 ZCL_TREX_JSON_SERIALIZER
实现序列化以及调用 ZCL_TREX_JSON_DESERIALIZER
实现反序列化的代码如下:
REPORT zabap_json .
DATA: serializer TYPE REF TO zcl_trex_json_serializer,
lv_json TYPE string.
DATA: BEGIN OF ls_json,
id TYPE string,
name TYPE string,
END OF ls_json.
DATA: lt_json LIKE STANDARD TABLE OF ls_json.
CLEAR ls_json.
ls_json-id = '001'.
ls_json-name = 'Stone'.
APPEND ls_json TO lt_json.
CLEAR ls_json.
ls_json-id = '002'.
ls_json-name = 'Brown'.
APPEND ls_json TO lt_json.
CREATE OBJECT serializer
EXPORTING DATA = lt_json[].
CALL METHOD serializer->serialize( ).
lv_json = serializer->get_data( ).
WRITE / lv_json.
* Deserialze
WRITE /.
WRITE: / 'Deseriaze json string to internal table: '.
DATA: deserializer TYPE REF TO zcl_trex_json_deserializer.
CLEAR lt_json.
CREATE OBJECT deserializer.
CALL METHOD deserializer->deserialize(
EXPORTING json = lv_json
IMPORTING abap = lt_json[] ).
LOOP AT lt_json INTO ls_json.
WRITE : / ls_json-id, ls_json-name.
ENDLOOP.