-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
PlatformNotSupportedException when attempting to serialize DirectoryInfo instance with Newtonsoft.Json #23261
Comments
We brought back binary serialization with .NET Core 2.0 but only for a limited set of types: https://github.com/dotnet/corefx/issues/19119. The ISerializable interface and the respective GetObjectData method which is called by Newtonsonft.Json is intended to be used by BinaryFormatter for binary serialization.
https://msdn.microsoft.com/de-de/library/system.runtime.serialization.iserializable(v=vs.110).aspx We don't expect it to be used by other serialization frameworks. As the GetObjectData method is public we couldn't simply remove it without breaking compatibility but "disabled" it so that casting to ISerializable is still working. |
A possible solution could be to change this line: to try
{
value.GetObjectData(serializationInfo, Serializer._context);
}
catch (PlatformNotSupportedException) { } |
Anyway is it correct that if this type did not support ISerializable, the issue would be found at compile time? I would have expected a serializer to cast dynamically. |
Correct. Newtonsonft.Json casts the object to ISerializable like that: If other serialization frameworks would cast it with an as or is operator it would either return null or false (same with pattern matching). |
@Cisien this probably isn't the info you want to hear, but you see it's a tradeoff- even where we had serialization implemented in earlier builds of .NET Core 2.0 , it wasn't all working (and as you know it wasn't in 1.x at all). If a type (even DirectoryInfo) that we don't serialize was heavily binary serialized in the .NET Framework world, and we get lots of feedback about that, we would certainly consider whether we should add it. But I recommend not using binary serialization for this -- and 3rd party serializers that do binary serialization probably should catch PNSE and try some other scheme. |
Wouldn't it make for a smoother transition to dotnet core if the interface was simply omitted from the types that are no longer supported by binary serialization? If the interface was removed, then the issue with Newtonsoft.Json wouldn't exist since they do check to see if the type is ISerializable before attempting to cast. |
I'm curious, can you show me where that happens in Newtonsoft.Json? |
It looks like this is handled in the DefaultContractResolver: |
Thanks! |
No one has ever said Catching
|
@Cisien I'm not familiar with the Newtonsoft.json code, but are you saying it walks the hierarchy of objects testing every object implements ISerializable and if any do not then it does not use binary serialization? Even if it does, testing for ISerializable is not sufficient to know whether a type is binary serializable. |
I'm not very familiar with newtonsoft.json either -- only what I've researched while tracking down this issue. @JamesNK would be the best person to answer that question. |
@Jameskn I talked offline with @stephentoub and it seems you are right. // Stephen:
|
So the issue is Json.NET is checking that a type implements ISerializable but not also checking for the SerialiazableAttribute? |
Correct :) |
I will close the issue as it seems James will implement the code suggestion on his side. If you disagree feel free to reopen. |
I think I may do it. Unfortunately it is a breaking change so I'll put some thought into it first. |
I created test Asp.Net Core 2.0 project and added following line action inside Values controller It raised following exception I know that it worked in previous releases of hybrid .NET Framework and Core 1.0 and 1.1 projects. Could you fix this annoying issue? Thanks. |
Hi Boris, DBNull serialization was added in the last servicing release of .NET Core 2.0. Please update to the latest version. |
Hi Viktor, Where can I find the last servicing release of .NET Core 2.0? |
cc @danmosemsft how to obtain the latest servicing release? |
Cross-posting with JamesNK/Newtonsoft.Json#1404 to demonstrate the pain this can cause with 3rd party libraries that have historically relied on this behavior vs simply removing the API which would bring attention to the problem immediately, instead of at runtime.
Please consider removing these APIs in the future
Source/destination types
Source/destination JSON
None/serialization fails.
Expected behavior
Serialization of the DirectoryInfo object to function how it works on .net 4.x
Actual behavior
Appears to be related to a change made in the .net core implementation to no longer support serializing several types.
dotnet/corefx#20220
Steps to reproduce
Other Observations
SerializerSettings.Error = (s, e) => e.ErrorContext.Handled = true;
does not handle this exceptionsetting
SerializerSettings.ContractResolver = new DefaultContractResolver { IgnoreSerializableInterface = true };
does not change this behaviorThe text was updated successfully, but these errors were encountered: