C # DataMember Serializer for Comparison

As per this article, I expect to see fields in the base class at the top of the field list when serializing to JSON. However, I see fields at the bottom of the list. The order is correct in the actual class itself, but not in the hierarchy.

What happens is it is ordering correctly with the class, it does the exact opposite of what I expect. I expect the base classes to be serialized first. I don't want to use the Order = X attribute because there are too many fields in my objects.

This is the exact opposite behavior described here:

http://msdn.microsoft.com/en-us/library/ms729813(v=vs.110).aspx

[DataContract]
public class MyBase {
  [DataMember]
  public long Id { get; set; }
}

[DataContract]
public class MyChild : MyBase { 
  [DataMember]
  public string Field1 { get; set; }
  [DataMember]
  public string Field2 { get; set; }
  [DataMember]
  public string Field3 { get; set; }
}

[DataContract]
public class MySecondChild : MyChild { 
  [DataMember]
  public string SecondField { get; set; }
}

      

When serializing an instance of MySecondChild ...

Expected

{ 
    "Id": 1,        
    "Field1": "f1",
    "Field2": "f2",
    "Field3": "f3",
    "SecondField": "s1"
}

      

Actual

{  
    "SecondField": "s1",      
    "Field1": "f1",
    "Field2": "f2",
    "Field3": "f3",
    "Id": 1
}

      

0


source to share


1 answer


Works for me: http://pastebin.com/PqBEHf6g



{"Id":1,"Field1":"f1","Field2":"f2","Field3":"f3","SecondField":"s1"}

      

0


source







All Articles