.NET WebAPI Serialization k_BackingField Nastiness

Serializationasp.net Web-Api

Serialization Problem Overview


When i serialize the following:

[Serializable]
public class Error
{

    public string Status { get; set; }
    public string Message { get; set; }
    public string ErrorReferenceCode { get; set; }
    public List<FriendlyError> Errors { get; set; }
}

I get this disgusting mess:

<ErrorRootOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance"   xmlns="http://schemas.datacontract.org/2004/07/Printmee.Api">
<_x003C_Errors_x003E_k__BackingField>
An exception has occurred. Please contact printmee support
</_x003C_Errors_x003E_k__BackingField>
<_x003C_LookupCode_x003E_k__BackingField>988232ec-6bc9-48f3-8116-7ff7c71302dd</_x003C_LookupCode_x003E_k__BackingField>
</ErrorRootOfstring>

What gives? How can i make this pretty? JSON responses also contain the k_BackingField

Serialization Solutions


Solution 1 - Serialization

By default you don't need to use neither [Serializable] nor [DataContract] to work with Web API.

Just leave your model as is, and Web API would serialize all the public properties for you.

Only if you want to have more control about what's included, you then decorate your class with [DataContract] and the properties to be included with [DataMember] (because both DCS and JSON.NET respsect these attributes).

If for some reason, you need the [Serializable] on your class (i.e. you are serializing it into a memory stream for some reason, doing deep copies etc), then you have to use both attributes in conjunction to prevent the backing field names:

[Serializable]
[DataContract]
public class Error
{
    [DataMember]
    public string Status { get; set; }
    [DataMember]
    public string Message { get; set; }
    [DataMember]
    public string ErrorReferenceCode { get; set; }
    [DataMember]
    public List<FriendlyError> Errors { get; set; }
}

Solution 2 - Serialization

There is a more general solution: you can configure the Json Serializer to ignore the [Serializable] attribute, so that you don't have to change the attributes in your classes.

You should make this configuration change in the application start, i.e. in Global.asax Application_Start event:

var serializerSettings =
  GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
var contractResolver =
  (DefaultContractResolver)serializerSettings.ContractResolver;
contractResolver.IgnoreSerializableAttribute = true;

You can also make other changes to the Json serialization, like specifying formats for serializing dates, and many other things.

This will only apply to the Web API JSON serialization. The other serializations in the app (Web API XML serialization, MVC JsonResult...) won't be affected by this setting.

Solution 3 - Serialization

Try using DataContract instead of Serializable for marking your class. For more detail on why, look at this good blog post on serializing automatic properties.

Solution 4 - Serialization

The [DataContract] attributes dosn't worked for me, so it was not an option.

https://stackoverflow.com/questions/35846873/xmlserializer-ignores-xmlattribute-in-webapi

The above resolution solved it for me.

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMicahView Question on Stackoverflow
Solution 1 - SerializationFilip WView Answer on Stackoverflow
Solution 2 - SerializationJotaBeView Answer on Stackoverflow
Solution 3 - SerializationSixto SaezView Answer on Stackoverflow
Solution 4 - SerializationJanBorupView Answer on Stackoverflow