CSharp · Console
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
public class Program
{
// The names of auto properties in the Poco class are serialized with "k__BackingField" in the name.
// To fix this, change either one of the marked lines below.
public static void Main()
{
var poco = new Poco { Id = 1, Name = "foo" };
var resolver = new DefaultContractResolver
{
IgnoreSerializableAttribute = false // change this to true
};
var settings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ContractResolver = resolver
};
string json = JsonConvert.SerializeObject(poco, settings);
Console.WriteLine(json);
}
}
[Serializable] // comment out this line
class Poco
{
public int Id { get; set; }
public string Name { get; set; }
}