ABAP 函数入参/出参格式转换JSON/XML 格式

*&---------------------------------------------------------------------*
*& Report YTEST_JSON
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT yjson.

DATA: gt_params   TYPE STANDARD TABLE OF rfc_fint_p,
      gt_paramtab TYPE abap_func_parmbind_tab,
      gt_exceptab TYPE abap_func_excpbind_tab,
      gv_xstring  TYPE xstring.

SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME.
  PARAMETERS p_name TYPE rs38l_fnam OBLIGATORY MATCHCODE OBJECT sfunc_modules.
  SELECTION-SCREEN SKIP 1.
  SELECTION-SCREEN COMMENT 1(36) TEXT-003.
  SELECTION-SCREEN BEGIN OF LINE.
    PARAMETERS p_json RADIOBUTTON GROUP g1 MODIF ID m1 DEFAULT 'X'
                     USER-COMMAND uc1.
    SELECTION-SCREEN COMMENT 3(36) TEXT-001 FOR FIELD p_json.
    PARAMETERS p_xml RADIOBUTTON GROUP g1 MODIF ID m1 .
    SELECTION-SCREEN COMMENT 43(36) TEXT-002 FOR FIELD p_xml.
  SELECTION-SCREEN END OF LINE.
  SELECTION-SCREEN SKIP 1.
  SELECTION-SCREEN COMMENT 1(36) TEXT-005.
  PARAMETERS p_displ RADIOBUTTON GROUP g2 MODIF ID m2 DEFAULT 'X'
                     USER-COMMAND uc2.
  PARAMETERS p_dload RADIOBUTTON GROUP g2 MODIF ID m2.
  SELECTION-SCREEN SKIP 1.
  PARAMETERS p_save TYPE c AS CHECKBOX.
SELECTION-SCREEN END OF BLOCK blk1.

INITIALIZATION.

  %_p_name_%_app_%-text = '函数名'.
  %_p_displ_%_app_%-text = '仅显示'.
  %_p_dload_%_app_%-text = '下载'.
  %_p_save_%_app_%-text = '保存'.
  %c003003_1000 = '格式选择'.
  %f001006_1000 = 'JSON'.
  %f002008_1000 = 'XML'.
  %c005011_1000 = '选择'.


***********************************************************************
*   START-OF-SELECTION                                                *
***********************************************************************
START-OF-SELECTION.
  CLEAR: gt_params, gt_paramtab, gt_exceptab, gv_xstring.

  PERFORM build_params.
  PERFORM format_output.

  IF gv_xstring IS NOT INITIAL.
    IF p_save EQ 'X'.
      PERFORM clipboard_export.
    ENDIF.
    IF p_displ EQ 'X'.
      IF p_json = 'X'.
        cl_demo_output=>display_json( gv_xstring ).
      ELSEIF p_xml = 'X'.
        cl_demo_output=>display_xml( gv_xstring ).
      ENDIF.
    ELSEIF p_dload EQ 'X'.
      PERFORM download_data.
    ENDIF.
  ENDIF.

*&---------------------------------------------------------------------*
*& Form build_params
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM build_params .

  TYPE-POOLS: abap.
  DATA lv_defval TYPE rs38l_defo.
  DATA lv_dataname TYPE string.
  DATA lr_waref TYPE REF TO data.

  FIELD-SYMBOLS:
       TYPE any,
     TYPE any,
     TYPE ANY TABLE.

  DATA lv_len TYPE i.
  DATA lv_excnt TYPE i VALUE 1.

  DATA ls_paramline  TYPE LINE  OF abap_func_parmbind_tab.
  DATA ls_exceptline TYPE LINE  OF abap_func_excpbind_tab.
  DATA ls_params_p   TYPE rfc_fint_p.

  DEFINE remove_enclosing_quotes.
    " Remove enclosing single quotes
    IF &2 GT 1.
      SUBTRACT 1 FROM &2.
      IF &1+&2 EQ ''''.
        &1+&2 = space.
      ENDIF.
      IF &1(1) EQ ''''.
        SHIFT &1 LEFT.
      ENDIF.
      &2 = strlen( &1 ).
    ENDIF.
  END-OF-DEFINITION.


