This project has moved and is read-only. For the latest updates, please go here.

JsonConvert.SerializeObject and Inheritance

Feb 3, 2012 at 7: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.