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

5
  • And what's your question? Commented Apr 30, 2018 at 7:01
  • how can i only pass custom label object for serializng? @SebastianHofmann Commented Apr 30, 2018 at 7:01
  • 1
    yeah, but your object inherits from Label so, it is a label too. Commented Apr 30, 2018 at 7:06
  • @Jodrell can i only pass this values to json? Commented Apr 30, 2018 at 7:08
  • @DanialDP what value do you mean by this values? You can pass any object to JSON.NET. Objects will always be serialized as dictionaries. Did you try it? What code did you use? What did you get and what did you expect? Commented Apr 30, 2018 at 7:10

3 Answers 3

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)));
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

2 Comments

but its pass label properties.i just wanna pass custom label values!
sorry i misunderstood you. I added the class definition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.