As I understand you want to be able to manually serialize the object, while still benefiting from the .NET to Json serialization classes.
In this case you can use the JavaScriptSerializer class. You can register a convertor for this class in which case you have full control over the serialization of the object you're passing. The Serialize method that you override returns a simple IDictionary, which will then be directly serialized to json..
Here's an example of what it looks like..
void Main()
{
var js = new JavaScriptSerializer();
js.RegisterConverters(new[] { new PonySerializer() });
js.Serialize(new Bar { Foo = 5 }).Dump();
}
public class PonySerializer : JavaScriptConverter
{
public override IEnumerable<Type> SupportedTypes
{
get { return new [] { typeof(Bar) }; }
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var result = new Dictionary<string, object>();
if (obj is Bar)
{
var ob = obj as Bar;
result["Foo"] = ob.Foo;
}
return result;
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
}
public class Bar
{
public int Foo { get; set; }
}