sbcgua / ajson

Yet another json parser serializer for ABAP
MIT License
53 stars 16 forks source link

json -> abap conversion of string to packed #179

Closed oblomov-dev closed 9 months ago

oblomov-dev commented 9 months ago

At the frontend i get a value typed as string, but i need to map it to an abap variable typed with p. Is this possible or is the exception intended?

    DATA(lv_test) = `{` &&
                    `  "string": "my string",` &&
                    `  "value":  "1.3333"` &&
                    `}`.

    DATA r TYPE REF TO zif_abapgit_ajson.
    r =  zcl_abapgit_ajson=>parse( lv_test ).
    r = r->slice( `/value` ).
    TRY.
        DATA lv_p TYPE p LENGTH 10 DECIMALS 3.
        r->to_abap(
          IMPORTING
            ev_container     = lv_p
        ).
      CATCH cx_root INTO DATA(x).
        "Unexpected timestamp format
        DATA(lv_error) = x->get_text( ).
        "workaround
        lv_p = r->mt_json_tree[ 1 ]-value.
    ENDTRY.

I think the problem is here: https://github.com/sbcgua/ajson/blob/f1f6f230e5bd67955c5176b7410d1bfcfab3d030/src/core/zcl_ajson.clas.locals_imp.abap#L990

image

But not sure how to extend it, because there is no chance to differentiate between type p and timestampl.

Originally found this issue here and work with the workaround from above at the moment: https://github.com/abap2UI5/abap2UI5/issues/975

Thank you for your help!

albertmink commented 9 months ago

Did you try to rename

DATA value TYPE p LENGTH 10 DECIMALS 3.

I remember that to_abap does the job if there is (canonical/or customized) name mapping, see https://github.com/sbcgua/ajson?tab=readme-ov-file#converting-to-abap-structure

oblomov-dev commented 9 months ago

yes same problem also with renamed variable:

    DATA(lv_test) = `{` &&
                    `  "string": "my string",` &&
                    `  "value":  "1.3333"` &&
                    `}`.

    DATA r TYPE REF TO zif_abapgit_ajson.
    r =  zcl_abapgit_ajson=>parse( lv_test ).
    r = r->slice( `/value` ).
    TRY.
        DATA value TYPE p LENGTH 10 DECIMALS 3.
        r->to_abap(
          IMPORTING
            ev_container     = value
        ).
      CATCH cx_root INTO DATA(x).
        "Unexpected timestamp format
        DATA(lv_error) = x->get_text( ).
        "workaround
        value = r->mt_json_tree[ 1 ]-value.
    ENDTRY.
mbtools commented 9 months ago

The if comparing to packed should also check for ABAP timestamp data element(s) or matching number of digits and decimals. Only then call to_timestamp.

sbcgua commented 9 months ago

Try #180

oblomov-dev commented 9 months ago

thank you for quickly fixing this! works on my side now! and i was able to replace the workaround with https://github.com/abap2UI5/abap2UI5/pull/985