Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to stringify for trailing commas #191

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions src/core/zif_ajson.intf.abap
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ interface zif_ajson
methods stringify
importing
iv_indent type i default 0
iv_trailing_comma type abap_bool default abap_false
preferred parameter iv_indent
returning
value(rv_json) type string
raising
Expand Down
Loading