* 获取RFC的参数
  CALL FUNCTION 'RFC_GET_FUNCTION_INTERFACE_P'
    EXPORTING
      funcname      = p_name
      language      = sy-langu
    TABLES
      params_p      = gt_params
    EXCEPTIONS
      fu_not_found  = 1
      nametab_fault = 2
      OTHERS        = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 DISPLAY LIKE 'E'.
    STOP.
  ENDIF.


* Build params table
  LOOP AT gt_params INTO ls_params_p.

    UNASSIGN .
    UNASSIGN .
    UNASSIGN .
    CLEAR ls_paramline.

    CASE ls_params_p-paramclass.
* Import/Export/Changing参数
      WHEN 'I' OR 'E' OR 'C'.

        ls_paramline-name = ls_params_p-parameter.

        IF ls_params_p-paramclass = 'E'.
          ls_paramline-kind = abap_func_importing.
        ELSEIF ls_params_p-paramclass = 'I'.
          ls_paramline-kind = abap_func_exporting.
        ELSE.
          ls_paramline-kind = abap_func_changing.
        ENDIF.

        IF ls_params_p-fieldname IS INITIAL.
          lv_dataname = ls_params_p-tabname.
        ELSE.
          CONCATENATE ls_params_p-tabname ls_params_p-fieldname INTO
              lv_dataname SEPARATED BY '-'.
        ENDIF.

* Assign default values
        lv_defval = ls_params_p-default.
        IF lv_dataname IS INITIAL.
          lv_dataname = 'STRING'.
        ENDIF.
        CREATE DATA lr_waref TYPE (lv_dataname).
        ASSIGN lr_waref->* TO .
        lv_len = strlen( lv_defval ).
        remove_enclosing_quotes lv_defval lv_len.
        IF lv_defval = 'SPACE'.
           = space.
        ELSEIF lv_len > 3 AND lv_defval+0(3) = 'SY-'.
          ASSIGN (lv_defval) TO .
           = .
          UNASSIGN .
        ELSE.
          IF lv_defval IS NOT INITIAL.
             = lv_defval.
          ENDIF.
        ENDIF.
        IF ls_params_p-exid EQ 'h'.
          ASSIGN lr_waref->* TO .
          INSERT INITIAL LINE INTO TABLE  ASSIGNING .
          PERFORM extend_deep_structure CHANGING .
        ENDIF.
        UNASSIGN .
        ls_paramline-value = lr_waref.
        INSERT ls_paramline INTO TABLE gt_paramtab.
* Tables参数
      WHEN 'T'.
        ls_paramline-name = ls_params_p-parameter.
        ls_paramline-kind = abap_func_tables.
        IF ls_params_p-exid EQ 'h'.
          CREATE DATA lr_waref TYPE (ls_params_p-tabname).
        ELSE.
          CREATE DATA lr_waref TYPE STANDARD TABLE OF (ls_params_p-tabname).
        ENDIF.
        ASSIGN lr_waref->* TO .
        INSERT INITIAL LINE INTO TABLE  ASSIGNING .
        PERFORM extend_deep_structure CHANGING .
        ls_paramline-value = lr_waref.
        INSERT ls_paramline INTO TABLE gt_paramtab.

    ENDCASE.

  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form format_output
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM format_output .

  TYPE-POOLS: abap.

  DATA:
    lt_stab     TYPE abap_trans_srcbind_tab,
    ls_slin     TYPE abap_trans_srcbind,
    lr_oexcp    TYPE REF TO cx_root,
    lv_etext    TYPE string,
    lr_adata    TYPE REF TO data,
    json_writer TYPE REF TO cl_sxml_string_writer.

  FIELD-SYMBOLS  TYPE abap_func_parmbind.

  LOOP AT gt_paramtab ASSIGNING .
    ls_slin-name  = -name.
    ls_slin-value = -value.
    APPEND ls_slin TO lt_stab.
    CLEAR ls_slin.
  ENDLOOP.

  json_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).

  TRY.
      CASE 'X'.
        WHEN p_xml.
          CALL TRANSFORMATION id OPTIONS data_refs = 'embedded'
                                         initial_components = 'include'
                                 SOURCE (lt_stab)
                                 RESULT XML gv_xstring.
        WHEN p_json.
          CALL TRANSFORMATION id OPTIONS data_refs = 'embedded'
                                         initial_components = 'include'
                                 SOURCE (lt_stab)
                                 RESULT XML json_writer.
*          gv_xstring = json_writer->get_output( ).
          DATA(ls_string) = cl_abap_codepage=>convert_from( json_writer->get_output( ) ).
          "*-----------------------Begin 02.06.2021 16:03:39---------------------------------------------*
          REPLACE ALL OCCURRENCES OF |_-| IN ls_string WITH |/|.
*
*
*          REPLACE ALL OCCURRENCES OF '[{' IN ls_string WITH '{"ITEM":[{'.
*          REPLACE ALL OCCURRENCES OF '}]' IN ls_string WITH '}]}'.
          "*--------------------------End 02.06.2021 16:03:39------------------------------------------*
          gv_xstring = cl_abap_codepage=>convert_to( ls_string ).
      ENDCASE.

    CATCH cx_root INTO lr_oexcp.
      lv_etext = lr_oexcp->if_message~get_text( ).
      MESSAGE lv_etext TYPE 'S' DISPLAY LIKE 'E'.
      STOP.
  ENDTRY.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form download_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM download_data .

  DATA: lv_filename    TYPE string,
        lv_dfname      TYPE string,
        lv_path        TYPE string,
        lv_fullpath    TYPE string,
        lv_result      TYPE i,
        lv_exttype     TYPE string,
        lv_file_filter TYPE string.

  DATA(lv_bytecount) = xstrlen( gv_xstring ).
  DATA(lt_rawdata)   = cl_document_bcs=>xstring_to_solix( ip_xstring  = gv_xstring ).

  IF p_json = 'X'.
    lv_exttype = 'json'.
    lv_file_filter = 'JSON文件(*.json)'.
  ELSEIF p_xml = 'X'.
    lv_exttype = 'xml'.
    lv_file_filter = 'XML文件(*.xml)'.
  ENDIF.

  lv_dfname = p_name.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      default_extension = lv_exttype
      default_file_name = lv_dfname
      initial_directory = 'C:\'
      file_filter       = lv_file_filter
    CHANGING
      filename          = lv_filename
      path              = lv_path
      fullpath          = lv_fullpath
      user_action       = lv_result.

  IF lv_filename = ''.
    EXIT.
  ENDIF.


  CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      bin_filesize            = lv_bytecount
      filename                = lv_filename
      filetype                = 'BIN'
    CHANGING
      data_tab                = lt_rawdata
    EXCEPTIONS
      file_write_error        = 1
      no_batch                = 2
      gui_refuse_filetransfer = 3
      invalid_type            = 4
      no_authority            = 5
      unknown_error           = 6
      header_not_allowed      = 7
      separator_not_allowed   = 8
      filesize_not_allowed    = 9
      header_too_long         = 10
      dp_error_create         = 11
      dp_error_send           = 12
      dp_error_write          = 13
      unknown_dp_error        = 14
      access_denied           = 15
      dp_out_of_memory        = 16
      disk_full               = 17
      dp_timeout              = 18
      file_not_found          = 19
      dataprovider_exception  = 20
      control_flush_error     = 21
      not_supported_by_gui    = 22
      error_no_gui            = 23
      OTHERS                  = 24.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form clipboard_export
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM clipboard_export .

  DATA: lv_rc TYPE i.

  DATA(lt_rawdata)   = cl_document_bcs=>xstring_to_solix( ip_xstring  = gv_xstring ).
  CALL METHOD cl_gui_frontend_services=>clipboard_export
    IMPORTING
      data                 = lt_rawdata
*     data                 = gv_xstring
    CHANGING
      rc                   = lv_rc
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 DISPLAY LIKE 'E'.
    STOP.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form extend_deep_structure
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      <-- 
*&---------------------------------------------------------------------*
FORM extend_deep_structure CHANGING cv_data.

  FIELD-SYMBOLS:
       TYPE any,
     TYPE ANY TABLE.

  DO.
    ASSIGN COMPONENT sy-index OF STRUCTURE cv_data TO FIELD-SYMBOL().
    IF sy-subrc NE 0.
      EXIT.
    ENDIF.
    DESCRIBE FIELD  TYPE DATA(ls_type).
    IF ls_type EQ 'h'.
      ASSIGN  TO .
      INSERT INITIAL LINE INTO TABLE  ASSIGNING .
      PERFORM extend_deep_structure CHANGING .
    ENDIF.
  ENDDO.

ENDFORM.

你可能感兴趣的:(ABAP,开发语言)