Skip to content

Commit

Permalink
Add option to stringify for trailing commas
Browse files Browse the repository at this point in the history
Setting `iv_trailing_comma = abap_true` adds a comma to the last node of objects and arrays. This reduces diffs when comparing JSON files, for example.

Example:

- regular

```abap
lo_json->stringify(
  iv_indent         = 2 ).
```

- with trailing commas

```abap
lo_json->stringify(
  iv_indent         = 2 
  iv_trailing_comma = abap_true ).
```


```
  • Loading branch information
mbtools committed Sep 7, 2024
1 parent d618c78 commit 3550d5c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/core/zcl_ajson.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,7 @@ CLASS zcl_ajson IMPLEMENTATION.
rv_json = lcl_json_serializer=>stringify(
it_json_tree = mt_json_tree
iv_keep_item_order = ms_opts-keep_item_order
iv_trailing_comma = iv_trailing_comma
iv_indent = iv_indent ).

endmethod.
Expand Down
9 changes: 8 additions & 1 deletion src/core/zcl_ajson.clas.locals_imp.abap
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ class lcl_json_serializer definition final create private.
it_json_tree type zif_ajson_types=>ty_nodes_ts
iv_indent type i default 0
iv_keep_item_order type abap_bool default abap_false
iv_trailing_comma type abap_bool default abap_false
returning
value(rv_json_string) type string
raising
Expand All @@ -414,6 +415,7 @@ class lcl_json_serializer definition final create private.

data mt_json_tree type zif_ajson_types=>ty_nodes_ts.
data mv_keep_item_order type abap_bool.
data mv_trailing_comma type abap_bool.
data mt_buffer type string_table.
data mv_indent_step type i.
data mv_level type i.
Expand Down Expand Up @@ -458,6 +460,7 @@ class lcl_json_serializer implementation.
lo->mt_json_tree = it_json_tree.
lo->mv_indent_step = iv_indent.
lo->mv_keep_item_order = iv_keep_item_order.
lo->mv_trailing_comma = iv_trailing_comma.
rv_json_string = lo->_stringify( ).

endmethod.
Expand Down Expand Up @@ -583,7 +586,11 @@ class lcl_json_serializer implementation.
endloop.

if mv_indent_step > 0 and lv_first_done = abap_true. " only of items were in the list
append cl_abap_char_utilities=>newline to mt_buffer.
if mv_trailing_comma = abap_true.
append gv_comma_with_lf to mt_buffer.
else.
append cl_abap_char_utilities=>newline to mt_buffer.
endif.
endif.

endmethod.
Expand Down
44 changes: 44 additions & 0 deletions src/core/zcl_ajson.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ class ltcl_serializer_test definition final

methods stringify_condensed for testing raising zcx_ajson_error.
methods stringify_indented for testing raising zcx_ajson_error.
methods stringify_with_comma for testing raising zcx_ajson_error.
methods array_index for testing raising zcx_ajson_error.
methods item_order for testing raising zcx_ajson_error.
methods simple_indented for testing raising zcx_ajson_error.
Expand Down Expand Up @@ -629,6 +630,49 @@ class ltcl_serializer_test implementation.

endmethod.

method stringify_with_comma.

data lv_act type string.
data lv_exp type string.

lv_act = lcl_json_serializer=>stringify(
it_json_tree = sample_nodes( )
iv_indent = 2
iv_trailing_comma = abap_true ).
lv_exp = sample_json( ).

lv_exp = replace(
val = lv_exp
sub = |4\n|
with = |4,\n|
occ = 0 ).
lv_exp = replace(
val = lv_exp
sub = |3\n|
with = |3,\n|
occ = 0 ).
lv_exp = replace(
val = lv_exp
sub = |"abc"\n|
with = |"abc",\n|
occ = 0 ).
lv_exp = replace(
val = lv_exp
sub = |\}\n|
with = |\},\n|
occ = 0 ).
lv_exp = replace(
val = lv_exp
sub = |]\n|
with = |],\n|
occ = 0 ).

cl_abap_unit_assert=>assert_equals(
act = lv_act
exp = lv_exp ).

endmethod.

method array_index.

data lv_act type string.
Expand Down
1 change: 1 addition & 0 deletions src/core/zif_ajson.intf.abap
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ interface zif_ajson
methods stringify
importing
iv_indent type i default 0
iv_trailing_comma type abap_bool default abap_false
returning
value(rv_json) type string
raising
Expand Down

0 comments on commit 3550d5c

Please sign in to comment.