Skip to content
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

How to ignore properties on model if I'm not using Newtonsoft.Json #2316

Closed
hez2010 opened this issue Jul 19, 2019 · 4 comments
Closed

How to ignore properties on model if I'm not using Newtonsoft.Json #2316

hez2010 opened this issue Jul 19, 2019 · 4 comments

Comments

@hez2010
Copy link

hez2010 commented Jul 19, 2019

In ASP.NET Core 3.0, Newtonsoft.Json has been removed from Microsoft.AspNetCore.App, so there's no Newtonsoft.Json.JsonIgnore by default anymore. Instead, it uses System.Text.Json by default.

How can I ignore properties on model if I'm not using Newtonsoft.Json? Is there any way to config a custom ignore attribute?

@RicoSuter
Copy link
Owner

Ref: #2243

@dazinator
Copy link

dazinator commented Sep 11, 2019

System.Text.Json and Newtonsoft both have their own ignore attributes. This the System.Text.Json equivalent attribute: https://docs.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonignoreattribute?view=netcore-3.0

So if you use them, you are coupled with that particular serialiser. No doubt when #2243 is done, you'd opt to use System.Text.Json and then all your generated model classes will have the appropriate attribute rather than newtonsofts.

However Newtonsoft also honours DataContract's i.e System.Runtime.Serialisation attributes - which is awesome as DataContracts are a way that models can opt properties into serialisation in a way that doesn't couple them with a particular serialiser. However microsoft have no plans to extend System.Text.Json to support System.Runtime.Serialisation data contract attributes - which imho is a shame. https://github.com/dotnet/corefx/issues/38758

If Microsoft did do that, it would be possible for tools like NSwag to generate less serialiser specific code, as models could use data contracts honored by a variety of serialisers. It would also be possible for libraries in the .NET ecosystem that provide serialisable models, to avoid passing a dependency on a particular serialiser onto their consumers.

@eldortemirov
Copy link

How about the attribute BindNeverAttribute from assembly Microsoft.AspNetCore.Mvc.Core? [BindNever] ignores the properties on the model. Give it a try!

@RicoSuter
Copy link
Owner

We probably need to set the property to ignored when the System.Text.Json ignore attribute is on the property:

https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema/Generation/SystemTextJsonUtilities.cs#L56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants