Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Serializer object with internal properties

Tags:

json

c#

.net

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" } 
like image 807
Farukh Avatar asked Nov 11 '14 20:11

Farukh


People also ask

How does the JsonProperty attribute affect JSON serialization?

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.

What is the difference between JSON and serialization?

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.

What does Jsonconvert Deserializeobject do?

Deserializes the JSON to the specified . NET type. Deserializes the JSON to the specified . NET type using a collection of JsonConverter.

What is JsonSerializerSettings?

DefaultIgnoreCondition Property (System. Text. Json) Gets or sets a value that determines when properties with default values are ignored during serialization or deserialization.


1 Answers

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?.

like image 189
dbc Avatar answered Sep 20 '22 01:09

dbc