Mark the internal properties with the [JsonProperty()] attribute:
public class Foo { [JsonProperty()] internal int num1 { get; set; } [JsonProperty()] internal double num2 { get; set; } public string Description { get; set; } public override string ToString() { if (!string.IsNullOrEmpty(Description)) return Description; return base.ToString(); } }
And then, to check:
Foo f = new Foo(); f.Description = "Foo Example"; f.num1 = 101; f.num2 = 202; JsonSerializerSettings settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }; var jsonOutput = JsonConvert.SerializeObject(f, Formatting.Indented, settings); Debug.WriteLine(jsonOutput);
I get the following output:
{ "$type": "Tile.JsonInternalPropertySerialization.Foo, Tile", "num1": 101, "num2": 202.0, "Description": "Foo Example" }
(where "Tile.JsonInternalPropertySerialization" and "Tile" are the namespace names and assembly names that I use).
source share