Continuation from the comment section. Even if you call a function to remove the null values, my personal opinion about it would be that it's bad design, having a dictionary and serializing that is a more elegant way than having to remove the properties we don't want after we're done.
What I would do is something like this:
public class MyCustomClassObject
{
public Dictionary<string, object> Foo { get; set; }
public MyCustomClassObject()
{
this.Foo = new Dictionary<string, object>();
}
}
public MyCustomClassObject GetTestData()
{
MyCustomClassObject x = new MyCustomClassObject();
x.Foo.Add("PropertyA", 2);
x.Foo.Add("PropertyC", "3");
return x.Foo;
}
this gives you a more generic object to work with and follows the JSON format better, since you theoretically could have a list or array of objects as a value, this is also more adaptable to work with since you can add the PropertyD here.
Why do you need something that removes the values after you've added them?