JamesNK/Newtonsoft.Json — pull request #376 by OndrejPetrzilka (2014-09-12 21:39:20+00:00)
Title: [ObjectContract] Added support for custom member serialization

Custom member (de)serialization allows me to have more control over serialization and decide at runtime what and how to serialize. Member abstraction (TypeHandling.Auto) works with this too. It was hard/impossible to get it working with Extension Data.

Using custom member (de)serialization and few other classes makes possible this, I'm used to this format from SharpDX.IDataSerializable:

public class Control : IJsonSerializable
{
public string Name;
public Vector2 Position;
public Vector2 Size { get; set; }
public HashSet Controls = new HashSet();

public virtual void Serialize(JsonSerializableContext context)
{
if (context.Writing) // Same method used for serialization
{
context.Serialize("Name", "--unnamed--");
}
else
{
context.Serialize("OldName", ref Name);
context.Serialize("Name", ref Name);
}

if (Position != Vector2.Zero) // Decide here if serialize or not (similar to ShouldSerialize)
{
context.Serialize("Position", ref Position);
}
context.Serialize(() => Size); // Possible like this (this expression gets cached)
//context.Serialize("Size", ref Size); // Or like this
context.Serialize("Controls", ref Controls);
}
}