Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON.Net not serializing when JsonProperty set?
    text
    copied!<p>I'm using the latest (4.5) version of Newtonsoft Json.Net to serialize a complex type returned by my WCF web service. For some reason, if I apply the <code>[JsonProperty]</code> attribute, the fields simply don't serialize:</p> <pre><code> [DataContract] [JsonObject(MemberSerialization = Newtonsoft.Json.MemberSerialization.OptIn)] public class ScalarResult { [JsonProperty(Order = 0)] public string QueryId { get; set; } [JsonProperty(Order = 1)] public float CurrentPeriodValue { get; set; } [JsonProperty(Order = 2)] public bool HasPriorValue { get; set; } [JsonProperty(Order = 3)] public float PriorPeriodValue { get; set; } [JsonProperty(Order = 4)] public float ChangeOverPrior { get; set; } [JsonProperty(Order = 5)] public float ChangeOverPriorPercent { get; set; } </code></pre> <p>This results in a skimpy return object:</p> <pre><code>{ "SomethingElse": "why am I the only thing visible?" } </code></pre> <p>If I add <code>[DataMember]</code> to each field (which <a href="http://james.newtonking.com/json/help/index.html?topic=html/SerializationAttributes.htm" rel="nofollow">I'm not supposed to have to do</a>, according to the the Json.NET docs), then the fields show up, but the <code>(Order = x)</code> attribute is ignored, leading me to believe Json.NET might not actually be doing the serialization:</p> <pre><code>[DataContract] [JsonObject(MemberSerialization = Newtonsoft.Json.MemberSerialization.OptIn)] public class ScalarResult { [DataMember] [JsonProperty(Order = 0)] public string QueryId { get; set; } [DataMember] public string SomethingElse { get; set; } [DataMember] [JsonProperty(Order = 1)] public float CurrentPeriodValue { get; set; } [DataMember] [JsonProperty(Order = 2)] public bool HasPriorValue { get; set; } [DataMember] [JsonProperty(Order = 3)] public float PriorPeriodValue { get; set; } [DataMember] [JsonProperty(Order = 4)] public float ChangeOverPrior { get; set; } [DataMember] [JsonProperty(Order = 5)] public float ChangeOverPriorPercent { get; set; } </code></pre> <p>Which results in (<em>the wrong order</em>):</p> <pre><code>{ "ChangeOverPrior": 8, "ChangeOverPriorPercent": 0.25, "CurrentPeriodValue": 40, "HasPriorValue": true, "PriorPeriodValue": 32, "QueryId": "CitiesMonitored_count", "SomethingElse": "why am I the only thing visible?" } </code></pre> <p>Any ideas on how I can verify that Json.Net is doing the serialization and, if so, why the <code>(Order =)</code> property is being ignored?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload