You don't need to use JsonConverterAttribute, just keep your model clean and use CustomCreationConverter instead, the code is simpler:
Copypublic class SampleConverter : CustomCreationConverter<ISample>
{
public override ISample Create(Type objectType)
{
return new Sample();
}
}
Then:
Copyvar sz = JsonConvert.SerializeObject( sampleGroupInstance );
JsonConvert.DeserializeObject<SampleGroup>( sz, new SampleConverter());
Documentation: Deserialize with CustomCreationConverter
Answer from cuongle on Stack OverflowYou don't need to use JsonConverterAttribute, just keep your model clean and use CustomCreationConverter instead, the code is simpler:
Copypublic class SampleConverter : CustomCreationConverter<ISample>
{
public override ISample Create(Type objectType)
{
return new Sample();
}
}
Then:
Copyvar sz = JsonConvert.SerializeObject( sampleGroupInstance );
JsonConvert.DeserializeObject<SampleGroup>( sz, new SampleConverter());
Documentation: Deserialize with CustomCreationConverter
It is quite simple and out of the box support provided by json.net, you just have to use the following JsonSettings while serializing and Deserializing:
CopyJsonConvert.SerializeObject(graph,Formatting.None, new JsonSerializerSettings()
{
TypeNameHandling =TypeNameHandling.Objects,
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
});
and for Deserialzing use the below code:
CopyJsonConvert.DeserializeObject(Encoding.UTF8.GetString(bData),type,
new JsonSerializerSettings(){TypeNameHandling = TypeNameHandling.Objects}
);
Just take a note of the JsonSerializerSettings object initializer, that is important for you.
Should you still use Newtonsoft.Json for new projects over System.Text.Json in aspcore 3.1?
Serializing and Deserializing JSON with NewtonSoft (JSON.NET)
Newtonsoft.Json vs System.Text.Json vs Json.Net
We are forcing ourselves to use System.Text.Json where we can, not sure we have noticed major performance increases. However, there have been several compatibility issues, where the defaults provided by Newtonsoft were not supported by System.Text.Json at the time.
Once you work through things and increment your api versions to different patterns that support the System.Text.Json use cases everything works great. If I recall the guy who made Newtonsoft started working at MS, not sure if on the json codebase though.
Advantages of Newtonsoft.Json (A.k.a Json.NET)
- Mature
- Lots of configurability
- Wide range support for use cases
Disadvantages of Newtonsoft.Json
- This applies to older packages, but several MS provided packages could never make up their mind about which newtonsoft.Json version to use, and would lock versions making it incompatible with other MS packages.
Advantages of System.Text.Json
- Supposedly new and improved (performance)
- Built into ASP.NET now
- Plug and play in most cases where Newtonsoft was used previously
Disadvantages of System.Text.Json
- Support for complex cases can be difficult to find
- This is big for us, Polymorphic (De)Serialization
- Reference loop handling is weird, newtonsoft was more graceful
More on reddit.comVideos
Hello Everyone,
You all know that the default JSON serializer has been changed from Newtonsoft.Json to the native System.Text.Json when aspcore 3.0 was first introduced with a better performance and lower memory, so we all know what this means on the long run, it will go for System.Text.Json, but right now it has so many incomplete features compaired to Newtonsoft.Json, so, should you still use Newtonsoft.Json for new projects over System.Text.Json? or you go anyway with System.Text.Json as it's the future?
Thanks all
Hello, can anyone explain me the differences between this three libraries ?
Can you expose advantages and disadvantages of them ?
We are forcing ourselves to use System.Text.Json where we can, not sure we have noticed major performance increases. However, there have been several compatibility issues, where the defaults provided by Newtonsoft were not supported by System.Text.Json at the time.
Once you work through things and increment your api versions to different patterns that support the System.Text.Json use cases everything works great. If I recall the guy who made Newtonsoft started working at MS, not sure if on the json codebase though.
Advantages of Newtonsoft.Json (A.k.a Json.NET)
- Mature
- Lots of configurability
- Wide range support for use cases
Disadvantages of Newtonsoft.Json
- This applies to older packages, but several MS provided packages could never make up their mind about which newtonsoft.Json version to use, and would lock versions making it incompatible with other MS packages.
Advantages of System.Text.Json
- Supposedly new and improved (performance)
- Built into ASP.NET now
- Plug and play in most cases where Newtonsoft was used previously
Disadvantages of System.Text.Json
- Support for complex cases can be difficult to find
- This is big for us, Polymorphic (De)Serialization
- Reference loop handling is weird, newtonsoft was more graceful
json.net is just another name for newtonsoft.json
microsoft has an article detailing the differences when migrating to system.text.json:
https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to?pivots=dotnet-6-0