Note that there are some explanatory texts on larger screens.

plurals
  1. POSplitting a complex object to two separated key and value collections
    text
    copied!<p>I'm trying to implement the C# aspect of a LightWeight JSON Spec <a href="https://github.com/itechnology/JsonRaw" rel="nofollow">JsonR</a>, but cannot get my head around any kind of recursion :-/ If anyone could help out here it would be more than greatly appreciated.</p> <pre><code>// Mockup class public class User { public string Name { get; set; } public int Age { get; set; } public List&lt;string&gt; Photos { get; set; } public List&lt;Friend&gt; Friends { get; set; } } // Mockup class public class Friend { public string FirstName { get; set; } public string LastName { get; set; } } // Initialize objects var userList = new List&lt;User&gt;(); userList.Add(new User() { Name = "Robert", Age = 32, Photos = new List&lt;string&gt; { "1.jpg", "2.jpg" }, Friends = new List&lt;Friend&gt;() { new Friend() { FirstName = "Bob", LastName = "Hope"}, new Friend() { FirstName = "Mr" , LastName = "T"} } }); userList.Add(new User() { Name = "Jane", Age = 21, Photos = new List&lt;string&gt; { "4.jpg", "5.jpg" }, Friends = new List&lt;Friend&gt;() { new Friend() { FirstName = "Foo" , LastName = "Bar"}, new Friend() { FirstName = "Lady" , LastName = "Gaga"} } }); </code></pre> <p>The idea behind it all is to now take the above object and split it into 2 separate collections, one containing the keys, and the other containing the values. Like this we can eventually only send the values over the wire thus saving lots of bandwidth, and then recombine it on the client (a js implementation for recombining exists already)</p> <p>If all went well we should be able to get this out of the above object</p> <pre><code>var keys = new object[] { "Name", "Age", "Photos", new { Friends = new [] {"FirstName", "LastName"}}}; var values = new [] { new object[] {"Robert", 32, new [] {"1.jpg", "2.jpg"}, new [] { new [] {"Bob", "Hope"}, new [] {"Mr", "T"}}}, new object[] {"Jane", 21, new [] {"4.jpg", "5.jpg"}, new [] { new [] {"Foo", "Bar"}, new [] {"Lady", "Gaga"}}}}; </code></pre> <p>As a verification we can test the conformity of the result with</p> <pre><code>Newtonsoft.Json.JsonConvert.SerializeObject(keys).Dump("keys"); // Generates: // ["Name","Age","Photos",{"Friends":["FirstName","LastName"]}] Newtonsoft.Json.JsonConvert.SerializeObject(values).Dump("values"); // Generates: // [["Robert",32,["1.jpg","2.jpg"],[["Bob","Hope"],["Mr","T"]]],["Jane",21,["4.jpg","5.jpg"],[["Foo","Bar"],["Lady","Gaga"]]]] </code></pre> <p>A shortcut i explored was to take advantage of Newton's JArray/JObject facilities like this</p> <pre><code>var JResult = Newtonsoft.Json.JsonConvert.DeserializeObject( Newtonsoft.Json.JsonConvert.SerializeObject(userList)); </code></pre> <p>Like this we end up with a sort of array object that we can already start iterating on</p> <p>Anyone think they can crack this in a memory/speed efficient way ?</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