This article is a mirror article of machine translation, please click here to jump to the original article.

View: 33737|Reply: 2

[Source] Newtonsoft.Json ignores the DataContract feature

[Copy link]
Posted on 5/30/2018 4:50:17 PM | | | |
Newtonsoft.JsonAs you probably know, commonly used JSON serialization and deserialization tools

DataContractAttributeSpecifies that type to define or implement a data contract that can be serialized by a serializer such as DataContractSerializer. To make a type serializable, the type author must define a data contract for its type.

encountered a difficult problem,The parent class has the [DataContract] feature, and the child class inherits the parent class, and all the attributes of the subclass need to be serialized and deserialized with the [DataMember] attributeSince the parent class is someone else's encapsulated dll, the [DataContract] feature of the parent class cannot be deleted!

The test is as follows: Test1 class has the [DataContract] feature, if the attribute does not add the [DataMember] feature, it will cause serialization and deserialization to fail, as shown in the figure below:




We annotated the [DataContract] feature of class Test1 and found that it can be serialized and deserialized normally, as follows:



Is there any advanced use of Newtonsoft.Json that can ignore this feature? The answer is yes!

Just use the Json.Net's OptOut property. It will take precedence over the DataContract.

//
        Abstract:
        All public members are serialized by default. Members can be excluded using Newtonsoft.Json.JsonIgnoreAttribute
        or System.NonSerializedAttribute. This is the default member serialization pattern.
        OptOut = 0,
        //
        Abstract:
        Only members labeled Newtonsoft.Json.JsonPropertyAttribute or System.Runtime.Serialization.DataMemberAttribute
        is serialized. This member serialization mode can also be set by tagging
        // class with System.Runtime.Serialization.DataContractAttribute.
        OptIn = 1,
        //
        Abstract:
        All public and private fields are serialized. Members can be excluded using Newtonsoft.Json.JsonIgnoreAttribute
        or System.NonSerializedAttribute. This member serialization pattern is also fine
        Set this by tagging the class with System.SerializableAttribute and setting the IgnoreSerializableAttribute
        Set Newtonsoft.Json.Serialization.DefaultContractResolver to false.
        Fields = 2


Test code:







Previous:Monitor AppDomains
Next:. .NET's fastest OOM object mapping component, Tiny Mapper, in project practice
Posted on 6/1/2018 1:20:10 PM |
Determine whether a json string is an object or an array
Newtonsoft.Json.Linq.JToken s = Newtonsoft.Json.Linq.JToken.Parse(str);

switch (s.Type.ToString())
                {
                    case "Object":
                        break;
                    case "Array":
                        break;
                    default:
                        break;
                }
Posted on 8/21/2020 11:52:32 AM |
Support the landlord is a master of asking for thighs
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com