Reputation: 13914
For my ASP.NET MVC webservice I have a model that I return as a JSON object. Now there are some properties in my model that I don't want to return.
An example model:
class Account {
public int ID { get; }
public string Username { get; }
public string Password { get; }
//... more properties
}
Let's say I only want to return the ID
and Username
properties as JSON. I am looking for a good way to filter only these properties. Changing the access modifier is not an option for me.
A solution which I could think of is creating a whitelist like below. Here I have added a DisplayName
which would be a nice thing to be able to customize, but it isn't required.
class FilterProperty
{
public string PropertyName { get; }
public string DisplayName { get; }
public FilterProperty(string propertyName, string displayName)
{
PropertyName = propertyName;
DisplayName = displayName;
}
}
class Account
{
public static FilterProperty[] Whitelist = {
new FilterProperty("ID", "accountId"),
new FilterProperty("Username", "accountName")
};
//...
}
The downside to this solution is: if I would change the name of a property, I would need to change the whitelist too.
Could I make this work or are there better solutions?
Upvotes: 0
Views: 1898
Reputation: 540
Web API uses JSON.net as default serializer.
You can add JSONIgnore attribute to skip a property.
public class Class
{
// included in JSON
public string Model { get; set; }
public DateTime Year { get; set; }
public List<string> Features { get; set; }
// ignored
[JsonIgnore]
public DateTime LastModified { get; set; }
}
Alternatively if you need to ignore most of the properties you can use opt in approach.
Use DataContract attribute on your class and then add Datamember to only the properties that you want to include
[DataContract]
public class Class
{
[DataMember]
public string Model { get; set; }
[DataMember]
public DateTime Year { get; set; }
// ignored
public List<string> Features { get; set; }
public DateTime LastModified { get; set; }
}
Upvotes: 1
Reputation: 321
If you are concerned with naming only, then use of nameof operator is one option. (.NET version dependent)
class Account
{
public static FilterProperty[] Whitelist = {
new FilterProperty(nameof(Account.ID), "accountId"),
new FilterProperty(nameof(Account.Username), "accountName")
};
//...
}
Upvotes: 0
Reputation: 1201
There could be multiple solutions to your problem:
Create a ViewModel having the only required properties in it and map those properties from the Original model and return that viewModel. You can use AutoMapper library for mapping the original model to your view model.
The other thing is that ASP.NET Web API uses Json.Net as default formatter, so if your application just only uses JSON as data format, you can use [JsonIgnore] to ignore property for serialization:
class Account {
public int ID { get; }
public string Username { get; }
[JsonIgnore]
public string Password { get; }
//... more properties
}
Hope this will help you out.
Upvotes: 2