Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Define a simple Crosshairs type that is a wrapper for a string, object dictionary:</p> <pre><code>class CrosshairsCollection : List&lt;Crosshairs&gt; { } class Crosshairs { private Dictionary&lt;string, object&gt; dict = new Dictionary&lt;string,object&gt;(); public IEnumerable&lt;KeyValuePair&lt;string, object&gt;&gt; GetAllProperties() { foreach (string key in dict.Keys) { yield return new KeyValuePair&lt;string, object&gt;(key, dict[key]); } } public object GetProperty(string s) { object value; bool exists = dict.TryGetValue(s, out value); if (!exists) { value = null; } return value; } public void SetProperty(string s, object o) { if (!dict.ContainsKey(s)) { dict.Add(s, o); } else { dict[s] = o; } } } </code></pre> <p>Then implement a JavaScriptConverter for CrosshairsCollection, similar to this: <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptconverter.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptconverter.aspx</a></p> <p>Replace your List with CrosshairsCollection and register the JavaScriptConverter with your JavaScriptSerializer instance. </p> <p>[<strong>Edit:</strong> answers to follow up questions]</p> <p>I'm assuming that you have two follow-up questions, both numbered 1, and that the second one is "how do I get the JSON output to be more like my original example?" :)</p> <p>The way I interpreted your original XML was that a tooltip has multiple crosshairs, and not a single crosshairs with multiple, overlapping property definitions. The representation in the XML and your name usage in the code aren't in accord though: you call the entire thing "crosshairs." Bear that in mind as you're reading my original and edited response.</p> <p>So to get an object that mimics the original XML, I would have expected you to provide the properties like so:</p> <pre><code>CrosshairsCollection crosshairsList = new CrosshairsCollection(); Crosshairs c1 = new Crosshairs(); c1.SetProperty("width", 3); c1.SetProperty("color", "green"); c1.SetProperty("padding", "5px"); crosshairsList.Add(c1); Crosshairs c2 = new Crosshairs(); c2.SetProperty("width", 3); c2.SetProperty("color", "blue"); crosshairsList.Add(c2); </code></pre> <p>Take that one step further, turn each Crosshairs initialization into a factory method, and plug it into your XML representation, like you're doing in the <code>HCToolTip</code> constructor. </p> <p>Then, you can add each name value pair into the JSON output as they are:</p> <pre><code>foreach (Crosshairs crosshairs in crosshairsList) { Dictionary&lt;string, object&gt; crosshairProps = new Dictionary&lt;string, object&gt;(); foreach (KeyValuePair&lt;string, object&gt; prop in crosshairs.GetAllProperties()) { crosshairProps.Add(prop.Key, prop.Value); } itemsList.Add(crosshairProps); } result["crosshairs"] = itemsList; </code></pre> <p>You might need to implement a JavaScriptConverter one level higher up, on ToolTip, in order to get an anonymous list as the property value for <code>ToolTip.crosshairs</code>. </p> <p>I hope what this is showing though is that really all you're looking for is a key/value collection. I named the classes <code>CrosshairsCollection</code> and <code>Crosshairs</code> to try to draw the distinction between a list of <code>&lt;crosshairs&gt;</code> tags and the subtree of a <code>&lt;crosshairs&gt;</code> tag. But you could name them <code>MultiPropertyItemCollection</code> and <code>MultiPropertyItem</code>, or whatever, because there's nothing type specific in the implementation.</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