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" }
JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.
Deserializes the JSON to the specified . NET type. Deserializes the JSON to the specified . NET type using a collection of JsonConverter.
DefaultIgnoreCondition Property (System. Text. Json) Gets or sets a value that determines when properties with default values are ignored during serialization or deserialization.
Mark the internal properties to be serialized 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 later, to test:
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); Console.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 namespace and assembly names I am using).
As an aside, when using TypeNameHandling
, do take note of this caution from the Newtonsoft docs:
TypeNameHandling should be used with caution when your application deserializes JSON from an external source. Incoming types should be validated with a custom SerializationBinder when deserializing with a value other than None.
For a discussion of why this may be necessary, see TypeNameHandling caution in Newtonsoft Json and and External json vulnerable because of Json.Net TypeNameHandling auto?.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With