Skip to content
This repository was archived by the owner on Dec 17, 2023. It is now read-only.

Commit bbca17b

Browse files
Use ABAP escape function to escape JSON (#183)
* Use ABAP escape function to escape JSON * Remove parameter prefixes Co-authored-by: Johannes Konings <[email protected]>
1 parent 17a9eaa commit bbca17b

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/zamp_converter/zcl_amp_conv_http_json.clas.abap

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,51 @@ CLASS zcl_amp_conv_http_json DEFINITION
88
INTERFACES zif_amp_converter .
99
PROTECTED SECTION.
1010
PRIVATE SECTION.
11+
12+
METHODS escape_json
13+
IMPORTING val TYPE string
14+
RETURNING VALUE(result) TYPE string.
1115
ENDCLASS.
1216

1317

1418

1519
CLASS zcl_amp_conv_http_json IMPLEMENTATION.
1620

17-
1821
METHOD zif_amp_converter~convert.
1922

2023
DATA(metric_store_groups) = metric_store.
21-
2224
SORT metric_store_groups BY metric_group.
23-
2425
DELETE ADJACENT DUPLICATES FROM metric_store_groups COMPARING metric_group.
2526

26-
27-
2827
converted_metrics = |\{|.
2928

3029
LOOP AT metric_store_groups ASSIGNING FIELD-SYMBOL(<metric_store_group>).
3130

32-
converted_metrics = converted_metrics &&
33-
|"{ sy-sysid }_{ sy-mandt }_{ <metric_store_group>-metric_scenario }_{ <metric_store_group>-metric_group }" : \{|.
34-
35-
LOOP AT metric_store ASSIGNING FIELD-SYMBOL(<metric>) WHERE metric_group = <metric_store_group>-metric_group.
36-
37-
DATA(metric_key) = <metric>-metric_key.
31+
DATA(group_key) = |{ sy-sysid }_{ sy-mandt }_{ <metric_store_group>-metric_scenario }_{ <metric_store_group>-metric_group }|.
3832

39-
REPLACE ALL OCCURRENCES OF REGEX `(["\\\/])` IN metric_key WITH `\\$0`.
40-
41-
converted_metrics = converted_metrics && |"{ metric_key }" : { <metric>-metric_value },|.
33+
converted_metrics = converted_metrics && |"{ escape_json( group_key ) }" : \{|.
4234

35+
LOOP AT metric_store ASSIGNING FIELD-SYMBOL(<metric>) WHERE metric_group = <metric_store_group>-metric_group.
36+
converted_metrics = converted_metrics && |"{ escape_json( CONV #( <metric>-metric_key ) ) }" : { <metric>-metric_value },|.
4337
ENDLOOP.
4438

4539
converted_metrics = substring( val = converted_metrics off = 0 len = strlen( converted_metrics ) - 1 ).
46-
4740
converted_metrics = converted_metrics && |\},|.
4841

4942
ENDLOOP.
43+
5044
IF sy-subrc = 0.
5145
converted_metrics = substring( val = converted_metrics off = 0 len = strlen( converted_metrics ) - 1 ).
5246
ENDIF.
5347

5448
converted_metrics = converted_metrics && |\}|.
5549

56-
5750
content_type = 'application/json'.
5851

5952
ENDMETHOD.
53+
54+
55+
METHOD escape_json.
56+
result = escape( val = val format = cl_abap_format=>e_json_string ).
57+
ENDMETHOD.
6058
ENDCLASS.

0 commit comments

Comments
 (0)