4

I am trying to exclude properties from being serialized to JSON in web ApiControllers. I have verified the following 2 scenarios work.

I have included the following attributes on the property I wish to exclude.

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

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

UPDATE:

I realized all my tests were in a much more complex project and that I hadn't tried this in a an isolated environment. I did this and am still getting the same results. Here is an example of some code that is failing.

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>
Adam Carr
  • 2,986
  • 7
  • 31
  • 38
  • possible duplicate of [Ignoring properties when serializing](http://stackoverflow.com/questions/9285439/ignoring-properties-when-serializing) – McGarnagle May 07 '12 at 01:18
  • I don't see this as a duplicate of that since I can't use IgnoreDataMember because my object and all of it's properties still need to be serialized for WCF communication behind the scenes. I guess the root of my question is what is ASP.NET using to serialize to JSON? It doesn't seem they are using the JavaScriptSerializer. – Adam Carr May 07 '12 at 01:29
  • ah, got you. I stand corrected. – McGarnagle May 07 '12 at 01:35
  • Check out this one: the answer seems to be that the Model Binder does it, and you need to implement your own model binder: http://stackoverflow.com/questions/7330555/how-does-mvc-asp-net-serialization-work-for-json-object-on-controller-actions. – McGarnagle May 07 '12 at 01:38
  • No, that doesn't apply either. I already have a custom model binder in place for deserialization that is working fine. This is related to serialization and omitting properties as attributed. – Adam Carr May 07 '12 at 01:41
  • Ha... you're right again. I'll consider myself struck out on this one. Sorry. – McGarnagle May 07 '12 at 01:45
  • 1
    Ok, one more try... check out this guy's solution: http://stackoverflow.com/questions/1302946/asp-net-mvc-controlling-serialization-of-property-names-with-jsonresult – McGarnagle May 07 '12 at 01:51
  • Well, I think this likely answers the question I said about what does MVC use to serialize to JSON. It looks like it is DataContractJsonSerializer. I want different serialization behavior when serializing to JSON vs. when used via WCF calls. This is suggesting both depend on [DataMember] attributes so they become tightly coupled. Odd that this isn't the case with Xml serialization. I'm going to continue looking into this. – Adam Carr May 07 '12 at 01:57

5 Answers5

4

Be aware that JSON serialization is changing in Web API.

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

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

Mike Wasson
  • 6,572
  • 2
  • 24
  • 20
  • 7
    As of this date, if you are using MVC 4 Web Api out of the box, you can decorate the property to exclude with Newtonsoft.Json.JsonIgnore – cab0 Dec 01 '12 at 22:02
  • Is this still true with WCF service and .NET 4.5? – lorenzop Sep 15 '14 at 13:01
4

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

This article provides more detailed info on the default media-type formatters:

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

crimbo
  • 10,308
  • 8
  • 51
  • 55
4

This is a little late to the game, but IgnoreDataMember is precisely what we need in your/my scenario:

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

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

JMD
  • 7,331
  • 3
  • 29
  • 39
1

JsonIgnore modifies the entire class definition. In case you want to control specific action/request, you can try this approach.

Community
  • 1
  • 1
joym8
  • 4,014
  • 3
  • 50
  • 93
0

Looks like this covers what I need. It shows you you can swap out formatters. It even includes an example formatter that uses the JavaScriptSerializer which is what I need.

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

Adam Carr
  • 2,986
  • 7
  • 31
  • 38