Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserialization problem with DataContractJsonSerializer
    primarykey
    data
    text
    <p>I've got the following piece of JSON:</p> <pre><code>[{ "name": "numToRetrieve", "value": "3", "label": "Number of items to retrieve:", "items": { "1": "1", "3": "3", "5": "5" }, "rules": { "range": "1-2" } }, { "name": "showFoo", "value": "on", "label": "Show foo?" }, { "name": "title", "value": "Foo", "label": "Foo:" }] </code></pre> <p>All in one line version (suitable for a string literal):</p> <pre><code>[{\"name\":\"numToRetrieve\",\"value\":\"3\",\"label\":\"Number of items to retrieve:\",\"items\":{\"1\":\"1\",\"3\":\"3\",\"5\":\"5\"},\"rules\":{\"range\":\"1-2\"}},{\"name\":\"showFoo\",\"value\":\"on\",\"label\":\"Show foo?\"},{\"name\":\"title\",\"value\":\"Foo\",\"label\":\"Foo:\"}] </code></pre> <p>In the above example, <code>name</code>, <code>value</code>, and <code>label</code> are required but <code>items</code> and <code>rules</code> are optional.</p> <p>Here's the class I'm trying to deserialize into:</p> <pre><code>using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization; namespace foofoo { [DataContract] public sealed class FooDef { [DataMember(Name = "name", IsRequired = true)] public string Name { get; set; } [DataMember(Name = "value", IsRequired = true)] public string Value { get; set; } [DataMember(Name = "label", IsRequired = true)] public string Label { get; set; } [DataMember(Name = "items", IsRequired = false)] public Dictionary&lt;string, string&gt; Items { get; set; } [DataMember(Name = "rules", IsRequired = false)] public Dictionary&lt;string, string&gt; Rules { get; set; } } } </code></pre> <p>Here's the code I use to deserialize:</p> <pre><code>var json = new DataContractJsonSerializer(typeof(List&lt;FooDef&gt;)); var bar = "[{\"name\":\"numToRetrieve\",\"value\":\"3\",\"label\":\"Number of items to retrieve:\",\"items\":{\"1\":\"1\",\"3\":\"3\",\"5\":\"5\"},\"rules\":{\"range\":\"1-2\"}},{\"name\":\"showFoo\",\"value\":\"on\",\"label\":\"Show foo?\"},{\"name\":\"title\",\"value\":\"Foo\",\"label\":\"Foo:\"}]"; var stream = new MemoryStream(Encoding.UTF8.GetBytes(bar)); var foo = json.ReadObject(stream); stream.Close(); </code></pre> <p>Everything goes reasonably well except that <code>items</code> and <code>rules</code> are empty for the first <code>FooDef</code> pass. I have tried everything under the sun to try and get them populated: custom classes, <code>NameValueCollection</code>, <code>KeyValuePair&lt;string, string&gt;</code>, <code>List</code> of both of the latter, and every other collection that seemed to apply. [EDIT: I forgot to try <code>Hashtable</code>, which seemed like an obvious candidate. Didn't work.]</p> <p>The problem, as I see it, is that the key piece under <code>items</code> and <code>rules</code> is open-ended. That is, it's not always going to be called <code>range</code> or <code>3</code>. Any advice or ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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