Exclude property from json serialization in ApiController

I am trying to exclude properties from serialization in JSON in web ApiControllers. I checked the following 2 work scenarios.

I have included the following attributes in the property that I want to exclude.

[System.Web.Script.Serialization.ScriptIgnore] [System.Xml.Serialization.XmlIgnore] 

If I manually serialize my object using JavaScriptSerializer, the property is excluded. Also, if I look at the serialized XML output from the ApiController web, the property is excluded. The problem is that serialized JSON through the ApiController web interface still contains the property. Is there any other attribute that I can use that will exclude the property from JSON serialization?

UPDATE:

I realized that all my tests were in a much more complex project and that I did not try this in an isolated environment. I did this and still get the same results. Here is an example of some code that fails.

 public class Person { public string FirstName { get; set; } [System.Web.Script.Serialization.ScriptIgnore] [System.Xml.Serialization.XmlIgnore] public string LastName { get; set; } } public class PeopleController : ApiController { public IEnumerable<Person> Get() { return new[] { new Person{FirstName = "John", LastName = "Doe"}, new Person{FirstName = "Jane", LastName = "Doe"} }; } } 

Here is the generated output.

JSON:

 [ { "FirstName" : "John", "LastName" : "Doe" }, { "FirstName" : "Jane", "LastName" : "Doe" } ] 

XML:

 <?xml version="1.0" encoding="utf-8"?> <ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Person> <FirstName>John</FirstName> </Person> <Person> <FirstName>Jane</FirstName> </Person> </ArrayOfPerson> 
+4
source share
5 answers

Remember that JSON serialization is changing in the web API.

In beta, the Web API used DataContractJsonSerializer to serialize JSON. However, this has changed; the latest version of the web API uses json.net by default, although you can override this and use the DataContractJsonSerializer instead.

With the DataContractJsonSerializer, you can use the [DataContract] attributes to control serialization. I am not very familiar with json.net, so I don’t know how it manages serialization.

+4
source

You can use the [JsonIgnore] attribute for a specific JSON patch; or you can use [DataContract] and [DataMember] for a fix that works with both the JSON format and the (XML) DataContractSerializer .

This article provides more information about the default media file formats:

http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization#json_media_type_formatter

+4
source

This is a bit late for the game, but IgnoreDataMember is exactly what we need in your / my scenario:

 [System.Runtime.Serialization.IgnoreDataMember] public int NotSerialized { get; set; } 

According to MSDN, IgnoreDataMember came with .NET 3.0 SP2.

+3
source

JsonIgnore changes the definition of the entire class. If you want to control a specific action / request, you can try this approach .

+1
source

This seems to be what I need. It shows that you can change formats. It even includes a formatting example that uses the JavaScriptSerializer that I need.

http://wildermuth.com/2012/2/22/WebAPI_for_the_MVC_Guy

0
source

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


All Articles