Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *&---------------------------------------------------------------------*
- *& Report ZZ_TEST_JSON
- *&---------------------------------------------------------------------*
- * Testing ABAP's built-in JSON Parser and JSON Builder
- report zz_test_json.
- parameters:
- * Convert ABAP data into a JSON string
- p_tojsns radiobutton group 001 default 'X',
- * Convert ABAP data into a JSON string and visualize them as HTML doc
- p_tojson radiobutton group 001,
- * Convert JSON into an XML representation (JSONXML)
- p_toxml radiobutton group 001,
- * Convert JSON into ABAP (fixed test structure IHTTPNVP)
- p_toabap radiobutton group 001.
- start-of-selection.
- perform start.
- * ---
- form start.
- if p_tojson = 'X'.
- perform abap_to_json.
- elseif p_tojsns = 'X'.
- perform abap_to_json_string.
- elseif p_toabap = 'X'.
- perform json_to_abap.
- else.
- perform json_to_jsonxml.
- endif.
- endform. "start
- *&---------------------------------------------------------------------*
- *& Form json_to_jsonxml
- *&---------------------------------------------------------------------*
- form json_to_jsonxml.
- data: lv_json type string,
- lo_xmldoc type ref to if_ixml_document.
- "JSON to JSON-XML
- lv_json = `{"TEXT":{"NAME":"JSON","VALUE":"cool!"}}`.
- perform edit_string using 'JSON-Daten' changing lv_json.
- lo_xmldoc = cl_ixml=>create( )->create_document( ).
- call transformation id source xml lv_json
- result xml lo_xmldoc.
- call function 'SDIXML_DOM_TO_SCREEN'
- exporting
- document = lo_xmldoc
- title = 'JSON-Daten in JSON-XML-Präsentierung'
- exceptions
- no_document = 1
- others = 2.
- endform. "json_to_abapxml
- *&---------------------------------------------------------------------*
- *& Form json_to_abap
- *&---------------------------------------------------------------------*
- form json_to_abap.
- data: lv_json type string,
- ls_text type ihttpnvp,
- lo_xmldoc type ref to if_ixml_document.
- "JSON to ABAP
- lv_json = `{"TEXT":{"NAME":"JSON","VALUE":"cool!"}}`.
- perform edit_string using 'JSON-Daten' changing lv_json.
- call transformation id source xml lv_json
- result text = ls_text.
- call function 'RS_COMPLEX_OBJECT_EDIT'
- exporting
- object_name = 'TEXT'
- mode = space
- changing
- object = ls_text
- exceptions
- object_not_supported = 1
- others = 2.
- endform. "json_to_abap
- *&---------------------------------------------------------------------*
- *& Form abap_to_json
- *&---------------------------------------------------------------------*
- form abap_to_json.
- data: lv_data type ref to data,
- lv_json type xstring,
- lo_writer type ref to cl_sxml_string_writer,
- lo_xmldoc type ref to if_ixml_document,
- lv_xml type string.
- field-symbols: <lv_data> type any.
- perform get_abap_data changing lv_data.
- check lv_data is bound.
- assign lv_data->* to <lv_data>.
- lo_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
- call transformation id source root = <lv_data>
- result xml lo_writer.
- lv_json = lo_writer->get_output( ).
- perform display_as_html using lv_json.
- endform. "abap_to_json
- *&---------------------------------------------------------------------*
- *& Form abap_to_json_string
- *&---------------------------------------------------------------------*
- form abap_to_json_string.
- data: lv_data type ref to data,
- lv_json type string,
- lo_writer type ref to cl_sxml_string_writer,
- lo_xmldoc type ref to if_ixml_document,
- lv_xml type string.
- field-symbols: <lv_data> type any.
- perform get_abap_data changing lv_data.
- check lv_data is bound.
- assign lv_data->* to <lv_data>.
- lo_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
- call transformation id source root = <lv_data>
- result xml lo_writer.
- lv_json = cl_abap_codepage=>convert_from( lo_writer->get_output( ) ).
- call function 'Z_DISPLAY_STRING'
- exporting
- iv_string = lv_json
- iv_title = 'JSON-Ergebnis'.
- endform. "abap_to_json_string
- *&---------------------------------------------------------------------*
- *& Form get_abap_data
- *&---------------------------------------------------------------------*
- form get_abap_data changing ev_data type ref to data.
- data: lt_abap type stringtab,
- lv_repid type repid,
- lv_mess type string.
- field-symbols: <lv_data> type any.
- perform set_test_abap changing lt_abap.
- perform edit_text using 'ABAP Daten erzeugen' changing lt_abap.
- generate subroutine pool lt_abap name lv_repid
- message lv_mess.
- if sy-subrc ne 0.
- message lv_mess type 'I'.
- return.
- endif.
- perform get_data in program (lv_repid) changing ev_data.
- endform. "get_abap_data
- * --- Popup zur Eingabe von langen Texten
- form edit_text using iv_title type csequence
- changing ct_text type stringtab.
- call function 'TERM_CONTROL_EDIT'
- exporting
- titel = iv_title
- tables
- textlines = ct_text
- exceptions
- user_cancelled = 1
- others = 2.
- if sy-subrc eq 1.
- leave to screen 0.
- elseif sy-subrc ne 0.
- message id sy-msgid type 'A' number sy-msgno
- with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
- endif.
- endform. "edit_text
- *&---------------------------------------------------------------------*
- *& Form edit_string
- *&---------------------------------------------------------------------*
- form edit_string using iv_title type csequence
- changing cv_string type string.
- data: lt_lines type stringtab.
- field-symbols: <lv_line> type string.
- split cv_string at cl_abap_char_utilities=>cr_lf into table lt_lines.
- perform edit_text using iv_title changing lt_lines.
- clear cv_string.
- loop at lt_lines assigning <lv_line>.
- concatenate cv_string <lv_line> into cv_string
- separated by cl_abap_char_utilities=>cr_lf.
- endloop.
- endform. "edit_string
- define _append.
- append &2 to &1.
- end-of-definition.
- *&---------------------------------------------------------------------*
- *& Form set_test_abap
- *&---------------------------------------------------------------------*
- form set_test_abap changing ct_abap type stringtab.
- _append ct_abap :
- `program data_getter.`,
- `types: begin of ty_data,`,
- ` test1 type i,`,
- ` test2 type ihttpnvp,`,
- ` test3 type string,`,
- ` end of ty_data.`,
- `data: gs_data type ty_data.`,
- `form get_data changing ev_data type ref to data.`,
- ` gs_data-test1 = 17.`,
- ` gs_data-test2-name = 'JSON'.`,
- ` gs_data-test2-value = 'cool'.`,
- ` gs_data-test3 = 'Happy Go Lucky'.`,
- ` get reference of gs_data into ev_data.`,
- `endform.`.
- endform. "set_test_abap
- *&---------------------------------------------------------------------*
- *& Form display_as_xhtml
- *&---------------------------------------------------------------------*
- data: go_cont type ref to cl_gui_custom_container,
- go_html type ref to cl_gui_html_viewer,
- go_doc type ref to if_ixml_document.
- *&---------------------------------------------------------------------*
- *& Form display_as_xhtml
- *&---------------------------------------------------------------------*
- form display_as_html using iv_json type xstring.
- * Transform into interactive HTML presentation
- go_doc = cl_ixml=>create( )->create_document( ).
- call transformation sjson2html source xml iv_json
- result xml go_doc.
- * Create new Custom Container, new HTML viewer
- clear go_cont.
- call screen 100
- starting at 10 10
- ending at 80 20.
- endform. "display_as_html
- *&---------------------------------------------------------------------*
- *& Form status_0100
- *&---------------------------------------------------------------------*
- form status_0100.
- data: lt_text type tttext255,
- lv_url type text255,
- lv_xml type string.
- if go_cont is initial.
- create object go_cont
- exporting
- container_name = 'CONT'.
- create object go_html
- exporting
- parent = go_cont.
- call transformation id source xml go_doc
- result xml lv_xml.
- replace all occurrences of '<' in lv_xml with '<'.
- replace all occurrences of '>' in lv_xml with '>'.
- call function 'Z_SPLIT_STRING_TO_TABLE'
- exporting
- iv_string = lv_xml
- iv_length = 255
- importing
- et_table = lt_text.
- go_html->load_data( importing assigned_url = lv_url
- changing data_table = lt_text ).
- go_html->show_url( lv_url ).
- endif.
- set pf-status 'POPU' of program 'SAPLSPO1'.
- endform. "status_0100
- *&---------------------------------------------------------------------*
- *& Form user_command_0100
- *&---------------------------------------------------------------------*
- form user_command_0100.
- case sy-ucomm.
- when 'CANC'.
- leave to screen 0.
- endcase.
- endform. "user_command_0100
- *----------------------------------------------------------------------*
- * MODULE status_0100 OUTPUT
- *----------------------------------------------------------------------*
- module status_0100 output.
- perform status_0100.
- endmodule. "status_0100 OUTPUT
- *----------------------------------------------------------------------*
- * MODULE user_command_0100
- *----------------------------------------------------------------------*
- module user_command_0100.
- perform user_command_0100.
- endmodule. "user_command_0100
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement