sbcgua / ajson

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

Serialization of custom object instances #119

Closed jrodriguez-rc closed 1 year ago

jrodriguez-rc commented 2 years ago

Custom JSON object. Interface ZIF_AJSON_OBJECT

Serializing objects with the interface zif_ajson_object will retrieve the object content at the moment of setting data to zcl_ajson instance.

Example

class zcl_custom_object definition final.

  public section.
    interfaces zif_ajson_object.

endclass.

class zcl_custom_object implementation.

  method zif_ajson_object~retrieve_content.

    data:
      begin of ls_content,
        field1 type string,
        field2 type string,
      end of ls_content.

    data:
      lo_ajson type ref to zcl_ajson.

    ls_content-field1 = 'Value1'.
    ls_content-field2 = 'Value2'.

    lo_ajson = zcl_ajson=>create_empty( ).

    lo_ajson->set( iv_path = '' iv_val = ls_content ).

    ri_result = lo_ajson.

  endmethod.

endclass.
  method set_cutom_object.

    data ls_struc type ty_custom_object.

    ls_struc-field = 'abc'.

    create object ls_struc-object type lcl_custom_object_for_testing.

    DATA(lo_ajson) = zcl_ajson=>create_empty( ).

    lo_ajson->set( ls_struc ).

    DATA(lv_json_result) = lo_ajson->stringify( ).

  endmethod.

The result of the upper example code is this JSON

{
    "field": "abc",
    "object": {
        "field1": "Value1",
        "field2": "Value2"
    }
}
sbcgua commented 2 years ago

let's discuss in #123

jrodriguez-rc commented 1 year ago

I'll close this PR for now, I need time to resume it.

sbcgua commented 1 year ago

Sorry for no reaction for so long. I wanted to get to this more attentive for the whole autemn but couldn't find time (it is even opened in my browser for a couple of month by now ... :). From the high level: I think that in general the idea of self-serializable classes is good. But I'm a bit hesitant to include all possible (and rarely used) functionalities into ajson not to bloat it too much. I think it is already a bit on the edge :) So object serialization, if implemented within the package, should be ideally very lightweigth. But I'm open for ideas. And sorry again. please don't think it was ignoring :(