3

I am building a RESTful Web Service with the new Microsoft MVC 4 ApiController class and WebAPI. I have a Person class:

public class Person
{
    public string surname { get; set; }
    public string name{ get; set; }
}

and the default HTTP GET method works, returning the following:

<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <surname>John</surname>
    <name>Titor</name>
</Person>

I now need an annotation set which lets me change the default inner objects' names, for example I'd like to change surname into msurname. I have tried adding the following:

[XmlElement("msurname")]

annotation, but that only works if the Accept header of my request contains application/xml (of course). I have tried and used the [DataMember] annotation, which is completely ignored.

Is there an annotation set I can use with this ApiController in WebAPI for serialization into both XML and JSON formats? Thank you.

EDIT: correction, if I use the [DataMember] and [DataContract] annotation, I get the desired behaviour with JSON serialization, but not with the XML. The opposite thing happens if I use [XmlElement].

3 Answers 3

5

The behaviour you are seeing with DataMember is because by default WebAPI uses XmlSerializer, not DataContractSerializer. However JSON uses the JSONDataContractSerializer by default at the moment. However in the future it will not. You can change the XML to the XmlDataContractSerializer by setting

GlobalConfiguration.Config.Formatters.XmlDataContractSerializer = true;

That way, both the JSON and XML formats will use the DataContractSerializer.

Sign up to request clarification or add additional context in comments.

4 Comments

Great, thank you! I have put in my Global.asax.cs Application_Start the following line (a bit different from the one you suggested): GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseDataContractSerializer = true;
By the way, what will be the default behavior in the future? Will JSON use a serializer with the same super class as XmlSerializer?
@MacGyver No. They are looking to replace the default JSON serializer with JSON.Net. However, You can always change the Formatters collection to only use the ones you want.
Do we know if we can eventually remove this line by RTM and have our DataMember/DataContract attributes working?
0

The two serializers do use different Attributes to handle renaming of columns etc.

There is no way to unify that you will need to have both Attributes present.

You could however use a different XML/JSON serializer that recognizes the other ones attributes.

UPDATE
You might also try the DataAnnotations and see if the serializers recognize them

1 Comment

[DisplayName("msurname")] actually changes nothing, I still get "surname" in both XML and JSON serializations.
0

The way it works is that you will be dealing with Formatters. The XML data you are getting is produced by XmlMediaTypeFormatter (XmlMediaTypeFomatter Class).

I am not aware of any built-in feature as you describe but it is fairly easy to write your own formatter.

Here is an example of a custom formatter implementation, you will get the idea:

Using JSON.NET with ASP.NET Web API

2 Comments

Yes but I'd rather keep it simple. I was hoping there was some kind of annotation super class that was taken into consideration by all of the default serializers, since the default serialization system is hidden.
@MacGyver Darrel Miller has a better answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.