@@ -1163,111 +1163,115 @@ CLASS Z_UI2_JSON IMPLEMENTATION.
11631163 ENDMETHOD .
11641164
11651165
1166- METHOD generate_int .
1167-
1168- DATA : lt_json TYPE t_t_json,
1169- mark LIKE offset ,
1170- match LIKE offset ,
1171- data_opt LIKE data ,
1172- mlen TYPE i ,
1173- lo_type TYPE REF TO cl_abap_datadescr,
1174- lt_types TYPE SORTED TABLE OF REF TO cl_abap_datadescr WITH UNIQUE KEY table_line ,
1175- lt_fields TYPE t_t_name_value.
1176-
1177- FIELD-SYMBOLS : <data> TYPE data ,
1178- <struct> TYPE data ,
1179- <json> LIKE LINE OF lt_json,
1180- <field> LIKE LINE OF lt_fields,
1181- <table> TYPE STANDARD TABLE ,
1182- <table_opt> LIKE <table> .
1183-
1184- IF length IS NOT SUPPLIED .
1185- length = strlen ( json ).
1186- ENDIF .
1187-
1188- eat_white.
1189-
1190- CASE json+offset(1 ).
1191- WHEN '{' ."result must be a structure
1192- restore_type( EXPORTING json = json length = length type_descr = so_type_t_name_value CHANGING offset = offset data = lt_fields ).
1193- IF mv_gen_optimize EQ abap_true .
1166+ METHOD generate_int .
1167+
1168+ DATA : lt_json TYPE t_t_json,
1169+ mark LIKE offset ,
1170+ match LIKE offset ,
1171+ data_opt LIKE data ,
1172+ mlen TYPE i ,
1173+ lo_type TYPE REF TO cl_abap_datadescr,
1174+ lo_table_type TYPE REF TO cl_abap_tabledescr,
1175+ lt_types TYPE SORTED TABLE OF REF TO cl_abap_datadescr WITH UNIQUE KEY table_line ,
1176+ lt_fields TYPE t_t_name_value.
1177+
1178+ FIELD-SYMBOLS : <data> TYPE data ,
1179+ <struct> TYPE data ,
1180+ <value> TYPE data ,
1181+ <json> LIKE LINE OF lt_json,
1182+ <field> LIKE LINE OF lt_fields,
1183+ <table> TYPE STANDARD TABLE ,
1184+ <table_opt> LIKE <table> .
1185+
1186+ IF length IS NOT SUPPLIED .
1187+ length = strlen ( json ).
1188+ ENDIF .
1189+
1190+ eat_white.
1191+
1192+ CASE json+offset(1 ).
1193+ WHEN '{' ."result must be a structure
1194+ restore_type( EXPORTING json = json length = length type_descr = so_type_t_name_value CHANGING offset = offset data = lt_fields ).
1195+ IF mv_gen_optimize EQ abap_true .
1196+ LOOP AT lt_fields ASSIGNING <field> .
1197+ generate_int( EXPORTING json = <field> -value CHANGING data = <field> -data ).
1198+ ENDLOOP .
1199+ generate_struct( CHANGING fields = lt_fields data = data ).
1200+ IF data IS BOUND .
1201+ ASSIGN data->* TO <struct> .
11941202 LOOP AT lt_fields ASSIGNING <field> .
1195- generate_int( EXPORTING json = <field> -value CHANGING data = <field> -data ).
1203+ ASSIGN COMPONENT sy -tabix OF STRUCTURE <struct> TO <data> .
1204+ CHECK <field> -data IS NOT INITIAL .
1205+ ASSIGN <field> -data->* TO <value> .
1206+ <data> = <value> .
11961207 ENDLOOP .
1197- generate_struct( CHANGING fields = lt_fields data = data ).
1198- IF data IS BOUND .
1199- ASSIGN data->* TO <struct> .
1200- LOOP AT lt_fields ASSIGNING <field> .
1201- ASSIGN COMPONENT sy -tabix OF STRUCTURE <struct> TO <data> .
1202- CHECK <field> -data IS NOT INITIAL .
1203- <data> = <field> -data->*.
1204- ENDLOOP .
1205- ENDIF .
1206- ELSE .
1207- generate_struct( CHANGING fields = lt_fields data = data ).
1208- IF data IS BOUND .
1209- ASSIGN data->* TO <struct> .
1210- LOOP AT lt_fields ASSIGNING <field> .
1211- ASSIGN COMPONENT sy -tabix OF STRUCTURE <struct> TO <data> .
1212- generate_int( EXPORTING json = <field> -value CHANGING data = <data> ).
1213- ENDLOOP .
1214- ENDIF .
12151208 ENDIF .
1216- WHEN '[' ."result must be a table of ref
1217- restore_type( EXPORTING json = json length = length type_descr = so_type_t_json CHANGING offset = offset data = lt_json ).
1218- CREATE DATA data TYPE ref_tab.
1219- ASSIGN data->* TO <table> .
1220- LOOP AT lt_json ASSIGNING <json> .
1221- APPEND INITIAL LINE TO <table> ASSIGNING <data> .
1222- generate_int( EXPORTING json = <json> CHANGING data = <data> ).
1223- IF mv_gen_optimize EQ abap_true AND <data> IS NOT INITIAL .
1224- lo_type ?= cl_abap_typedescr=>describe_by_data_ref( <data> ).
1225- INSERT lo_type INTO TABLE lt_types.
1226- ENDIF .
1227- ENDLOOP .
1228- IF mv_gen_optimize EQ abap_true AND lines ( lt_types ) EQ 1 .
1229- DATA (lo_table_type ) = cl_abap_tabledescr=>get( p_line_type = lo_type ).
1230- CREATE DATA data_opt TYPE HANDLE lo_table_type.
1231- ASSIGN data_opt->* TO <table_opt> .
1232- LOOP AT <table> ASSIGNING <data> .
1233- APPEND <data> ->* TO <table_opt> .
1209+ ELSE .
1210+ generate_struct( CHANGING fields = lt_fields data = data ).
1211+ IF data IS BOUND .
1212+ ASSIGN data->* TO <struct> .
1213+ LOOP AT lt_fields ASSIGNING <field> .
1214+ ASSIGN COMPONENT sy -tabix OF STRUCTURE <struct> TO <data> .
1215+ generate_int( EXPORTING json = <field> -value CHANGING data = <data> ).
12341216 ENDLOOP .
1235- data = data_opt.
1236- ENDIF .
1237- WHEN '"' ."string
1238- FIND FIRST OCCURRENCE OF REGEX so_regex_generate_type_detect IN SECTION OFFSET offset
1239- OF json MATCH LENGTH mlen.
1240- IF sy -subrc IS INITIAL .
1241- CASE mlen.
1242- WHEN 10 . " time
1243- restore_reference so_type_t.
1244- WHEN 12 . " date
1245- restore_reference so_type_d.
1246- WHEN OTHERS . " timestamp
1247- restore_reference so_type_ts.
1248- ENDCASE .
1249- ELSE .
1250- restore_reference so_type_s.
12511217 ENDIF .
1252- WHEN '-' OR '0' OR '1' OR '2' OR '3' OR '4' OR '5' OR '6' OR '7' OR '8' OR '9' . " number
1253- IF json+offset CA '.Ee' .
1254- restore_reference so_type_f.
1255- ELSEIF length GT 9 .
1256- restore_reference so_type_p.
1257- ELSE .
1258- restore_reference so_type_i.
1259- ENDIF .
1260- WHEN OTHERS .
1261- eat_bool_string.
1262- IF json+mark(match) EQ 'true' OR json+mark(match) EQ 'false' . "#EC NOTEXT
1263- offset = mark . "need to restore after eat_bool_string
1264- restore_reference so_type_b.
1265- ELSE . "null or no match
1266- CLEAR data .
1218+ ENDIF .
1219+ WHEN '[' ."result must be a table of ref
1220+ restore_type( EXPORTING json = json length = length type_descr = so_type_t_json CHANGING offset = offset data = lt_json ).
1221+ CREATE DATA data TYPE ref_tab.
1222+ ASSIGN data->* TO <table> .
1223+ LOOP AT lt_json ASSIGNING <json> .
1224+ APPEND INITIAL LINE TO <table> ASSIGNING <data> .
1225+ generate_int( EXPORTING json = <json> CHANGING data = <data> ).
1226+ IF mv_gen_optimize EQ abap_true AND <data> IS NOT INITIAL .
1227+ lo_type ?= cl_abap_typedescr=>describe_by_data_ref( <data> ).
1228+ INSERT lo_type INTO TABLE lt_types.
12671229 ENDIF .
1268- ENDCASE .
1230+ ENDLOOP .
1231+ IF mv_gen_optimize EQ abap_true AND lines ( lt_types ) EQ 1 .
1232+ lo_table_type = cl_abap_tabledescr=>get( p_line_type = lo_type ).
1233+ CREATE DATA data_opt TYPE HANDLE lo_table_type.
1234+ ASSIGN data_opt->* TO <table_opt> .
1235+ LOOP AT <table> ASSIGNING <data> .
1236+ ASSIGN <data> ->* TO <value> .
1237+ APPEND <value> TO <table_opt> .
1238+ ENDLOOP .
1239+ data = data_opt.
1240+ ENDIF .
1241+ WHEN '"' ."string
1242+ FIND FIRST OCCURRENCE OF REGEX so_regex_generate_type_detect IN SECTION OFFSET offset
1243+ OF json MATCH LENGTH mlen.
1244+ IF sy -subrc IS INITIAL .
1245+ CASE mlen.
1246+ WHEN 10 . " time
1247+ restore_reference so_type_t.
1248+ WHEN 12 . " date
1249+ restore_reference so_type_d.
1250+ WHEN OTHERS . " timestamp
1251+ restore_reference so_type_ts.
1252+ ENDCASE .
1253+ ELSE .
1254+ restore_reference so_type_s.
1255+ ENDIF .
1256+ WHEN '-' OR '0' OR '1' OR '2' OR '3' OR '4' OR '5' OR '6' OR '7' OR '8' OR '9' . " number
1257+ IF json+offset CA '.Ee' .
1258+ restore_reference so_type_f.
1259+ ELSEIF length GT 9 .
1260+ restore_reference so_type_p.
1261+ ELSE .
1262+ restore_reference so_type_i.
1263+ ENDIF .
1264+ WHEN OTHERS .
1265+ eat_bool_string.
1266+ IF json+mark(match) EQ 'true' OR json+mark(match) EQ 'false' . "#EC NOTEXT
1267+ offset = mark . "need to restore after eat_bool_string
1268+ restore_reference so_type_b.
1269+ ELSE . "null or no match
1270+ CLEAR data .
1271+ ENDIF .
1272+ ENDCASE .
12691273
1270- ENDMETHOD .
1274+ ENDMETHOD .
12711275
12721276
12731277 METHOD generate_int_ex .
0 commit comments