Recently I found an annoying different behavior between net35 and net40 NewtonSoft Json library. For net40, the serialized payload is good. But for net35, the serialized payload includes annoying value k__BackingField.
Here is a sample code to repro the issue:
// Notice that there is no serializable attribute
public class SamplePayload
{
public Guid Id { get; set; }
}
static void Main(string[] args)
{
var writeStream = new MemoryStream();
var formatter = new JsonMediaTypeFormatter();
formatter.WriteToStreamAsync(typeof(SamplePayload), new SamplePayload(), writeStream, null, null).Wait();
Console.WriteLine(System.Text.Encoding.UTF8.GetString(writeStream.ToArray()));
}
If reference a net40/net45 Json library, the serialized payload is like "Id" which is expected. But with net35 library, the serialized payload includes "k__BackingField".
I'm wondering why there is such a behavior difference? Is it a defect in NewtonSoft Json library, or a by-design behavior? If it is latter, what's the best practice to avoid such issue?