Note that there are some explanatory texts on larger screens.

plurals
  1. PONested class serialization
    text
    copied!<p>I'd like to serialize (in JSON format) a list of object where each object has a property of another type of object. This is what I've got so far:</p> <pre><code>[DataContract] public class Person{ [DataMember] public string Name { get; set; } [DataMember] public Address FullAddress { get; set; } } [DataContract] public class Address { private readonly byte[] _foo; private ulong _value; public byte[] Foo { get { return (byte[]) _foo.Clone(); }} public ulong Value { get { return _value; } set { return _value; }} public Address(byte [] bytes){ _foo = new byte[bytes.Length]; Array.Copy(bytes, _foo, bytes.Length); foreach(byte b in _foo){ _value |= b; // I do some bit manipulation here and modify the _value } } public MacAddress() // added this otherwise I get an exception { } } </code></pre> <p>this is how I'm serializing and deserializing:</p> <pre><code>public class MyJson{ public MyJson(){ var list = new List&lt;Person&gt;{ /* added a bunch of person here */ }; var serializer = new JavaScriptSerializer(); string json = serializer.Serialize(list); // serialization works fine var desList = serializer.Deserialize&lt;IList&lt;Person&gt;&gt;(json); // the deserialization doesn't properly deserialize Address property. } } </code></pre> <p>As commented above, the serialization works fine but deserialization doesn't deserialize Address properly. I get a number for <strong>Value</strong> property (as expected) but not for <strong>Foo</strong> (I know it is missing a setter but what if, for some reason, I cannot put a setter?).</p> <p>What am I missing here?</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