Skip to content

Commit

Permalink
fix json restore issue
Browse files Browse the repository at this point in the history
1, fix json restore issue for curr fields
2, add link for function module name field in ALV
  • Loading branch information
hhelibeb authored Oct 21, 2020
1 parent 1866ceb commit 99b2129
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 30 deletions.
51 changes: 24 additions & 27 deletions src/zafl_viewer.prog.abap
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,29 @@ CLASS lcl_handle_events IMPLEMENTATION.
"on_double_click

METHOD on_link_click.
PERFORM show_cell_info USING 0 row column TEXT-i06.

DATA: value TYPE string.

READ TABLE gt_log INDEX row ASSIGNING FIELD-SYMBOL(<row>).
IF sy-subrc = 0.
ASSIGN COMPONENT column OF STRUCTURE <row> TO FIELD-SYMBOL(<value>).
IF sy-subrc = 0.
value = <value>.
ENDIF.
ENDIF.

IF column = 'FNAME'.

DATA(fm_name) = CONV rs38l_fnam( value ).
SET PARAMETER ID 'LIB' FIELD fm_name.
CALL TRANSACTION 'SE37' AND SKIP FIRST SCREEN.

ELSE.

cl_demo_output=>display_json( <value> ).

ENDIF.

ENDMETHOD. "on_single_click
ENDCLASS.

Expand Down Expand Up @@ -164,7 +186,7 @@ FORM display.
DATA: lr_functions TYPE REF TO cl_salv_functions.

PERFORM set_column USING '' lr_cols 'GUID' 'GUID' .
PERFORM set_column USING '' lr_cols 'FNAME' 'Function Module' .
PERFORM set_column USING 'X' lr_cols 'FNAME' 'Function Module' .
PERFORM set_column USING '' lr_cols 'CUST_FIELD1' 'CUST_FIELD1' .
PERFORM set_column USING '' lr_cols 'CUST_FIELD2' 'CUST_FIELD2' .
PERFORM set_column USING '' lr_cols 'CUST_FIELD3' 'CUST_FIELD3' .
Expand Down Expand Up @@ -274,28 +296,3 @@ FORM process_selected_rows.
MESSAGE msg TYPE 'S'.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form SHOW_CELL_INFO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0 text
* -->P_ROW text
* -->P_COLUMN text
* -->P_TEXT_I06 text
*----------------------------------------------------------------------*
FORM show_cell_info USING i_level TYPE i
i_row TYPE i
i_column TYPE lvc_fname
i_text TYPE string.

READ TABLE gt_log INDEX i_row ASSIGNING FIELD-SYMBOL(<row>).
IF sy-subrc = 0.
ASSIGN COMPONENT i_column OF STRUCTURE <row> TO FIELD-SYMBOL(<value>).
IF sy-subrc = 0.
cl_demo_output=>display_json( <value> ).
ENDIF.

ENDIF.

ENDFORM.
2 changes: 2 additions & 0 deletions src/zafl_viewer.prog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@
<TEXT_NAME>ICON_REFRESH</TEXT_NAME>
<ICON_ID>@42@</ICON_ID>
<FUN_TEXT>Refresh</FUN_TEXT>
<ICON_TEXT>Refresh</ICON_TEXT>
<INFO_TEXT>Refresh</INFO_TEXT>
</RSMPE_FUNT>
<RSMPE_FUNT>
<CODE>SET_CELL</CODE>
Expand Down
146 changes: 146 additions & 0 deletions src/zcl_afl_json.clas.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
CLASS zcl_afl_json DEFINITION
PUBLIC
INHERITING FROM /ui2/cl_json
FINAL
CREATE PUBLIC .

PUBLIC SECTION.

METHODS generate_int REDEFINITION .
CLASS-METHODS generate_new IMPORTING
!json TYPE json
!pretty_name TYPE pretty_name_mode DEFAULT pretty_mode-none
!name_mappings TYPE name_mappings OPTIONAL
RETURNING
VALUE(rr_data) TYPE REF TO data .

PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.



CLASS ZCL_AFL_JSON IMPLEMENTATION.


METHOD generate_int.
TYPES: BEGIN OF ts_field,
name TYPE string,
value TYPE json,
END OF ts_field.

DATA: length TYPE i,
offset TYPE i.

DATA: lt_json TYPE STANDARD TABLE OF json WITH DEFAULT KEY,
lv_json LIKE LINE OF lt_json,
lv_comp_name TYPE abap_compname,
lt_fields TYPE SORTED TABLE OF ts_field WITH UNIQUE KEY name,
lo_type TYPE REF TO cl_abap_datadescr,
lt_comp TYPE abap_component_tab,
lt_names TYPE HASHED TABLE OF string WITH UNIQUE KEY table_line,
cache LIKE LINE OF mt_name_mappings_ex,
ls_comp LIKE LINE OF lt_comp.

FIELD-SYMBOLS: <data> TYPE any,
<struct> TYPE any,
<field> LIKE LINE OF lt_fields,
<table> TYPE STANDARD TABLE,
<cache> LIKE LINE OF mt_name_mappings_ex.

length = numofchar( json ).

eat_white.

CASE json+offset(1).
WHEN `{`."result must be a structure
restore_type( EXPORTING json = json length = length CHANGING data = lt_fields ).
IF lt_fields IS NOT INITIAL.
ls_comp-type = cl_abap_refdescr=>get_ref_to_data( ).
LOOP AT lt_fields ASSIGNING <field>.
READ TABLE mt_name_mappings_ex WITH TABLE KEY json = <field>-name ASSIGNING <cache>.
IF sy-subrc IS INITIAL.
ls_comp-name = <cache>-abap.
ELSE.
cache-json = ls_comp-name = <field>-name.
TRANSLATE ls_comp-name USING `/_:_~_._-_`. " remove characters not allowed in component names
IF mv_pretty_name EQ pretty_mode-camel_case OR mv_pretty_name EQ pretty_mode-extended.
REPLACE ALL OCCURRENCES OF REGEX `([a-z])([A-Z])` IN ls_comp-name WITH `$1_$2`. "#EC NOTEXT
ENDIF.
TRANSLATE ls_comp-name TO UPPER CASE.
cache-abap = ls_comp-name = lv_comp_name = ls_comp-name. " truncate by allowed field name length
INSERT cache INTO TABLE mt_name_mappings_ex.
ENDIF.
INSERT ls_comp-name INTO TABLE lt_names.
IF sy-subrc IS INITIAL.
APPEND ls_comp TO lt_comp.
ELSE.
DELETE lt_fields.
ENDIF.
ENDLOOP.
TRY.
lo_type = cl_abap_structdescr=>create( p_components = lt_comp p_strict = c_bool-false ).
CREATE DATA rr_data TYPE HANDLE lo_type.
ASSIGN rr_data->* TO <struct>.
LOOP AT lt_fields ASSIGNING <field>.
ASSIGN COMPONENT sy-tabix OF STRUCTURE <struct> TO <data>.
<data> = generate_int( <field>-value ).
ENDLOOP.
CATCH cx_sy_create_data_error cx_sy_struct_creation.
ENDTRY.
ENDIF.
WHEN `[`."result must be a table of ref
restore_type( EXPORTING json = json length = length CHANGING data = lt_json ).
CREATE DATA rr_data TYPE TABLE OF REF TO data.
ASSIGN rr_data->* TO <table>.
LOOP AT lt_json INTO lv_json.
APPEND INITIAL LINE TO <table> ASSIGNING <data>.
<data> = generate_int( lv_json ).
ENDLOOP.
WHEN OTHERS.
IF json+offset(1) EQ `"`.
CREATE DATA rr_data TYPE string.
ELSEIF json+offset(1) CA `-0123456789.`.
IF json+offset CS '.'.
CREATE DATA rr_data TYPE decfloat34.
ELSEIF length GT 9.
CREATE DATA rr_data TYPE p.
ELSE.
CREATE DATA rr_data TYPE i.
ENDIF.
ELSEIF json+offset EQ `true` OR json+offset EQ `false`.
CREATE DATA rr_data TYPE abap_bool.
ENDIF.
IF rr_data IS BOUND.
ASSIGN rr_data->* TO <data>.
restore_type( EXPORTING json = json length = length CHANGING data = <data> ).
ENDIF.
ENDCASE.
ENDMETHOD.


METHOD generate_new.

DATA: lo_json TYPE REF TO zcl_afl_json,
lv_json LIKE json.

lv_json = json.

REPLACE ALL OCCURRENCES OF `\r\n` IN lv_json WITH cl_abap_char_utilities=>cr_lf.
REPLACE ALL OCCURRENCES OF `\n` IN lv_json WITH cl_abap_char_utilities=>newline.
REPLACE ALL OCCURRENCES OF `\t` IN lv_json WITH cl_abap_char_utilities=>horizontal_tab.

CREATE OBJECT lo_json
EXPORTING
pretty_name = pretty_name
name_mappings = name_mappings
assoc_arrays = c_bool-true
assoc_arrays_opt = c_bool-true.

TRY .
rr_data = lo_json->generate_int( lv_json ).
CATCH cx_sy_move_cast_error.
ENDTRY.

ENDMETHOD.
ENDCLASS.
25 changes: 25 additions & 0 deletions src/zcl_afl_json.clas.locals_imp.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations

DEFINE eat_white.
while_offset_cs sv_white_space.
END-OF-DEFINITION.

DEFINE while_offset_cs.
* >= 7.02 alternative
* pos = find_any_not_of( val = json sub = &1 off = offset ).
* if pos eq -1. offset = length.
* else. offset = pos. endif.

* < 7.02
while offset < length.
find first occurrence of json+offset(1) in &1.
if sy-subrc is not initial.
exit.
endif.
offset = offset + 1.
endwhile.
* < 7.02

END-OF-DEFINITION.
16 changes: 16 additions & 0 deletions src/zcl_afl_json.clas.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<VSEOCLASS>
<CLSNAME>ZCL_AFL_JSON</CLSNAME>
<LANGU>E</LANGU>
<DESCRIPT>Subclass of /UI2/CL_JSON</DESCRIPT>
<STATE>1</STATE>
<CLSCCINCL>X</CLSCCINCL>
<FIXPT>X</FIXPT>
<UNICODE>X</UNICODE>
</VSEOCLASS>
</asx:values>
</asx:abap>
</abapGit>
6 changes: 3 additions & 3 deletions src/zcl_afl_utilities.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ CLASS ZCL_AFL_UTILITIES IMPLEMENTATION.
CONTINUE.
ENDIF.

DATA(json_data) = /ui2/cl_json=>generate( json = <json_raw> ).
DATA(json_data) = zcl_afl_json=>generate_new( json = <json_raw> ).
ASSIGN json_data->* TO FIELD-SYMBOL(<json_data>).


Expand Down Expand Up @@ -138,8 +138,8 @@ CLASS ZCL_AFL_UTILITIES IMPLEMENTATION.
IF line_exists( data_elements[ domname = <parameter>-structure ] ).
<data_ref> = <temp>.
ELSE.
DATA(json_temp) = /ui2/cl_json=>serialize( data = <parameter_val> ).
/ui2/cl_json=>deserialize( EXPORTING json = json_temp CHANGING data = <data_ref> ).
DATA(json_temp) = zcl_afl_json=>serialize( data = <parameter_val> ).
zcl_afl_json=>deserialize( EXPORTING json = json_temp CHANGING data = <data_ref> ).
ENDIF.

GET REFERENCE OF <data_ref> INTO ptab_line-value.
Expand Down

0 comments on commit 99b2129

Please sign in to comment.