简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM