-
-
Notifications
You must be signed in to change notification settings - Fork 491
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
[OnSerializing] / [OnDeserialized] request #785
Comments
To rephrase what I think you’re asking for is to have an optional method called on the object during different phases of the serialization and deserialization of the object? Something like when the object is first created/start of serialization, before and after the property is serialiszed/deserialized and after it is done with serialization and desreialization of the onject? Maybe even a custom method to use for construction of the object? |
Like other libraries, a method that is called after deserialize for an instance of an object, and before serialization. |
Workaround for now would be: var deserializer = new DeserializerBuilder()
.WithNodeDeserializer(inner => new MyNodeDeserializer(inner), s => s.InsteadOf<ObjectNodeDeserializer>())
.Build();
// The Interface to put on your Instance that will be called when done Deserialize //
public interface IYamlDeserializeObject {
public void YamlDeserialize();
} // Interface //
// The Node Deserializer Class to call the Interface after Deserialize //
public class MyNodeDeserializer : INodeDeserializer {
private INodeDeserializer nodeDeserializer;
public MyNodeDeserializer(INodeDeserializer nodeDeserializer) {
this.nodeDeserializer = nodeDeserializer;
} // Constructor //
public bool Deserialize(IParser reader, Type expectedType, Func<IParser, Type, object?> nestedObjectDeserializer, out object? value) {
if (!nodeDeserializer.Deserialize(reader, expectedType, nestedObjectDeserializer, out value))
return false;
if (value is IYamlDeserializeObject ydObj)
ydObj.YamlDeserialize();
return true;
} // Function //
} // Class // You can of course do anything else at that point, like get the method that has your attribute, etc. |
While it's possible to simulate the callback behaviors with INodeDeserializer as you explained, it's kinda weird to not have it out of the box. I've spent quite some time to find this ticket while searching for a solution |
I going to put something together for this. It will probably be an interface that you can implement so it’s not a breaking change in some of the other classes and interfaces due to reflection uses to find the attributes. I’m going to do 4 methods. Onserializing, onserialized, and same for deserialized. I’ll also try and use default implementations of the methods on that interface so you will only need to implement the ones you want. Hopefully that works in the older frameworks. If not I’ll need to come up with something different, might be multiple interfaces or something. |
This will go out in the next release. That's about just over a week away. Put the attribute you want from the The I'm closing this since the work is done and just waiting to go out. |
Is your feature request related to a problem? Please describe.
The code below is the part that receives a callback when Serializing / Deserializing an object with Newtonsoft.Json.
If I can get a callback, I can easily process the data without a custom converter. It could be solved by creating a custom type converter, but it seems difficult to manage, such as having to create a separate class and adding it every time when initializing all serializers.
Describe the solution you'd like
Describe alternatives you've considered
Additional context
The text was updated successfully, but these errors were encountered: