sbcgua / ajson

Yet another json parser serializer for ABAP
MIT License
49 stars 15 forks source link

Kind of resorting when merging objects #177

Closed albertmink closed 5 months ago

albertmink commented 5 months ago

Kind of resorting for example

data(ajson_1) = zcl_ajson=>parse( iv_json            = `{"descriptions":  {"methods": [ { "name": "METH1", "description": "Sonne"} ] } }`
                                  iv_keep_item_order = abap_true ).

data(ajson_2) = zcl_ajson=>parse( iv_json            = ` {"descriptions":  {"methods": [ { "name": "METH1", "parameters":[ { "name": "param2", "description": "Parameter B"} ]} ]  } }`
                                  iv_keep_item_order = abap_true ).

data(merged) = zcl_ajson_utilities=>new( )->merge( io_json_a = ajson_1
                                                   io_json_b = ajson_2 ).

data(str) = merged->stringify( iv_indent = 2 ).

Produces

{
  "descriptions": {
    "methods": [
      {
        "parameters": [
          {
            "description": "Parameter B",
            "name": "param2"
          }
        ],
        "name": "METH1",
        "description": "Sonne"
      }
    ]
  }
}

where I hoped to get

{
  "descriptions": {
    "methods": [
      {
        "name": "METH1",
        "description": "Sonne",
        "parameters": [
          {
            "description": "Parameter B",
            "name": "param2"
          }
        ]
      }
    ]
  }
}

I know, ordering does not mean much to JSON data. But can AJSON merge the both objects respecting the order?

mbtools commented 5 months ago

looks like a bug in touch_array which inserts at the top (pos 0). does the following work for you?

  method zif_ajson~touch_array.
...
      if ms_opts-keep_item_order = abap_true.
        if ls_deleted_node is not initial.
          ls_new_node-order = ls_deleted_node-order.
        else.
          ls_new_node-order = lr_parent->children.
        endif.
      endif.
...
  endmethod.
sbcgua commented 5 months ago

Hmm, I would actually expect:

{
  "descriptions": {
    "methods": [
      { "name": "METH1", "description": "Sonne"},
      { "name": "METH1", "parameters":[ { "name": "param2", "description": "Parameter B"} ]}
    ]
  }
}

Because the arrays have no keys

@mbtools Marc what do you think ? If I remember well you contributed the merge, what was the initial design

mbtools commented 5 months ago

"name": "METH1" already exists so it adds to it. it just needs the fix to put it into the correct position (at the end with keep order)

sbcgua commented 5 months ago

OK, no strong opinion on this. PR would be welcomed!