How do I skip default JavaScript array serialization for IEnumerable types in Json.Net?

后端 未结 1 1951
走了就别回头了
走了就别回头了 2020-12-18 01:10

Some custom types that implement IEnumerable don\'t necessarily have backing collections. They could be generated dynamically, for example using \'yield\' or LINQ. Here is a

相关标签:
1条回答
  • 2020-12-18 01:43

    Answered my own question - see the excellent documentation on IEnumerable, Lists, and Arrays:

    .NET lists (types that inherit from IEnumerable) and .NET arrays are converted to JSON arrays. Because JSON arrays only support a range of values and not properties, any additional properties and fields declared on .NET collections are not serialized. In situations where a JSON array is not wanted the JsonObjectAttribute can be placed on a .NET type that implements IEnumerable to force the type to be serialized as a JSON object instead.

    So, for my example:

    [JsonObject]
    public class MyCustomEnumerable : IEnumerable<KeyValuePair<int,float>>
    {
        ...
    }
    
    0 讨论(0)
提交回复
热议问题