Here is a reference to an article written by ScottGu
Based on that, I wrote some code which I think might be helpful
public interface IEducationalInstitute
{
string Name
{
get; set;
}
}
public class School : IEducationalInstitute
{
private string name;
#region IEducationalInstitute Members
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
#endregion
}
public class Student
{
public IEducationalInstitute LocalSchool
{
get; set;
}
public int ID
{
get; set;
}
}
public static class JSONHelper
{
public static string ToJSON(this object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
public static string ToJSON(this object obj, int depth)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RecursionLimit = depth;
return serializer.Serialize(obj);
}
}
And this is how you would call it
School myFavSchool = new School()
{
Name = "JFK High School"
};
Student sam = new Student()
{
ID = 1,
LocalSchool = myFavSchool
};
string jSONstring = sam.ToJSON();
Console.WriteLine(jSONstring);
//Result {"LocalSchool":{"Name":"JFK High School"},"ID":1}
If I understand it correctly, I do not think you need to specify a concrete class which implements the interface for JSON serialization.