Thursday, 15 September 2011

c# - Why doesn't Json.net properly serialize a class derived from TreeNode? -


I have a class TreeNode that it is just a string that returns when I try to sequentially There is no JSON string inherited (as I expected).

For example:

  string json = JsonConvert.SerializeObject (new A ());  

Output:

  "tree_ode:"  

where A is defined Gone:

  Public class A: triode {public int x {get; Set; }}  

If I remove TreeNode Virat, then output is:

  {"x": 0}   

If it inherited from TreeNode does not it sort the property? Note: I can only filter my class to serialize the public properties of my by using a contract:

  public class ShouldSerializeContractResolver: DefaultContractResolver {from private list & Lt; String & gt; PropertiesNames; Public ShouldSerializeContractResolver (type type) {this.propertiesNames = type.GetProperties (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) .Select (P = & gt; p.Name) .toList (); } Protected override JsonProperty CreateProperty (MemberInfo member, MemberSerialization memberSerialization) {JsonProperty property = base.CreateProperty (member, memberSerialization); Property. ShouldSerialize = instance = & gt; {Return Property Properties Insert (property.PropertyName); }; Return assets; }}  

and then using the object sequentially:

  ShouldSerializeContractResolver contract = new ShouldSerializeContractResolver (typeof (CustomTreeNode)); JsonSerializer settings jsonSerializerSettings = New JsonSerializer settings () {contract Resolver = contract}; String json = JsonConvert.SerializeObject (group, formatting. Indented, jsonSerializer settings);  

But this gives the same invalid output as I mentioned in the beginning of the topic.

You are talking about the reason you are seeing this behavior, because TreeNode is a feature applied to it, which causes a simple string value to be serialized by Json.Net in place of any object.

You can get it done in different ways to work the way you want it to work.

  1. Apply a [JsonObject] attribute in your custom tree node category [JasonObject] Public Class A: Orion {...}

  2. Or, override your custom contractualizer in the CreateContract () method So that it creates an object contract for your custom type:

      Public class Chiasiorialize contract receiver: default content {personal type myType; Private listing & lt; String & gt; PropertiesNames; Public ShouldSerializeContractResolver (type type) {myType = type; This.propertiesNames = type.GetProperties (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DecclaredOnly) Select (P = & gt; p.Name) .toList (); } Secure Override JsonContractContractContract (type object type) {if (objectType == myType) {ReturnObjectContract Return (Object Type); } Return Base Crete contract (object type); } Protected Override Jason SupertyytePropti (member permissions member, subscriber activation member serialization) {Jasonproperty property = base. CreteProperty (Member, Member Serialization); Property. ShouldSerialize = instance = & gt; {Return Property Properties Insert (property.PropertyName); }; Return assets; }}  

No comments:

Post a Comment