GithubHelp home page GithubHelp logo

Change JSON property Name about zjson HOT 5 CLOSED

sdfraga avatar sdfraga commented on September 28, 2024
Change JSON property Name

from zjson.

Comments (5)

se38 avatar se38 commented on September 28, 2024 1

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

from zjson.

se38 avatar se38 commented on September 28, 2024

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

from zjson.

se38 avatar se38 commented on September 28, 2024

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

from zjson.

sdfraga avatar sdfraga commented on September 28, 2024

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?

from zjson.

se38 avatar se38 commented on September 28, 2024

Implemented lower case ABAP names and merged with Master branch

from zjson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.