I found quite a nasty difference between the XML and Json serializer used by MVC4 today.
I tried to return an object that looks like this:
public class Person
{
public string FirstName {get;set;}
public string LastName {get;set;}
public string FullName{ get{ return FirstName + " " + LastName }}
}
When the client request json as the result, I get FirstName
, LastName
and FullName
returned with the expected values.
However, when the client asks for XML data, he only get FirstName
and LastName
.
This is in my opinion very bad, as it means that the client will get different data depending on which format they request.
It seems this is intentional since the documentation for the Json serializer says that read-only properties are included by default, and for the Xml serializer that they are excluded by default.
So my questions is: How chan I change the Xml serializer used by MVC4 to include those read-only by default. I know it manages to, because if you add the DataContract
attribute to the class, and DataMember
attribute to each property of the class it returnes the read-only variable as well, but I really don't want to do that.