Advertisement
rplantiko

Built-in JSON / ABAP transformer

Jan 23rd, 2013
4,960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 10.10 KB | None | 0 0
  1. *&---------------------------------------------------------------------*
  2. *& Report  ZZ_TEST_JSON
  3. *&---------------------------------------------------------------------*
  4.  
  5. * Testing ABAP's built-in JSON Parser and JSON Builder
  6.  
  7. report  zz_test_json.
  8.  
  9. parameters:
  10. * Convert ABAP data into a JSON string
  11.             p_tojsns radiobutton group 001 default 'X',
  12. * Convert ABAP data into a JSON string and visualize them as HTML doc
  13.             p_tojson radiobutton group 001,
  14. * Convert JSON into an XML representation (JSONXML)
  15.             p_toxml  radiobutton group 001,
  16. * Convert JSON into ABAP (fixed test structure IHTTPNVP)
  17.             p_toabap radiobutton group 001.
  18.  
  19.  
  20. start-of-selection.
  21.   perform start.
  22.  
  23. * ---
  24. form start.
  25.  
  26.   if     p_tojson = 'X'.
  27.     perform abap_to_json.
  28.   elseif p_tojsns = 'X'.
  29.     perform abap_to_json_string.
  30.   elseif p_toabap = 'X'.
  31.     perform json_to_abap.
  32.   else.
  33.     perform json_to_jsonxml.
  34.   endif.
  35.  
  36. endform.                    "start
  37.  
  38.  
  39. *&---------------------------------------------------------------------*
  40. *&      Form  json_to_jsonxml
  41. *&---------------------------------------------------------------------*
  42. form json_to_jsonxml.
  43.  
  44.   data: lv_json type string,
  45.         lo_xmldoc type ref to if_ixml_document.
  46.  
  47.   "JSON to JSON-XML
  48.   lv_json = `{"TEXT":{"NAME":"JSON","VALUE":"cool!"}}`.
  49.   perform edit_string using 'JSON-Daten' changing lv_json.
  50.  
  51.   lo_xmldoc = cl_ixml=>create( )->create_document( ).
  52.   call transformation id source xml lv_json
  53.                          result xml lo_xmldoc.
  54.   call function 'SDIXML_DOM_TO_SCREEN'
  55.     exporting
  56.       document    = lo_xmldoc
  57.       title       = 'JSON-Daten in JSON-XML-Präsentierung'
  58.     exceptions
  59.       no_document = 1
  60.       others      = 2.
  61.  
  62. endform.                    "json_to_abapxml
  63.  
  64. *&---------------------------------------------------------------------*
  65. *&      Form  json_to_abap
  66. *&---------------------------------------------------------------------*
  67. form json_to_abap.
  68.  
  69.   data: lv_json type string,
  70.         ls_text type ihttpnvp,
  71.         lo_xmldoc type ref to if_ixml_document.
  72.  
  73.   "JSON to ABAP
  74.   lv_json = `{"TEXT":{"NAME":"JSON","VALUE":"cool!"}}`.
  75.   perform edit_string using 'JSON-Daten' changing lv_json.
  76.  
  77.   call transformation id source xml lv_json
  78.                          result text = ls_text.
  79.  
  80.   call function 'RS_COMPLEX_OBJECT_EDIT'
  81.     exporting
  82.       object_name          = 'TEXT'
  83.       mode                 = space
  84.     changing
  85.       object               = ls_text
  86.     exceptions
  87.       object_not_supported = 1
  88.       others               = 2.
  89.  
  90. endform.                    "json_to_abap
  91.  
  92. *&---------------------------------------------------------------------*
  93. *&      Form  abap_to_json
  94. *&---------------------------------------------------------------------*
  95. form abap_to_json.
  96.  
  97.   data: lv_data type ref to data,
  98.         lv_json type xstring,
  99.         lo_writer type ref to cl_sxml_string_writer,
  100.         lo_xmldoc type ref to if_ixml_document,
  101.         lv_xml type string.
  102.  
  103.   field-symbols: <lv_data> type any.
  104.  
  105.   perform get_abap_data changing lv_data.
  106.   check lv_data is bound.
  107.   assign lv_data->* to <lv_data>.
  108.  
  109.  
  110.   lo_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
  111.   call transformation id source root = <lv_data>
  112.                          result xml lo_writer.
  113.   lv_json = lo_writer->get_output( ).
  114.  
  115.  
  116.   perform display_as_html using lv_json.
  117.  
  118.  
  119. endform.                    "abap_to_json
  120.  
  121. *&---------------------------------------------------------------------*
  122. *&      Form  abap_to_json_string
  123. *&---------------------------------------------------------------------*
  124. form abap_to_json_string.
  125.  
  126.   data: lv_data type ref to data,
  127.         lv_json type string,
  128.         lo_writer type ref to cl_sxml_string_writer,
  129.         lo_xmldoc type ref to if_ixml_document,
  130.         lv_xml type string.
  131.  
  132.   field-symbols: <lv_data> type any.
  133.  
  134.   perform get_abap_data changing lv_data.
  135.   check lv_data is bound.
  136.   assign lv_data->* to <lv_data>.
  137.  
  138.   lo_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
  139.   call transformation id source root = <lv_data>
  140.                          result xml lo_writer.
  141.  
  142.   lv_json = cl_abap_codepage=>convert_from(  lo_writer->get_output( ) ).
  143.  
  144.   call function 'Z_DISPLAY_STRING'
  145.     exporting
  146.       iv_string = lv_json
  147.       iv_title  = 'JSON-Ergebnis'.
  148.  
  149.  
  150.  
  151. endform.                    "abap_to_json_string
  152.  
  153.  
  154.  
  155. *&---------------------------------------------------------------------*
  156. *&      Form  get_abap_data
  157. *&---------------------------------------------------------------------*
  158. form get_abap_data changing ev_data type ref to data.
  159.  
  160.   data: lt_abap type stringtab,
  161.         lv_repid type repid,
  162.         lv_mess type string.
  163.  
  164.   field-symbols: <lv_data> type any.
  165.  
  166.   perform set_test_abap changing lt_abap.
  167.   perform edit_text using 'ABAP Daten erzeugen' changing lt_abap.
  168.  
  169.   generate subroutine pool lt_abap name lv_repid
  170.     message lv_mess.
  171.   if sy-subrc ne 0.
  172.     message lv_mess type 'I'.
  173.     return.
  174.   endif.
  175.  
  176.   perform get_data in program (lv_repid) changing ev_data.
  177.  
  178. endform.                    "get_abap_data
  179.  
  180.  
  181. * ---  Popup zur Eingabe von langen Texten
  182. form edit_text using iv_title type csequence
  183.                changing ct_text type stringtab.
  184.   call function 'TERM_CONTROL_EDIT'
  185.     exporting
  186.       titel          = iv_title
  187.     tables
  188.       textlines      = ct_text
  189.     exceptions
  190.       user_cancelled = 1
  191.       others         = 2.
  192.   if sy-subrc eq 1.
  193.     leave to screen 0.
  194.   elseif sy-subrc ne 0.
  195.     message id sy-msgid type 'A' number sy-msgno
  196.       with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  197.   endif.
  198. endform.                    "edit_text
  199.  
  200. *&---------------------------------------------------------------------*
  201. *&      Form  edit_string
  202. *&---------------------------------------------------------------------*
  203. form edit_string using iv_title type csequence
  204.                  changing cv_string type string.
  205.  
  206.   data: lt_lines type stringtab.
  207.  
  208.   field-symbols: <lv_line> type string.
  209.  
  210.   split cv_string at cl_abap_char_utilities=>cr_lf into table lt_lines.
  211.  
  212.   perform edit_text using iv_title changing lt_lines.
  213.  
  214.   clear cv_string.
  215.   loop at lt_lines assigning <lv_line>.
  216.     concatenate cv_string <lv_line> into cv_string
  217.       separated by cl_abap_char_utilities=>cr_lf.
  218.   endloop.
  219.  
  220. endform.                    "edit_string
  221.  
  222. define _append.
  223.   append &2 to &1.
  224. end-of-definition.
  225.  
  226. *&---------------------------------------------------------------------*
  227. *&      Form  set_test_abap
  228. *&---------------------------------------------------------------------*
  229. form set_test_abap changing ct_abap type stringtab.
  230.  
  231.   _append ct_abap :
  232.     `program data_getter.`,
  233.     `types: begin of ty_data,`,
  234.     `         test1 type i,`,
  235.     `         test2 type ihttpnvp,`,
  236.     `         test3 type string,`,
  237.     `         end of ty_data.`,
  238.     `data: gs_data type ty_data.`,
  239.     `form get_data changing ev_data type ref to data.`,
  240.     `  gs_data-test1 = 17.`,
  241.     `  gs_data-test2-name = 'JSON'.`,
  242.     `  gs_data-test2-value = 'cool'.`,
  243.     `  gs_data-test3 = 'Happy Go Lucky'.`,
  244.     `  get reference of gs_data into ev_data.`,
  245.     `endform.`.
  246.  
  247. endform.                    "set_test_abap
  248.  
  249. *&---------------------------------------------------------------------*
  250. *&      Form  display_as_xhtml
  251. *&---------------------------------------------------------------------*
  252. data: go_cont type ref to cl_gui_custom_container,
  253.       go_html type ref to cl_gui_html_viewer,
  254.       go_doc  type ref to if_ixml_document.
  255. *&---------------------------------------------------------------------*
  256. *&      Form  display_as_xhtml
  257. *&---------------------------------------------------------------------*
  258. form display_as_html using iv_json type xstring.
  259.  
  260.  
  261. * Transform into interactive HTML presentation
  262.   go_doc = cl_ixml=>create( )->create_document( ).
  263.   call transformation sjson2html source xml iv_json
  264.                          result xml go_doc.
  265.  
  266. * Create new Custom Container, new HTML viewer
  267.   clear go_cont.
  268.  
  269.   call screen 100
  270.     starting at 10 10
  271.     ending at 80 20.
  272.  
  273. endform.                    "display_as_html
  274.  
  275.  
  276. *&---------------------------------------------------------------------*
  277. *&      Form  status_0100
  278. *&---------------------------------------------------------------------*
  279. form status_0100.
  280.  
  281.   data: lt_text type tttext255,
  282.         lv_url type text255,
  283.         lv_xml type string.
  284.  
  285.   if go_cont is initial.
  286.     create object go_cont
  287.       exporting
  288.         container_name = 'CONT'.
  289.     create object go_html
  290.       exporting
  291.         parent = go_cont.
  292.     call transformation id source xml go_doc
  293.                            result xml lv_xml.
  294.     replace all occurrences of '&lt;' in lv_xml with '<'.
  295.     replace all occurrences of '&gt;' in lv_xml with '>'.
  296.     call function 'Z_SPLIT_STRING_TO_TABLE'
  297.       exporting
  298.         iv_string = lv_xml
  299.         iv_length = 255
  300.       importing
  301.         et_table  = lt_text.
  302.     go_html->load_data( importing assigned_url = lv_url
  303.                         changing data_table = lt_text ).
  304.     go_html->show_url( lv_url ).
  305.   endif.
  306.   set pf-status 'POPU' of program 'SAPLSPO1'.
  307. endform.                    "status_0100
  308.  
  309. *&---------------------------------------------------------------------*
  310. *&      Form  user_command_0100
  311. *&---------------------------------------------------------------------*
  312. form user_command_0100.
  313.   case sy-ucomm.
  314.     when 'CANC'.
  315.       leave to screen 0.
  316.   endcase.
  317. endform.                    "user_command_0100
  318.  
  319. *----------------------------------------------------------------------*
  320. *  MODULE status_0100 OUTPUT
  321. *----------------------------------------------------------------------*
  322. module status_0100 output.
  323.   perform status_0100.
  324. endmodule.                    "status_0100 OUTPUT
  325. *----------------------------------------------------------------------*
  326. *  MODULE user_command_0100
  327. *----------------------------------------------------------------------*
  328. module user_command_0100.
  329.   perform user_command_0100.
  330. endmodule.                    "user_command_0100
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement