I have used CustomCreationConverter in Newtonsoft.Json for many years. Nowadays, I am trying to migrate to System.Text.Json. Is there a similar approach? I just want to add something in the method "Create".
Asked
Active
Viewed 76 times
0
-
There is no ability to do that currently. Also `PopulateObject()` is not currently implemented, see [.Net Core 3.0 JsonSerializer populate existing object](https://stackoverflow.com/q/56835040/3744182). If you can describe what you need to do in your converter perhaps we can suggest workarounds. – dbc Jul 04 '22 at 12:47
-
@dbc Please check out my [repo](https://github.com/popyoung/HugeHard.JsonConfig/blob/main/HugeHard.JsonConfig/JsonConfig.cs) and jump to line 108. – Pop Young Jul 11 '22 at 10:52
-
Could you do your `SetHost()` in an [`IJsonOnDeserialized.OnDeserialized`](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.ijsonondeserialized.ondeserialized?view=net-6.0) or [`IJsonOnDeserializing.OnDeserializing`](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.ijsonondeserializing.ondeserializing) callback? – dbc Jul 11 '22 at 13:49
-
@dbc Thanks for your advice. However, there was no way to pass a parameter to the class JsonConfig when JsonSerializer.Deserialize
was called, although I implemented the interface you mentioned. Finally, I substituted a global variable Application.OpenForms for the parameter. This approach is not so elegant, and can have some latent hazards, so I am still expecting some way to optimise it. – Pop Young Jul 29 '22 at 03:00