0

In my WPF Application I am using a custom Control that looks like this:

public class FancyLabel : Label
{
    public string FancyProperty  { get; set; }

    public FancyLabel()
    {
        Height = 26;
        BorderBrush = Brushes.Gray;
        Background = Brushes.LightGray;

    }
}

The Control has customizable attributes like 'FancyAttr' an can be changed during runtime. On application exit I am trying to save the control / changes in a JSON File like this:

string output = JsonConvert.SerializeObject(new FancyLabel(), 
                Formatting.Indented,
           new JsonSerializerSettings
           {
               ReferenceLoopHandling = ReferenceLoopHandling.Ignore
           });
File.WriteAllText("C:/test/savefile.txt", output);

The problem is, that always all Properties from the inherited Label class get serialized and saved aswell which causes the save process to be unbareably long and the save file to be huge. Is there a way to define that only some properties or properties form the class 'FancyLabel' get serialized and saved?

colosso
  • 2,525
  • 3
  • 25
  • 49
  • 2
    Possible duplicate of [How to serialize only inherited properties of a class using a JsonConverter](https://stackoverflow.com/questions/30063259/how-to-serialize-only-inherited-properties-of-a-class-using-a-jsonconverter) – Anastasios Moraitis Aug 12 '19 at 13:20
  • 1
    Or apply `[JsonObject(MemberSerialization.OptIn)]` to the type and `[JsonProperty]` to the properties you want to serialize as shown in [this answer](https://stackoverflow.com/a/55046228/3744182) to [Json.net serialize only certain properties](https://stackoverflow.com/q/50974672/3744182). Do the answer liked above, or that answer, also answer your question? – dbc Aug 12 '19 at 17:57

0 Answers0