Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom serialization/deserialization of objects #123

Open
sbcgua opened this issue Aug 30, 2022 · 5 comments
Open

Custom serialization/deserialization of objects #123

sbcgua opened this issue Aug 30, 2022 · 5 comments

Comments

@sbcgua
Copy link
Owner

sbcgua commented Aug 30, 2022

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
Copy link
Owner Author

sbcgua commented Aug 30, 2022

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 :)

@mbtools
Copy link
Contributor

mbtools commented Aug 30, 2022

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

@sbcgua
Copy link
Owner Author

sbcgua commented Aug 30, 2022

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.

@jrodriguez-rc
Copy link
Contributor

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants