Conditional data transfer. Serialization in WebApi.

I am trying to find a better way to conditionally enable and remove properties from my datacontract serialization in my .net WebApi project. In my Api, I want to allow users to specify the fields that they want to return.

For example, suppose I want my API to return an instance of the following class.

public class Car { public int Id { get; set; } public string Year { get; set; } public string Make { get; set; } public string Model { get; set; } public string Color { get; set; } } 

But instead of querying the whole object, an API call requires only the Id and Make fields. So the returned JSON will be

  { "Id": 12345, "Make": "Ford"} 

instead of the whole object.

Is there a way with a DataContract Serializer that I can conditionally add and remove properties from my return object?

** EDIT I ​​looked at the IgnoreDefault property and I don't think that it will do exactly what I need. The problem is that I want to include and exclude properties based on the api request, and not necessarily if they have data.

Is there a way to connect to the deserialization process and skip some properties? Can I make an individual contract?

+2
source share
2 answers

If you use a DataContractSerializer (or, in this case, a DataContractJsonSerializer ), you can use the DataMember(EmitDefaultValue = false)] decoration DataMember(EmitDefaultValue = false)] in your class. That way, you can set properties that you don't want to serialize by default (i.e. null for strings, 0 for int, etc.), and they won't.

If you use the ASP.NET web API, you should be aware that the default JSON serializer is not DataContractJsonSerializer (DCJS), but JSON.NET. Therefore, if you have not explicitly configured your JsonMediaTypeFormatter to use DCJS, you need another attribute to get the same behavior ( JsonProperty and its JsonProperty property).

In the code below, only two elements that were assigned in this Car object are serialized using both serializers. Please note that you can remove one of the attributes if you intend to use only one of them.

 public class StackOverflow_12465285 { [DataContract] public class Car { private int savedId; private string savedYear; private string savedMake; private string savedModel; private string savedColor; [DataMember(EmitDefaultValue = false)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] public int Id { get; set; } [DataMember(EmitDefaultValue = false)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] public string Year { get; set; } [DataMember(EmitDefaultValue = false)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] public string Make { get; set; } [DataMember(EmitDefaultValue = false)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] public string Model { get; set; } [DataMember(EmitDefaultValue = false)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] public string Color { get; set; } [OnSerializing] void OnSerializing(StreamingContext ctx) { this.savedId = this.Id; this.savedYear = this.Year; this.savedMake = this.Make; this.savedModel = this.Model; this.savedColor = this.Color; // Logic to determine which ones to serialize, let say I only want Id, Make; so make all others default. this.Color = default(string); this.Model = default(string); this.Year = default(string); } [OnSerialized] void OnSerialized(StreamingContext ctx) { this.Id = this.savedId; this.Year = this.savedYear; this.Make = this.savedMake; this.Model = this.savedModel; this.Color = this.savedColor; } } public static void Test() { Car car = new Car { Id = 12345, Make = "Ford", Model = "Focus", Color = "Red", Year = "2010" }; JsonSerializer js = new JsonSerializer(); StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); js.Serialize(sw, car); Console.WriteLine("Using JSON.NET: {0}", sb.ToString()); MemoryStream ms = new MemoryStream(); DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(Car)); dcjs.WriteObject(ms, car); Console.WriteLine("Using DCJS: {0}", Encoding.UTF8.GetString(ms.ToArray())); } } 
+6
source

May I know what is related to JSON Converter?

-1
source

Source: https://habr.com/ru/post/1200516/


All Articles