I have class with some internal properties and I would like to serialize them into json as well. How can I accomplish this? For example
public class Foo
{
internal int num1 { get; set; }
internal double num2 { get; set; }
public string Description { get; set; }
public override string ToString()
{
if (!string.IsNullOrEmpty(Description))
return Description;
return base.ToString();
}
}
Saving it using
Foo f = new Foo();
f.Description = "Foo Example";
JsonSerializerSettings settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All };
string jsonOutput = JsonConvert.SerializeObject(f, Formatting.Indented, settings);
using (StreamWriter sw = new StreamWriter("json_file.json"))
{
sw.WriteLine(jsonOutput);
}
I get
{
"$type": "SideSlopeTest.Foo, SideSlopeTest",
"Description": "Foo Example"
}