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

Change JSON property Name #27

Closed
sdfraga opened this issue Jan 19, 2021 · 5 comments
Closed

Change JSON property Name #27

sdfraga opened this issue Jan 19, 2021 · 5 comments

Comments

@sdfraga
Copy link

sdfraga commented Jan 19, 2021

Hello Uwe,
First of all thanks for all the work done in this library. Impressive!

I am trying to create the following JSON with zJSON:

{
"electronic_vault_subscription_enabled": true,
"paper_documents_distribution_enabled": true,
"electronic_documents_distribution_enabled": true,
"electronic_payslips_opted_out": true
}

Typically I create a DDIC structure and then use the CREATE_WITH_DATA method to generate the JSON.
Problem here is the length of the JSON property fields since in DDIC structure components have a max size of 30 chars.

I didn't find any method I could use to make a transformation on the property names. Is this possible to achieve with the current stage of zJSON?

Maybe the CREATE_WITH_DATA method could have have an import parameter for name_mappings ? (like it is done in constructor for /UI2/CL_JSON)

Thank you
Sérgio

@se38
Copy link
Owner

se38 commented Jan 19, 2021

Hi Sérgio,

good idea, thank you for that.
Looks like the /UI2/CL_JSON has been enhanced a lot in 7.50. There was no constructor in 7.40.
(but as always: cannot give any promisses when it will be done. Feel free to contribute)

Cheers, Uwe

@se38
Copy link
Owner

se38 commented Jan 21, 2021

Hi Sérgio,

I've implemented the changes. Can you please test? (Branch "issue_27")

Test ABAP -> JSON:

    DATA: BEGIN OF local_test,
            field_number1 TYPE string VALUE '111',
            field_number2 TYPE string VALUE '222',
            field_number3 TYPE string VALUE '333',
          END OF local_test.
*
    DATA(json) = zcl_json_document=>create_with_data(
                     data = local_test
                     name_mappings = VALUE #(
                       (
                         abap_name = 'field_number2'
                         json_name = 'ThisIsATheJSONFieldName'
                       )
                       (
                         abap_name = 'field_number3'
                         json_name = 'FieldNameThree'
                       )
                     )
                 )->get_json( ).

    cl_demo_output=>display_json( json ).

Result:

{
 "field_number1":"111",
 "ThisIsATheJSONFieldName":"222",
 "FieldNameThree":"333"
}

Test JSON -> ABAP

    DATA: BEGIN OF local_test,
            field_number1 TYPE string VALUE '111',
            field_number2 TYPE string VALUE '222',
            field_number3 TYPE string VALUE '333',
          END OF local_test.
    DATA json type string.

    json = '{' &&
           ' "field_number1":"111",' &&
           ' "ThisIsATheJSONFieldName":"222",' &&
           ' "FieldNameThree":"333"' &&
           '}'.

    DATA(json_doc) = zcl_json_document=>create_with_json(
                       json = json
                       name_mappings = VALUE #(
                         (
                           abap_name = 'field_number2'
                           json_name = 'ThisIsATheJSONFieldName'
                         )
                         (
                           abap_name = 'field_number3'
                           json_name = 'FieldNameThree'
                         )
                       )
                     ).

    TRY.
        json_doc->get_data( IMPORTING data = local_test ).
        cl_demo_output=>display( local_test ).
      CATCH zcx_json_document.
    ENDTRY.

Result:

FIELD_NUMBER1  FIELD_NUMBER2  FIELD_NUMBER3  
111            222            333    

Cheers, Uwe

@sdfraga
Copy link
Author

sdfraga commented Jan 22, 2021

Hello @se38 ,
Thanks a lot!
Did some tests and all looks good.
The only issue I found is if I send an abap_name with capital letters.. since the internals of zjson is converting everything to lower case, name_mapping table must be sent in lower case also.
Possible solution could be:

`METHOD set_name_mappings.

me->name_mappings = VALUE #( FOR <name> IN name_mappings ( 
                                                              abap_name = |{ <name>-abap_name CASE = LOWER }|
                                                              json_name = |{ <name>-json_name CASE = LOWER }| ) ).

ENDMETHOD.`

What do you think?

@se38
Copy link
Owner

se38 commented Jan 22, 2021

Good idea, will do (but with old syntax, because the class should be downward compatible down to 7.02.

@se38
Copy link
Owner

se38 commented Jan 22, 2021

Implemented lower case ABAP names and merged with Master branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants