

How can you prevent the serialization of DependencyObjectType information on Dep...
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?
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]
Recommend
-
8
Serialization without type information via impls Feb 25, 2012 My current implementation of the auto-serialization code generator requires full type information. This is a drag. First, macros and syntax extens...
-
10
What Is Cyber Extortion and How Can You Prevent It? By Chris Odogwu Published 17 hours ago Cyber extortion poses a significant th...
-
9
How can I prevent my UWP app from showing up in the Start menu?
-
1
What can be done to prevent Spam? What not to do Under no circumstances should you reply to any address(es) that appear to allow you to be removed from a spammer's list. Doing so u...
-
4
Can technology prevent the ‘bullwhip effect’?
-
10
News Release 27-Aug-2021 Reducing sugar in packaged foods can pre...
-
6
September 14, 2021
-
5
-
4
Guest How graph analytics can prevent buy-now, pay-later fraud Image Credit: B4LLS/Getty Images Jo...
-
8
How the BSV blockchain can prevent data ‘leaks’ of your private information Business 9 hours ago Do you know how much of your person...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK