1

I have created a custom label object in c# and I need to make json object from this object. But I have derived the Label from the 'Label' control to custom label object. After serializing custom label object, json is getting filled with label properties. But, I don't need it. I need to pass only the custom label object.

This is custom label :

public class customLabel:Label
{

        public string X { get; set; }

        public string Y { get; set; }

        public string H { get; set; }

        public string W { get; set; }

        public string FontName { get; set; }

        public string FontSize { get; set; }

        public string Type { get; set; }

        public string Align { get; set; }

        public string _Text { get; set; }


}

I am using Newtonsoft.Json for json serializng

Tanuj Wadhwa
  • 1,945
  • 8
  • 33
  • 56
DanialDP
  • 25
  • 10

3 Answers3

2

Create a custom JsonConvertor that includes the properties you want.

Then pass it to SerializeObject to control the serialization.

string result = JsonConvert.SerializeObject(
                     customLabel,
                     Formatting.Indented,
                     new CustomLabelConverter(typeof(CustomLabel)));
Jodrell
  • 32,967
  • 4
  • 79
  • 120
1

Take a look at this Ignore Base Class Properties in Json.NET Serialization

[JsonObject(MemberSerialization.OptIn)]
public class customLabel:Label
{
    [JsonProperty("X")]
    public string X { get; set; }

    [JsonProperty("Y")]
    public string Y { get; set; }

    ...
    public string H { get; set; }

    public string W { get; set; }

    public string FontName { get; set; }

    public string FontSize { get; set; }

    public string Type { get; set; }

    public string Align { get; set; }

    public string _Text { get; set; }

}

but you need to put JsonProperty to all any property you need to serialize it

Asakuraa Ranger
  • 517
  • 7
  • 18
0

Try something like this:

customLabel yourLabel = new customLabel();

yourLabel.X = 50;
yourLabel.Y = 20;
//....

string output = JsonConvert.SerializeObject(yourLabel);
//output contains the serialized object

customLabel deserializedLabel = JsonConvert.DeserializeObject<customLabel>(output);

edit: Change your class to this:

[DataContract]
public class customLabel:Label
{
     [DataMember]
     public string X { get; set; }
     [DataMember]    
     public string Y { get; set; }
     [DataMember]
     public string H { get; set; }
     [DataMember]    
     public string W { get; set; }
     [DataMember]
     public string FontName { get; set; }
     [DataMember]   
     public string FontSize { get; set; }
     [DataMember]  
     public string Type { get; set;
     [DataMember]
     public string Align { get; set; }
     [DataMember]
     public string _Text { get; set; }
}

Now only the properties with the attribute [DataMember] should be included

And take a look at the documentation: https://www.newtonsoft.com/json/help/html/SerializingJSON.htm

CoLiNaDE
  • 328
  • 2
  • 9