Consider the following example:
public class BaseClass
{
public string StringInBaseClass {get;set;}
public int IntInBaseClass {get;set;}
}
public InheritingClass : BaseClass
{
public long LongInInheritingClass {get;set;}
public long ShortInInheritingClass {get;set;}
public long CharInInheritingClass {get;set;}
}
Now, what I want to do is serialize JUST the inheriting properties to a JSON string. For example, I want to somehow create a JSON object out of just the 3 properties in InheritingClass, but if I do:
InheritingClass a = new InheritingClass();
JavaScriptSerializer jss = new JavaScriptSerializer();
string jsonString = jss.Serialize(a);
The jsonString value will have all of the properties of the BaseClass as well as all of the properties of InheritingClass. I understand that is normal, because I am inheriting all of those properties. What I am looking to do is NOT include those inherited properties and build a JSON string out of ONLY the 3 properties in InheritingClass.
Is this possible?
Use the ScriptIgnoreAttribute on any property you don’t want serialized with the JavaScriptSerializer. With this you can do the following in your inherited class to stop a property from the base class from being serialized: