Advertisement
rplantiko

A JSON Builder for ABAP

Jan 25th, 2012
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 9.66 KB | None | 0 0
  1. * A JSON Builder for ABAP
  2. * Complete source code of class ZCL_JSON_BUILDER
  3. * (generated with hidden fcode SHOW_CLIF in transaction SE24)
  4. * See http://ruediger-plantiko.blogspot.com/2011/09/ein-json-builder-in-abap.html
  5. * Requires XSLT transformation http://pastebin.com/FCkhLVQp
  6.  
  7. class ZCL_JSON_BUILDER definition
  8.   public
  9.   create public .
  10.  
  11. public section.
  12. *"* public components of class ZCL_JSON_BUILDER
  13. *"* do not include other source files here!!!
  14.  
  15.   methods BUILD
  16.     importing
  17.       !IS_DATA type ZUT_DATA
  18.     exporting
  19.       !EV_JSON type STRING .
  20.   methods STRINGS_TO_ARRAY
  21.     importing
  22.       !IT_STRINGS type STRINGTAB
  23.     exporting
  24.       !EV_JSON type STRING .
  25.   methods NAMED_JSON_DATA_TO_HASH
  26.     importing
  27.       !IT_PAIRS type TIHTTPNVP
  28.     exporting
  29.       !EV_JSON type STRING .
  30.   methods NAMED_STRINGS_TO_HASH
  31.     importing
  32.       !IT_PAIRS type TIHTTPNVP
  33.     exporting
  34.       !EV_JSON type STRING .
  35.   methods JSON_DATA_TO_ARRAY
  36.     importing
  37.       !IT_JSON type STRINGTAB
  38.     exporting
  39.       !EV_JSON type STRING .
  40.   methods STRUCTURE_TO_HASH
  41.     importing
  42.       !IS_STRUCTURE type ANY
  43.     exporting
  44.       !EV_JSON type STRING .
  45. protected section.
  46. *"* protected components of class ZCL_JSON_BUILDER
  47. *"* do not include other source files here!!!
  48. private section.
  49. *"* private components of class ZCL_JSON_BUILDER
  50. *"* do not include other source files here!!!
  51.  
  52.   methods DATA_TO_ARRAY
  53.     importing
  54.       !IT_STRINGS type STRINGTAB
  55.       !IV_TYPE type ZUT_DATA-TYPE default 'S'
  56.     exporting
  57.       !EV_JSON type STRING .
  58.   methods PAIRS_TO_HASH
  59.     importing
  60.       !IT_PAIRS type TIHTTPNVP
  61.       !IV_TYPE type ZUT_DATA-TYPE default 'S'
  62.     exporting
  63.       !EV_JSON type STRING .
  64. ENDCLASS.
  65.  
  66.  
  67.  
  68. CLASS ZCL_JSON_BUILDER IMPLEMENTATION.
  69.  
  70.  
  71. * <SIGNATURE>---------------------------------------------------------------------------------------+
  72. * | Instance Public Method ZCL_JSON_BUILDER->BUILD
  73. * +-------------------------------------------------------------------------------------------------+
  74. * | [--->] IS_DATA                        TYPE        ZUT_DATA
  75. * | [<---] EV_JSON                        TYPE        STRING
  76. * +--------------------------------------------------------------------------------------</SIGNATURE>
  77. method build.
  78.  
  79. * Diese Transformation ist des Builders Kern
  80.   call transformation zabap2json
  81.     options data_refs = 'heap-or-create'
  82.     source data_root = is_data
  83.     result json = ev_json.
  84.  
  85. * Führenden Leerraum entfernen
  86.   replace regex '^\s+' in ev_json with space.
  87.  
  88. * Abschliessenden Leerraum entfernen
  89.   replace regex '\s+$' in ev_json with space.
  90.  
  91. endmethod.
  92.  
  93.  
  94. * <SIGNATURE>---------------------------------------------------------------------------------------+
  95. * | Instance Private Method ZCL_JSON_BUILDER->DATA_TO_ARRAY
  96. * +-------------------------------------------------------------------------------------------------+
  97. * | [--->] IT_STRINGS                     TYPE        STRINGTAB
  98. * | [--->] IV_TYPE                        TYPE        ZUT_DATA-TYPE (default ='S')
  99. * | [<---] EV_JSON                        TYPE        STRING
  100. * +--------------------------------------------------------------------------------------</SIGNATURE>
  101. method data_to_array.
  102.  
  103.   data: ls_data type zut_data,
  104.         lt_array type zut_array_tab.
  105.  
  106.   ls_data-type = iv_type.
  107.   loop at it_strings reference into ls_data-data.
  108.     append ls_data to lt_array.
  109.   endloop.
  110.  
  111.   ls_data-type = 'a'.
  112.   get reference of lt_array into ls_data-data.
  113.  
  114.   call method build
  115.     exporting
  116.       is_data = ls_data
  117.     importing
  118.       ev_json = ev_json.
  119.  
  120. endmethod.
  121.  
  122.  
  123. * <SIGNATURE>---------------------------------------------------------------------------------------+
  124. * | Instance Public Method ZCL_JSON_BUILDER->JSON_DATA_TO_ARRAY
  125. * +-------------------------------------------------------------------------------------------------+
  126. * | [--->] IT_JSON                        TYPE        STRINGTAB
  127. * | [<---] EV_JSON                        TYPE        STRING
  128. * +--------------------------------------------------------------------------------------</SIGNATURE>
  129. method json_data_to_array.
  130.  
  131.   call method data_to_array
  132.     exporting
  133.       it_strings = it_json
  134.       iv_type    = 'J'
  135.     importing
  136.       ev_json    = ev_json.
  137.  
  138. endmethod.
  139.  
  140.  
  141. * <SIGNATURE>---------------------------------------------------------------------------------------+
  142. * | Instance Public Method ZCL_JSON_BUILDER->NAMED_JSON_DATA_TO_HASH
  143. * +-------------------------------------------------------------------------------------------------+
  144. * | [--->] IT_PAIRS                       TYPE        TIHTTPNVP
  145. * | [<---] EV_JSON                        TYPE        STRING
  146. * +--------------------------------------------------------------------------------------</SIGNATURE>
  147. method named_json_data_to_hash.
  148.  
  149.   call method pairs_to_hash
  150.     exporting
  151.       it_pairs = it_pairs
  152.       iv_type  = 'J'
  153.     importing
  154.       ev_json  = ev_json.
  155.  
  156. endmethod.
  157.  
  158.  
  159. * <SIGNATURE>---------------------------------------------------------------------------------------+
  160. * | Instance Public Method ZCL_JSON_BUILDER->NAMED_STRINGS_TO_HASH
  161. * +-------------------------------------------------------------------------------------------------+
  162. * | [--->] IT_PAIRS                       TYPE        TIHTTPNVP
  163. * | [<---] EV_JSON                        TYPE        STRING
  164. * +--------------------------------------------------------------------------------------</SIGNATURE>
  165. method named_strings_to_hash.
  166.  
  167.   call method pairs_to_hash
  168.     exporting
  169.       it_pairs = it_pairs
  170.       iv_type  = 'S'
  171.     importing
  172.       ev_json  = ev_json.
  173.  
  174. endmethod.
  175.  
  176.  
  177. * <SIGNATURE>---------------------------------------------------------------------------------------+
  178. * | Instance Private Method ZCL_JSON_BUILDER->PAIRS_TO_HASH
  179. * +-------------------------------------------------------------------------------------------------+
  180. * | [--->] IT_PAIRS                       TYPE        TIHTTPNVP
  181. * | [--->] IV_TYPE                        TYPE        ZUT_DATA-TYPE (default ='S')
  182. * | [<---] EV_JSON                        TYPE        STRING
  183. * +--------------------------------------------------------------------------------------</SIGNATURE>
  184. method pairs_to_hash.
  185.  
  186.   data: lt_hash type zut_hash_tab,
  187.         ls_hash type zut_hash_element,
  188.         ls_data type zut_data.
  189.  
  190.   field-symbols: <ls_pair> type ihttpnvp.
  191.  
  192.   loop at it_pairs assigning <ls_pair>.
  193.     ls_hash-key = <ls_pair>-name.
  194.     get reference of <ls_pair>-value into ls_hash-data.
  195.     ls_hash-type = iv_type.
  196.     insert ls_hash into table lt_hash.
  197.   endloop.
  198.  
  199.   ls_data-type = 'h'.
  200.   get reference of lt_hash into ls_data-data.
  201.  
  202.   call method build
  203.     exporting
  204.       is_data = ls_data
  205.     importing
  206.       ev_json = ev_json.
  207.  
  208. endmethod.
  209.  
  210.  
  211. * <SIGNATURE>---------------------------------------------------------------------------------------+
  212. * | Instance Public Method ZCL_JSON_BUILDER->STRINGS_TO_ARRAY
  213. * +-------------------------------------------------------------------------------------------------+
  214. * | [--->] IT_STRINGS                     TYPE        STRINGTAB
  215. * | [<---] EV_JSON                        TYPE        STRING
  216. * +--------------------------------------------------------------------------------------</SIGNATURE>
  217. method strings_to_array.
  218.  
  219.   call method data_to_array
  220.     exporting
  221.       it_strings = it_strings
  222.       iv_type    = 'S'
  223.     importing
  224.       ev_json    = ev_json.
  225.  
  226. endmethod.
  227.  
  228.  
  229. * <SIGNATURE>---------------------------------------------------------------------------------------+
  230. * | Instance Public Method ZCL_JSON_BUILDER->STRUCTURE_TO_HASH
  231. * +-------------------------------------------------------------------------------------------------+
  232. * | [--->] IS_STRUCTURE                   TYPE        ANY
  233. * | [<---] EV_JSON                        TYPE        STRING
  234. * +--------------------------------------------------------------------------------------</SIGNATURE>
  235. method structure_to_hash.
  236.  
  237.   data: lt_fieldnames type ttfieldname,
  238.         lv_type type c,
  239.         ls_data type zut_data,
  240.         lt_hash type zut_hash_tab,
  241.         ls_hash type zut_hash_element,
  242.         lv_string type ref to string.
  243.  
  244.   field-symbols: <lv_comp> type any.
  245.  
  246.   clear ev_json.
  247.  
  248.   describe field is_structure type lv_type.
  249.   if lv_type na 'uv'.
  250.     raise exception type cx_parameter_invalid_type.
  251.   endif.
  252.  
  253.   call function 'Z_GET_FIELDNAMES'
  254.     exporting
  255.       is_data  = is_structure
  256.     importing
  257.       et_names = lt_fieldnames.
  258.  
  259.   do.
  260.     assign component sy-index of structure is_structure to <lv_comp>.
  261.     if sy-subrc ne 0.
  262.       exit.
  263.     endif.
  264.     read table lt_fieldnames into ls_hash-key index sy-index.
  265.     assert sy-subrc eq 0. " Muss klappen (da #Feldnamen = #Komponenten)
  266.  
  267.     describe field <lv_comp> type lv_type.
  268.     case lv_type.
  269.       when 'u' or 'v'.
  270.         create data lv_string.
  271.         call method structure_to_hash
  272.           exporting
  273.             is_structure = <lv_comp>
  274.           importing
  275.             ev_json      = lv_string->*.
  276.         ls_hash-type = 'J'.
  277.         ls_hash-data = lv_string.
  278.       when 'g' or 'C' or 'D' or 'T' or 'N' or 'X' or 'y'.
  279.         get reference of <lv_comp> into ls_hash-data.
  280.         ls_hash-type = 'S'.
  281.       when 'i' or 'P' or 'F'.
  282.         get reference of <lv_comp> into ls_hash-data.
  283.         ls_hash-type = 'N'.
  284.       when others.
  285.         raise exception type cx_parameter_invalid_type.
  286.     endcase.
  287.  
  288.     insert ls_hash into table lt_hash.
  289.  
  290.   enddo.
  291.  
  292.   get reference of lt_hash into ls_data-data.
  293.   ls_data-type = 'h'.
  294.  
  295.   call method build
  296.     exporting
  297.       is_data = ls_data
  298.     importing
  299.       ev_json = ev_json.
  300.  
  301. endmethod.
  302. ENDCLASS.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement