-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
[Feature] context for serialization #67
base: master
Are you sure you want to change the base?
Conversation
…ntext - but right before the initial onReadyCallback
…izers to better interact with deserializing function
Thanks to these changes, I was able to create a custom serializer/deserializer that keeps track on all serialized entities and automatically stores only a reference (as the ID) in case the entity has already been serialized. This avoids breaking up in-memory references during serialization and creating duplicates upon deserializing. To make that work, I had to tackle #65 as well with this custom serializer. So the serializer stores a class identifier along the serialized data and uses a dependency injection system to create instances for this class identifier upon deserialization. Using a custom serializer to support these features seems less invasive than changing I would appreciate if you accept this PR, although some internal functions are exposed. This is only done for the good, to make custom serializers integrate better with |
I’m busy recently, I would have spare time to review this feauture seriously until weekend ... I reply to this just let you know we saw your nice works. 👍 |
@nros I need exactly this, along with the support for containers of polymorphic objects. I've pulled your feature branch for this, but I am new to serializr (and Javascript - using Typescript actually), so a rough example of your custom serializer/deserializer maintaining the context would be very helpful for me. |
@nros to achieve what I envisage at the top level, I would like to pass a context object to the |
@spinorkit see this example: https://gist.github.com/nros/7791baca1927ee6b25e790da76544d99 |
This is almost a year old by now. Any hope for it being merged? I have exactly the same use case - I need to serialize cyclic graph, preferably in a way compatible with json.net |
@nros @spinorkit I am happy to have a look at this, if there is still a need. |
**Update: ** I think I misunderstood the intent of this PR. I might put this suggestion in a separate issue.
serializr/src/core/deserialize.ts Line 120 in 69fed6f
serializr public api, and thus it is not re-exported by serializr |
It's been a while. The project depending on this fix has already been abondoned and replaced by a Java+Vue re-implementation. I'll do my best to update my MR and provide some test cases within the next weeks. |
deserialization uses a
Context
with all deserializers. Custom serializers become more flexible if the serialization process uses the same mechanism.purpose
support automatic creation of references with a custom serializer.
explanation
A context can be used as a place to store temporary data for serialization. A custom serializer can make use of this temporary data to keep track on all serialized objects and serialize only IDs for those, that have already been serialized. Of course, the companion custom deserializer need to know about such IDs, decode them and behave much like the deserializer of
reference()
.All this would be impossible without keeping track on all already serialized entities.