Json.net serialize specific private field
There is no need to implement a custom DefaultContractResolver
. The solution is to put [JsonProperty]
on _hexes
and [JsonIgnore]
on all the other properties and fields.
Because business models will eventually change, I prefer implementing ISerializable and using .NET's way of creating momentos (i.e., a property bag). This works best when you need to version your objects at runtime. Anything that you don't want to serialize, don't put it in the property bag.
Especially, since JSON.Net (Newtonsoft.Json) will also honor it with its serialization and deserialization methods.
using System;
using System.Runtime.Serialization;
[Serializable]
public class Visitor : ISerializable
{
private int Version;
public string Name { get; private set; }
public string IP { get; set: }
public Visitor()
{
this.Version = 2;
}
public void ChangeName(string Name)
{
this.Name = Name;
}
//Deserialize
protected Visitor(SerializationInfo info, StreamingContext context)
{
this.Version = info.GetInt32("Version");
this.Name = info.GetString("Name");
}
//Serialize
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Version", this.Version);
info.AddValue("Name", this.Name);
}
[OnDeserialized]
private void OnDeserialization(StreamingContext context)
{
switch (this.Version)
{
case 1:
//Handle versioning issues, if this
//deserialized version is one, so that
//it can play well once it's serialized as
//version two.
break;
}
}
}