using Newtonsoft.Json.Linq;
public static void Main()
var sampleClass = new SampleClass()
var str = "{sample:{id:2}}";
sampleClass = JsonConvert.DeserializeObject<SampleClass>(str, new JsonSerializerSettings());
Console.WriteLine("Id: " + sampleClass.Id);
[JsonConverter(typeof(JsonPathConverter))]
[JsonProperty("sample.id")]
class JsonPathConverter : JsonConverter
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
JObject jo = JObject.Load(reader);
object targetObj = Activator.CreateInstance(objectType);
foreach (PropertyInfo prop in objectType.GetProperties()
.Where(p => p.CanRead && p.CanWrite))
JsonPropertyAttribute att = prop.GetCustomAttributes(true)
.OfType<JsonPropertyAttribute>()
string jsonPath = (att != null ? att.PropertyName : prop.Name);
JToken token = jo.SelectToken(jsonPath);
if (token != null && token.Type != JTokenType.Null)
object value = token.ToObject(prop.PropertyType, serializer);
prop.SetValue(targetObj, value, null);
public override bool CanConvert(Type objectType)
public override bool CanWrite
public override void WriteJson(JsonWriter writer, object value,
JsonSerializer serializer)
throw new NotImplementedException();