5

How can you prevent the serialization of DependencyObjectType information on Dep...

 2 years ago
source link: https://www.codesd.com/item/how-can-you-prevent-the-serialization-of-dependencyobjecttype-information-on-dependencyobjects-using-newtonsoft-json.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How can you prevent the serialization of DependencyObjectType information on DependencyObjects using Newtonsoft JSON?

advertisements

When serializing DependencyObject derivative classes using Newtonsoft's JSON (version 8.0.0) for .Net / C# I notice that it always serializes the DependencyObjectType and Dispatcher objects along with all of my derivative class' properties. Since the DependencyObjectType and Dispatcher objects come from the DependencyObject class I'm inheriting from, how can I add smart tags or attributes (such as [JsonIgnore]) to prevent them from being serialized? Or am I perhaps thinking about it the wrong way?

Example class code:

public class MyClass : DependencyObject
{
    public int MyProperty
    {
        get { return (int)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }
    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));
}

Serializer code:

JsonSerializer serializer = new JsonSerializer();
using (StreamWriter sw = new StreamWriter(filePath))
using (JsonWriter jw = new JsonTextWriter(sw))
{
    serializer.Serialize(jw, MyInstance);
}

When serialized I get this massive blob (all but the top line are unwanted):

{
    "MyProperty": 0,
    "DependencyObjectType": {
      "Id": 148,
      "SystemType": "MyProject.MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
      "BaseType": {
        "Id": 0,
        "SystemType": "System.Windows.DependencyObject, WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxx",
        "BaseType": null,
        "Name": "DependencyObject"
      },
      "Name": "MyClass"
    },
    "IsSealed": false,
    "Dispatcher": {
      "Thread": {
        "ManagedThreadId": 17,
        "ExecutionContext": null,
        "Priority": 2,
        "IsAlive": true,
        "IsThreadPoolThread": true,
        "IsBackground": true,
        "ThreadState": 4,
        "ApartmentState": 1,
        "CurrentUICulture": "en-US",
        "CurrentCulture": "en-US",
        "Name": null
      },
      "HasShutdownStarted": false,
      "HasShutdownFinished": false,
      "Hooks": {}
    }
}


You can use OptIn approach to suppress serialization of all properties in the type hierarchy except required properties:

[JsonObject(MemberSerialization = MemberSerialization.OptIn)]

And mark properties for serialization with:

[JsonProperty]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK