一、前言
目前项目中很多地方会用到将ABAP的格式转换为JSON或者XML格式,下面介绍几种方法:
對transformation的方式生成XML文件,方法有多種:
- call transformation
- cl_proxy_xml_transform=>abap_to_xml_xstring的方式:
- /ui2/cl_json
- CL_TREX_JSON_SERIALIZER与CL_TREX_JSON_DESERIALIZER
二、call transformation
DATA: lo_writer TYPE REF TO cl_sxml_string_writer.
lo_writer = cl_sxml_string_writer=>create(
type = if_sxml=>co_xt_xml10
encoding = `UTF-8` ).
CALL TRANSFORMATION id
SOURCE spfli = lt_itab
sflight = lt_itab2
RESULT XML lo_writer.
lv_xml = lo_writer->get_output( ).
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_xml
IMPORTING
output_length = lv_size
TABLES
binary_tab = lt_xml.
若是需要json格式,則可以將type的參數改為IF_SXML=>CO_XT_BINARY,若目標內表中存在非扁平結構,需要在SE11中提前定義好結構,若需要像PI結構一樣可以控制initial字段,內表在最終的XML或者文件中可見,則需要在SE80中定義結構Enterprise Services中定義。
三、cl_proxy_xml_transform=>abap_to_xml_xstring
cl_proxy_xml_transform=>abap_to_xml_xstring(
EXPORTING
abap_data = is_itab3
ddic_type = 'ZLIAN_TEST_XML' " Name of Dictionary Type
* ext_xml = ABAP_FALSE
* xml_header = 'no'
* root_element =
* svar_name = " Proxy Generation: Name of an Object in R/3
RECEIVING
xml = lv_xml ).
CATCH cx_proxy_fault. " Proxy Fault
CATCH cx_transformation_error. " General Error When Performing CALL TRANSFORMATION
注:若是多層結構,兩種方式需要建立的結構不一樣,cl_proxy_xml_transform=>abap_to_xml_xstring不會產生最內層的tablel_line,最外層的name也不會產生;也不會帶出空字段,call transformation則都相反。
四、/ui2/cl_json
JSON转 ABAP结构
TYPES: BEGIN OF t_record,
key1 TYPE string,
key2 TYPE string,
END OF t_record.
DATA: json TYPE string.
DATA: record TYPE t_record.
json = '{"key1": "VALUE1", "key2": "VALUE2"}'.
/ui2/cl_json=>deserialize( EXPORTING json = json CHANGING data = record ).
IF sy-subrc = 0. ENDIF.
ABAP结构转JSON
json = /ui2/cl_json=>serialize( data = record compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).
当然SAP还是提供了其它类来进行JSON格式的转换。
但是这种JSON的方法出来的名字会直接保存为小写,意味着KEY1/KEY2在JSON中永远都是key1/key2
{"key1":"ValuE1","key2":"VALUE2"}
怎样才可以做到{"KEy1":"ValuE1","key2":"VALUE2"}
这时候需要做到驼峰美化
DATA: BEGIN OF record,
_K_EY1 TYPE string VALUE 'ValuE1',
key2 TYPE string VALUE 'VALUE2',
END OF record.
DATA: json TYPE string.
json = /ui2/cl_json=>serialize( data = record compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).
五、CL_TREX_JSON_SERIALIZER与CL_TREX_JSON_DESERIALIZER
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 c_colon INTO l_value
语句,改写为下面的语句。就是在 <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
方法做两点变更 (网上有一个做好的图片,我就直接贴图了):
完成后,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.