DanialDP
DanialDP

Reputation: 25

C# json custom serializing

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

Upvotes: 1

Views: 1158

Answers (3)

Asakuraa Ranger
Asakuraa Ranger

Reputation: 617

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

Upvotes: 1

CoLiNaDE
CoLiNaDE

Reputation: 348

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

Upvotes: 0

Jodrell
Jodrell

Reputation: 35696

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)));

Upvotes: 2

Related Questions