JsonConvert.SerializeObject and Inheritance

Feb 3, 2012 at 6:05 PM

Hi, I am trying to make a generic ToJSON method using JsonConvert.SerializeObject:

public static string ToJSON(this T obj)
{
  return JsonConvert.SerializeObject(obj, Formatting.None, new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver() });
}

My problem is I have a class structure similar to this:

public class A
{
}

public class B : A
{
  public string PropertyA { get; set; };
  public string PropertyB { get; set; };
}

When I try to serialize B, my resulting string is empty JSON: "{}"
If I add a property to A and fill it, my JSON string only has the value of the property of A.

Any thoughts why this would be happening? It doesn't seem to be working with the inheritance properly.
Thanks