JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.64k stars 3.24k forks source link

[Enhancement] when serializing/deserializing a netstandard lib used in .net framework/ net core #2965

Open mike7ang1rdz opened 3 weeks ago

mike7ang1rdz commented 3 weeks ago

When serializing objects in .NET Framework using Newtonsoft JSON, the serializer includes the assembly name in the type name, which is mscorlib for .NET Framework. However, when deserializing in .NET Core, the expected assembly name is System.Private.CoreLib, not mscorlib. This discrepancy causes a TypeNameHandling bug where the deserializer cannot find the type because it’s looking in the wrong assembly. This issue arises because .NET Core and .NET Framework have different core libraries, leading to different type names.

Source/destination types

// Put the types you are serializing or deserializing here

Source/destination JSON

{ "$type": "System.Object, mscorlib", "Property1": "Value1", "Property2": 123 }

Expected behavior

it should use TypeForwardedFrom attribute to handle this problem automatically without having to use jsonserializesettings

Actual behavior

throws exception because the assembly wasn't found.

Steps to reproduce

Deserialize the json in net.core