-
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
System.Text.Json.JsonSerializer doesn't serialize properties from derived classes #31742
Comments
The serializer serializes using whatever type information you gave it. Static typing on serialization prevents accidental data disclosure from derived types that didn't understand they were being serialized. If you really want polymorphic serialization, you can accomplish it in one of three different ways:
If you just have code like |
Hi Bartonjs. ` namespace NetCoreSerializerProblem
} |
I neglected to notice that my first bullet the generic specifier got eaten as a bad HTML tag, it should have been If you want "deeply polymorphic" you need to serialize as object. Any other type, including getting the type by generic inference, is intentionally static typed, not polymorphic. |
Do you mean
I did that and get the same result
|
@mauricio-bv No, literally the word "object", as in |
Oh. I see. Still, I tried that with the same result
Still getting the same result (no Specs property)
|
Anyone that can help on my last message? |
I have the same problem.the inherit property can not be transfered to web front.
//api request result expect |
I think it makes perfect sense to have the behavior of the serializer changed especially by considering the issue that @bailei1987 reported. How would serializer behave in signalR context when a polymorphic object is expected? Basically a simple JsonSerializer.Serialize(value, ...) should serialize an object properly without having to be worried about its polymorphism. /cc @bartonjs @terrajobst |
I agree with your statement, but I think I have to fundamentally disagree with your implied conclusion. Statically typed serialization is more secure (you can understand what data is written ahead of time, so no "I didn't remember this object was serialized and this property shouldn't have been" information disclosure) and able to be pregenerated for much more optimal code. I'm willing to concede there should be an option to turn it on, but it should neither be the only behavior, nor the default. |
Serialization goes well beyond sending data through the web and a good library should not limit polymorphism/SOLID principles, specially in .Net 3.0 that is a multiplatform framework. I am trying to move to the .Net core 3.0 serializer from Newtonsoft.JSon (which serializes perfectly deep polymorphic cobjects) because it is the recommended serializer for .Net Core 3.0, but I can't compromise my SOLID pattern/Interfaces Segregation. |
I think this would be acceptable too. Bottom line something that can turn on and automatically serialize a derived class's object without having to explicitly specify the type. |
I have a similar problem. I have a class that contains some properties that are classes being leafs in a class hierarchy. These properties are not being serialized at all with System.Text.Json. My code is working perfectly right in .NET 4.7.2 with Newtonsoft.Json being used. When migrating to .NET core 3.1 using System.Text.Json, I don't get the same result passed back to the client. And it is the Json serialization that is the problem. Tested by manually converting to Json before passing my result object back into the pipeline. |
Closing as duplicate of #29937. We only offer polymorphic support for The exception for other types is the root (with the non-generic |
Given
If we then do
We get
Despite using object as the serialization type |
@sake402 I couldn't get your repro to compile: https://dotnetfiddle.net/kLEyqt |
@layomia Please check again here. |
@sake402 you should use: |
I am trying to use the System.Text.Json serialization libray, but I can't get it is serialize it as I used to with Newtonson. Properties in derived classes or Interfaces are not serializing. This issue has also been asked and explained in the link below.
Link
Is there any serialization option I should set for proper serialization?
The text was updated successfully, but these errors were encountered: