Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- * A JSON Builder for ABAP
- * Complete source code of class ZCL_JSON_BUILDER
- * (generated with hidden fcode SHOW_CLIF in transaction SE24)
- * See http://ruediger-plantiko.blogspot.com/2011/09/ein-json-builder-in-abap.html
- * Requires XSLT transformation http://pastebin.com/FCkhLVQp
- class ZCL_JSON_BUILDER definition
- public
- create public .
- public section.
- *"* public components of class ZCL_JSON_BUILDER
- *"* do not include other source files here!!!
- methods BUILD
- importing
- !IS_DATA type ZUT_DATA
- exporting
- !EV_JSON type STRING .
- methods STRINGS_TO_ARRAY
- importing
- !IT_STRINGS type STRINGTAB
- exporting
- !EV_JSON type STRING .
- methods NAMED_JSON_DATA_TO_HASH
- importing
- !IT_PAIRS type TIHTTPNVP
- exporting
- !EV_JSON type STRING .
- methods NAMED_STRINGS_TO_HASH
- importing
- !IT_PAIRS type TIHTTPNVP
- exporting
- !EV_JSON type STRING .
- methods JSON_DATA_TO_ARRAY
- importing
- !IT_JSON type STRINGTAB
- exporting
- !EV_JSON type STRING .
- methods STRUCTURE_TO_HASH
- importing
- !IS_STRUCTURE type ANY
- exporting
- !EV_JSON type STRING .
- protected section.
- *"* protected components of class ZCL_JSON_BUILDER
- *"* do not include other source files here!!!
- private section.
- *"* private components of class ZCL_JSON_BUILDER
- *"* do not include other source files here!!!
- methods DATA_TO_ARRAY
- importing
- !IT_STRINGS type STRINGTAB
- !IV_TYPE type ZUT_DATA-TYPE default 'S'
- exporting
- !EV_JSON type STRING .
- methods PAIRS_TO_HASH
- importing
- !IT_PAIRS type TIHTTPNVP
- !IV_TYPE type ZUT_DATA-TYPE default 'S'
- exporting
- !EV_JSON type STRING .
- ENDCLASS.
- CLASS ZCL_JSON_BUILDER IMPLEMENTATION.
- * <SIGNATURE>---------------------------------------------------------------------------------------+
- * | Instance Public Method ZCL_JSON_BUILDER->BUILD
- * +-------------------------------------------------------------------------------------------------+
- * | [--->] IS_DATA TYPE ZUT_DATA
- * | [<---] EV_JSON TYPE STRING
- * +--------------------------------------------------------------------------------------</SIGNATURE>
- method build.
- * Diese Transformation ist des Builders Kern
- call transformation zabap2json
- options data_refs = 'heap-or-create'
- source data_root = is_data
- result json = ev_json.
- * Führenden Leerraum entfernen
- replace regex '^\s+' in ev_json with space.
- * Abschliessenden Leerraum entfernen
- replace regex '\s+$' in ev_json with space.
- endmethod.
- * <SIGNATURE>---------------------------------------------------------------------------------------+
- * | Instance Private Method ZCL_JSON_BUILDER->DATA_TO_ARRAY
- * +-------------------------------------------------------------------------------------------------+
- * | [--->] IT_STRINGS TYPE STRINGTAB
- * | [--->] IV_TYPE TYPE ZUT_DATA-TYPE (default ='S')
- * | [<---] EV_JSON TYPE STRING
- * +--------------------------------------------------------------------------------------</SIGNATURE>
- method data_to_array.
- data: ls_data type zut_data,
- lt_array type zut_array_tab.
- ls_data-type = iv_type.
- loop at it_strings reference into ls_data-data.
- append ls_data to lt_array.
- endloop.
- ls_data-type = 'a'.
- get reference of lt_array into ls_data-data.
- call method build
- exporting
- is_data = ls_data
- importing
- ev_json = ev_json.
- endmethod.
- * <SIGNATURE>---------------------------------------------------------------------------------------+
- * | Instance Public Method ZCL_JSON_BUILDER->JSON_DATA_TO_ARRAY
- * +-------------------------------------------------------------------------------------------------+
- * | [--->] IT_JSON TYPE STRINGTAB
- * | [<---] EV_JSON TYPE STRING
- * +--------------------------------------------------------------------------------------</SIGNATURE>
- method json_data_to_array.
- call method data_to_array
- exporting
- it_strings = it_json
- iv_type = 'J'
- importing
- ev_json = ev_json.
- endmethod.
- * <SIGNATURE>---------------------------------------------------------------------------------------+
- * | Instance Public Method ZCL_JSON_BUILDER->NAMED_JSON_DATA_TO_HASH
- * +-------------------------------------------------------------------------------------------------+
- * | [--->] IT_PAIRS TYPE TIHTTPNVP
- * | [<---] EV_JSON TYPE STRING
- * +--------------------------------------------------------------------------------------</SIGNATURE>
- method named_json_data_to_hash.
- call method pairs_to_hash
- exporting
- it_pairs = it_pairs
- iv_type = 'J'
- importing
- ev_json = ev_json.
- endmethod.
- * <SIGNATURE>---------------------------------------------------------------------------------------+
- * | Instance Public Method ZCL_JSON_BUILDER->NAMED_STRINGS_TO_HASH
- * +-------------------------------------------------------------------------------------------------+
- * | [--->] IT_PAIRS TYPE TIHTTPNVP
- * | [<---] EV_JSON TYPE STRING
- * +--------------------------------------------------------------------------------------</SIGNATURE>
- method named_strings_to_hash.
- call method pairs_to_hash
- exporting
- it_pairs = it_pairs
- iv_type = 'S'
- importing
- ev_json = ev_json.
- endmethod.
- * <SIGNATURE>---------------------------------------------------------------------------------------+
- * | Instance Private Method ZCL_JSON_BUILDER->PAIRS_TO_HASH
- * +-------------------------------------------------------------------------------------------------+
- * | [--->] IT_PAIRS TYPE TIHTTPNVP
- * | [--->] IV_TYPE TYPE ZUT_DATA-TYPE (default ='S')
- * | [<---] EV_JSON TYPE STRING
- * +--------------------------------------------------------------------------------------</SIGNATURE>
- method pairs_to_hash.
- data: lt_hash type zut_hash_tab,
- ls_hash type zut_hash_element,
- ls_data type zut_data.
- field-symbols: <ls_pair> type ihttpnvp.
- loop at it_pairs assigning <ls_pair>.
- ls_hash-key = <ls_pair>-name.
- get reference of <ls_pair>-value into ls_hash-data.
- ls_hash-type = iv_type.
- insert ls_hash into table lt_hash.
- endloop.
- ls_data-type = 'h'.
- get reference of lt_hash into ls_data-data.
- call method build
- exporting
- is_data = ls_data
- importing
- ev_json = ev_json.
- endmethod.
- * <SIGNATURE>---------------------------------------------------------------------------------------+
- * | Instance Public Method ZCL_JSON_BUILDER->STRINGS_TO_ARRAY
- * +-------------------------------------------------------------------------------------------------+
- * | [--->] IT_STRINGS TYPE STRINGTAB
- * | [<---] EV_JSON TYPE STRING
- * +--------------------------------------------------------------------------------------</SIGNATURE>
- method strings_to_array.
- call method data_to_array
- exporting
- it_strings = it_strings
- iv_type = 'S'
- importing
- ev_json = ev_json.
- endmethod.
- * <SIGNATURE>---------------------------------------------------------------------------------------+
- * | Instance Public Method ZCL_JSON_BUILDER->STRUCTURE_TO_HASH
- * +-------------------------------------------------------------------------------------------------+
- * | [--->] IS_STRUCTURE TYPE ANY
- * | [<---] EV_JSON TYPE STRING
- * +--------------------------------------------------------------------------------------</SIGNATURE>
- method structure_to_hash.
- data: lt_fieldnames type ttfieldname,
- lv_type type c,
- ls_data type zut_data,
- lt_hash type zut_hash_tab,
- ls_hash type zut_hash_element,
- lv_string type ref to string.
- field-symbols: <lv_comp> type any.
- clear ev_json.
- describe field is_structure type lv_type.
- if lv_type na 'uv'.
- raise exception type cx_parameter_invalid_type.
- endif.
- call function 'Z_GET_FIELDNAMES'
- exporting
- is_data = is_structure
- importing
- et_names = lt_fieldnames.
- do.
- assign component sy-index of structure is_structure to <lv_comp>.
- if sy-subrc ne 0.
- exit.
- endif.
- read table lt_fieldnames into ls_hash-key index sy-index.
- assert sy-subrc eq 0. " Muss klappen (da #Feldnamen = #Komponenten)
- describe field <lv_comp> type lv_type.
- case lv_type.
- when 'u' or 'v'.
- create data lv_string.
- call method structure_to_hash
- exporting
- is_structure = <lv_comp>
- importing
- ev_json = lv_string->*.
- ls_hash-type = 'J'.
- ls_hash-data = lv_string.
- when 'g' or 'C' or 'D' or 'T' or 'N' or 'X' or 'y'.
- get reference of <lv_comp> into ls_hash-data.
- ls_hash-type = 'S'.
- when 'i' or 'P' or 'F'.
- get reference of <lv_comp> into ls_hash-data.
- ls_hash-type = 'N'.
- when others.
- raise exception type cx_parameter_invalid_type.
- endcase.
- insert ls_hash into table lt_hash.
- enddo.
- get reference of lt_hash into ls_data-data.
- ls_data-type = 'h'.
- call method build
- exporting
- is_data = ls_data
- importing
- ev_json = ev_json.
- endmethod.
- ENDCLASS.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement