Reputation: 1712
I am currently developing a ASP.NET WebAPI using JSON.NET. I am looking to reduce traffic and want to ignore certain properties of my models for serialization, i.e. I don't want to return them in my JSON response, but I want to accept them when they are passed to my endpoint.
Example class
public class User {
public int Id { get; set; }
public string Name { get; set; }
public string Role { get; set; }
}
Use-cases
Problem
When I use the JsonIgnore attribute from JSON.NET, the property is ignored entirely. It is not serialized for my response, but the prop of my User is null, when I post the JSON User to my endpoint.
Is there a way to ignore a prop only for serialization?
Thank you in advance!
Upvotes: 0
Views: 991
Reputation: 307
Use this and pass null
when you want to ignore it!
[JsonProperty("property_name", NullValueHandling=NullValueHandling.Ignore)]
Upvotes: 0
Reputation: 15291
That's exactly what Data Transfer Objects are for. You should create different DTOs for different purposes (GET/POST).
Upvotes: 2