31785

C# DataMember Serializer Ordering Opposite of Expected

Question:

According to this article, I'd expect to see the fields in my base class at the top of the list of fields when serializing to JSON. However, I'm seeing the fields at the bottom of the list. The ordering is correct within the actual class itself, but not among the hierarchy.

What's happening is it's ordering properly with the class, it it's doing the exact reverse of what I'd expect. I'd expect the base classes to have their fields serialized first. I don't want to use the Order=X attribute because there are too many fields in my objects.

<strong>This is the exact opposite behavior as described here:</strong>

<a href="http://msdn.microsoft.com/en-us/library/ms729813(v=vs.110).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms729813(v=vs.110).aspx</a>

[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; } }

<em><strong>When serializing an instance of MySecondChild...</strong></em>

<strong>Expected</strong>

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

<strong>Actual</strong>

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

Answer1:

Works for me: <a href="http://pastebin.com/PqBEHf6g" rel="nofollow">http://pastebin.com/PqBEHf6g</a>

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

Recommend

  • How enable GZIP compression for Spring HttpInvoker requests using Tomcat?
  • Json.net Add property to every class containing of a certain type
  • Gson serializing Spring beans
  • Getting directory of input file (Applescript)
  • Finding parents in a tree hierarchy for a given child LINQ (lambda expression)
  • How to sort things out in ListView?
  • Android application not restoring state when installed from .apk, works fine from eclipse
  • Lua: Line breaks in strings
  • Telegram bot API - Inline bot getting Error 400 while trying to answer inline query
  • Compress a file with GZipStream while maintaining its meta-data
  • powershell Get-Counter -ComputerName parameter on Windows 7
  • Xamarin MonoAndroid Azure mobile service InsertAsync
  • Python cosine function precision [duplicate]
  • HttpClient: disabling chunked encoding
  • Private IP address in reserved subnet range
  • How To Customize ASP.NET Chart Databound To SqlDataSource
  • Is there a way to save the selected text and highlight it again once the page is refreshed?
  • Enabling DTD support in Sql Server
  • Trying to get the char code of ENTER key
  • UWP/C# - Issue with AQS and USB Devices
  • Invalid Date on validation Date of js
  • Why use database factory in asp.net mvc?
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • D3 nodes and links from JSON with nested arrays of children
  • Breeze - Deleted Items nav properties bug
  • Retrieving value from sql ExecuteScalar()
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • javaw.exe and eclipse startup problems
  • TFS: Get latest causes slow project reloading
  • Controls, properties, events and timers running in design time
  • Statically linking a C++ library to a C# process using CLI or any other way
  • How do I rollback to a specific git commit
  • How to format a variable of double type
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • python regex in pyparsing
  • need help with bizarre java.net.HttpURLConnection behavior
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy