ajson icon indicating copy to clipboard operation
ajson copied to clipboard

Custom serialization/deserialization of objects

Open sbcgua opened this issue 3 years ago • 5 comments

ref: #119 (see for code example), @jrodriguez-rc Creating an issue as it might need some discussion.

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.

sbcgua avatar Aug 30 '22 09:08 sbcgua

Thoughts ...

interesting idea, but let's unroll it a bit wider.

  • there is a potential feature to enable automatic object serialization, though it can support public attrs only
  • So, a custom interface may solve this issue and serialize also private state, right ?
  • what about deserialization ? although it might not be immediately implementable we should think about it. What is the specific use case you want this feature for ? @jrodriguez-rc

Naming, I think it can be improved

  • what about zif_ajson_object -> zif_ajson_serializable
  • the methods could be then serialize/deserialize or rehydrate (but too long imho). Or maybe simply read/write. so lo_my_object->zif_ajson_serializable~read( ... ) - looks rather understandable

Design

  • what about passing json instance inside ? zif_ajson_serializable~read( li_my_json_target ). Could be a bit better for performance. But exposes existing json content ... so probably no.

Deseriazation

  • certainly a separate step ... but let's imagine
  • an object must be created before rehydration. This is an obstacle. Even if we add some kind of metadata to the output ( { ... "@object_name": "zcl_blah_blah" } ). It probably won't work with local classes. And construction may require params. Or custom initialization. At the same time interfaces in abap allow static methods, no ? Is it a direction ? Hmmm ...
  • yet it doesn't block 1st level dehydration
data lo_my ref to zcl_blah.
create object lo_my exporting ...
lo_json->to_abap( changing cv_container = lo_my ).
" OR a new method (though ... very tentative ...)
lo_my ?= lo_json->rehydrate( new zcl_blah( ) ).
  • or adding something like zif_ajson_object_factory and =>instantiate( path = '/A/-/B' ) (where - stands for array item). But ... then ajson starts to be a gas factory (over complex :)

sbcgua avatar Aug 30 '22 09:08 sbcgua

Aside from tracing/debugging, I can't think of a scenario where serializing private attributes makes sense. Do we really need this?

mbtools avatar Aug 30 '22 15:08 mbtools

Could be an elegant way to get json representation of objects. E.g. some FI or SD document and you want to send to external service in json.

Or you want to serialize an object (or array of object or ...), send the state to another abap system and rehydrate the objects there. Yet ... this looks a bit far fetched for abap.

Anyway it's just my imagination, I don't have such cases. And I would also like to keep ajson simple (it is already not that simple ... ). So I'd be glad to see the real use case too.

sbcgua avatar Aug 30 '22 16:08 sbcgua

Let me explain what the objective is for me.

I'm working on an FHIR server and my idea is to make a class for each resource and some general elements.

For example, the Patient resource uses the Identifier element which is typical for multiple resources. image

So in the Patient structure resource, I just need to set the reference to the identifier object image

Now, when I serialize the patient resource, it automatically fills the Identifier in the structure I need. image

jrodriguez-rc avatar Aug 31 '22 15:08 jrodriguez-rc

About the deserialization, I have some doubts about how to do it.

There will be static methods in the interface, but it will lose the possibility of redefinition.

With an instance method we need to instantiate first the object, and then call the method, but the constructor will need parameters.

I like the second option, but a prerequisite for the serializable class will be that the constructor won't have importing parameters.

jrodriguez-rc avatar Aug 31 '22 16:08 jrodriguez-rc