Skip to content

Commit

Permalink
Merge pull request #180 from sbcgua/string-as-number
Browse files Browse the repository at this point in the history
String as number
  • Loading branch information
sbcgua authored Mar 3, 2024
2 parents f1f6f23 + 1ca9e4d commit 2504b31
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Legend
v1.1.11, 2024-??
------------------
* Fix touch_array with keep order (#178, @mbtools)
* Fix conversion of string to packed #179

v1.1.10, 2024-02-04
------------------
Expand Down
19 changes: 13 additions & 6 deletions src/core/zcl_ajson.clas.locals_imp.abap
Original file line number Diff line number Diff line change
Expand Up @@ -983,12 +983,19 @@ class lcl_json_to_abap implementation.

when zif_ajson_types=>node_type-string.
" TODO: check type ?
if is_node_type-type_kind = lif_kind=>date and is_node-value is not initial.
<container> = to_date( is_node-value ).
elseif is_node_type-type_kind = lif_kind=>time and is_node-value is not initial.
<container> = to_time( is_node-value ).
elseif is_node_type-type_kind = lif_kind=>packed and is_node-value is not initial.
<container> = to_timestamp( is_node-value ).
if is_node-value is not initial.
if is_node_type-type_kind = lif_kind=>date.
<container> = to_date( is_node-value ).
elseif is_node_type-type_kind = lif_kind=>time.
<container> = to_time( is_node-value ).
elseif is_node_type-dd->absolute_name = '\TYPE=TIMESTAMP'
or is_node_type-dd->absolute_name = '\TYPE=TIMESTAMPL'.
<container> = to_timestamp( is_node-value ).
elseif is_node_type-type_kind = lif_kind=>packed. " Number as a string, but not a timestamp
<container> = is_node-value.
else.
<container> = is_node-value.
endif.
else.
<container> = is_node-value.
endif.
Expand Down
28 changes: 28 additions & 0 deletions src/core/zcl_ajson.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,7 @@ class ltcl_json_to_abap definition
timestamp1 type timestamp,
timestamp2 type timestamp,
timestamp3 type timestamp,
timestamp4 type timestampl,
end of ty_complex.

methods to_abap_struc
Expand Down Expand Up @@ -1460,6 +1461,9 @@ class ltcl_json_to_abap definition
methods to_abap_time
for testing
raising cx_static_check.
methods to_abap_str_to_packed
for testing
raising cx_static_check.
endclass.

class zcl_ajson definition local friends ltcl_json_to_abap.
Expand Down Expand Up @@ -1489,6 +1493,7 @@ class ltcl_json_to_abap implementation.
lo_nodes->add( '/ |timestamp1 |str |2020-07-28T00:00:00 | ' ).
lo_nodes->add( '/ |timestamp2 |str |2020-07-28T00:00:00Z | ' ).
lo_nodes->add( '/ |timestamp3 |str |2020-07-28T01:00:00+01:00 | ' ).
lo_nodes->add( '/ |timestamp4 |str |2020-07-28T01:00:00+01:00 | ' ).

create object lo_cut.
lo_cut->to_abap(
Expand All @@ -1507,6 +1512,7 @@ class ltcl_json_to_abap implementation.
ls_exp-timestamp1 = lv_exp_timestamp.
ls_exp-timestamp2 = lv_exp_timestamp.
ls_exp-timestamp3 = lv_exp_timestamp.
ls_exp-timestamp4 = lv_exp_timestamp.

cl_abap_unit_assert=>assert_equals(
act = ls_mock
Expand Down Expand Up @@ -1574,6 +1580,28 @@ class ltcl_json_to_abap implementation.

endmethod.

method to_abap_str_to_packed.

data lo_cut type ref to lcl_json_to_abap.
data lv_act type p length 10 decimals 3.
data lo_nodes type ref to lcl_nodes_helper.

create object lo_nodes.
lo_nodes->add( ' | |str |1.3333 | ' ).

create object lo_cut.
lo_cut->to_abap(
exporting
it_nodes = lo_nodes->sorted( )
changing
c_container = lv_act ).

cl_abap_unit_assert=>assert_equals(
act = lv_act
exp = '1.333' ).

endmethod.

method to_abap_value.

data lo_cut type ref to lcl_json_to_abap.
Expand Down

0 comments on commit 2504b31

Please sign in to comment.