sbcgua / ajson

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

new feature: get members ordered by field #54

Closed mbtools closed 3 years ago

mbtools commented 3 years ago
{
  "results": [
    {
      "key": "def",
      "value": 789
    },
    {
      "key": "abc",
      "value": 456
    },
    {
      "key": "ghi",
      "value": 123
    }
  ]
}
lt_members = lo_json->members( '/results' ).   " returns [1,2,3]

New option would allow to have the members sorted:

lt_members = lo_json->members( iv_path = '/results' iv_sort = "key" ).   " returns [2,1,3]

lt_members = lo_json->members( iv_path = '/results' iv_sort = "value" ).   " returns [3,2,1]
sbcgua commented 3 years ago

The feature itself is nice. I'm just not sure that it is a right place for it (well, I'm actually sure it is not ...). The target for members can be an array (your case), or an object - they probably should behave differently. The feature looks like a separate utility - kind of https://lodash.com/docs/#sortBy So I think it would fit well into zcl_ajson_utils but not directly into the members

mbtools commented 3 years ago

Right. I didn't take into consideration that there could be other types in the members. I implemented it separately by doing a slice and to_abap into an internal table. then you can sort it any way you like and not just by one field.