Some custom types that implement IEnumerable do not necessarily support collections. They can be generated dynamically, for example, using yield or LINQ. Here is an example:
public class SOJsonExample { public class MyCustomEnumerable : IEnumerable<KeyValuePair<int,float>> { public List<int> Keys { get; set; } public List<float> Values { get; set; } public MyCustomEnumerable() { Keys = new List<int> { 1, 2, 3 }; Values = new List<float> { 0.1f, 0.2f, 0.3f }; } public IEnumerator<KeyValuePair<int, float>> GetEnumerator() { var kvps = from key in Keys from value in Values select new KeyValuePair<int, float>(key, value); return kvps.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } } }
I found that the default serialization of Json.NET is to enumerate each value and store the values ββin a JavaScript array (which I don't want). Then, by default, the deserializer will not be able to deserialize the collection because it cannot be populated. In these cases, I would instead want Json.NET to skip serializing the default JavaScript array and just keep the class members.
All I want is my keys and values ββ- is there any quick access method, or do I need to implement my own serializer?
Checked this and none of which is exactly my question. I scanned the documentation , but did not find what I was looking for (maybe I was looking for the wrong place).
(Edit # 1 - improved clarity)
(Edit # 2 - answered my question ... see below)
David Cuccia
source share