This issue is to add built-in support for specific collections including:
System.Collections.Generic
IEnumerable<T>ICollection<T>IList<T>IReadOnlyCollection<T>IReadOnlyList<T>ISet<T>Stack<T>Queue<T>HashSet<T>LinkedList<T>SortedSet<T>SortedDictionary<TKey, TValue>KeyValuePair<TKey, TValue>Future KeyedByCollection<TItem>, LinkedListNode<T>, SynchronizedCollection<T>, SynchronizedKeyCollection<K, T>, SynchronizedReadOnlyCollection<T>
Sytem.Collections
IEnumerableICollectionIListIDictionaryStackQueueHashtableArrayListSortedListFuture: BitArray
System.Collections.Immutable
IImmutableList<T>IImmutableQueue<T>IImmutableSet<T>IImmutableStack<T>IImmutableDictionary<TKey, TValue>ImmutableArray<T>ImmutableHashSet<T>ImmutableList<T>ImmutableQueue<T>ImmutableSortedSet<T>ImmutableStack<T>ImmutableDictionary<TKey, TValue>ImmutableSortedDictionary<TKey, TValue>It should leverage the extensibility support for IEnumerable and register these interfaces.
Design notes:
1) No new public APIs are needed.
2) The instance returned should only implement the specific interface. It should not be possible to cast to our implementation. This ensures we can change the type later if necessary and prevents other general misuses.
For example, the likely implementation for IEnumerable<T> is an internal class that implements IEnumerable<T> where the only public members are those used to implement IEnumerable<T>. The internal class then forwards each IEnumerable<T> member call to an instance of List<T> which is stored in a private field and thus not accessible publically and not castable to List<T> or other interfaces like IList<T>.
3) We may want to also provide default implementations of some non-interfaces as well pending feedback (for example System.Collections.Immutable.ImmutableArray)
UPDATE
Also support System.Collections.Immutable and System.Collections.Generic (actual list TBD). These are not interfaces, but the same mechanism can be used to support them.
IReadOnlyList<T> as well. cc @BrennanConroy @davidfowl @rynowak
We have decided to move the "extensibility model" from https://github.com/dotnet/corefx/issues/36640 to future with the request of adding built-in support of various collections in System.Collections.Immutable and System.Collections.Generic (actual list TBD).
@layomia I suggest just keeping this issue open to track these other collection types, however you can open a new issue if you want.
cc @rynowak
@steveharter is there a plan to support IDictionary<,> as part of this work item?
@pakrym it's being tracked by https://github.com/dotnet/corefx/issues/36024 (both serialization and deserialization) cc @layomia
@layomia I have some tests with KeyValuePair which by @ahsonkhan should be implemented with this issue. Please add below tests after adding KeyValuePair implementation:
[Fact]
public static void DeserializeKeyValuePair()
{
KeyValuePair<string, int> keyValuePair = JsonSerializer.Parse<KeyValuePair<string, int>>(@"{""Key"":""myKey"", ""Value"":123}");
Assert.Equal(keyValuePair.Key, "myKey");
Assert.Equal(keyValuePair.Value, 123);
string json = JsonSerializer.ToString(keyValuePair);
Assert.Equal(json, @"{""Key"": ""myKey"",""Value"":123}");
}
[Fact]
public static void DeserializeUnexpectedEnd()
{
JsonReaderException e = Assert.Throws<JsonReaderException>(() => JsonSerializer.Parse<KeyValuePair<string, int>>(@"{""Key"": ""123"","));
Assert.Equal(e.Message, "Unexpected end when reading JSON. Path 'Key', line 1, position 14.");
}
Also supported but tracked elsewhere
Array
Dictionary<string, TValue> - https://github.com/dotnet/corefx/issues/36024, https://github.com/dotnet/corefx/issues/37077
IDictionary<string, TValue> - https://github.com/dotnet/corefx/issues/36024, https://github.com/dotnet/corefx/issues/37077
IReadOnlyDictionary<string, TValue> - https://github.com/dotnet/corefx/issues/36024, https://github.com/dotnet/corefx/issues/37077
@layomia, there are still 8 tests (all appear to be immutable collections tests) disabled against this issue, e.g.
https://github.com/dotnet/runtime/blob/04f22268efb4434b7cd81e8ae2c3354be99fd4a7/src/libraries/System.Text.Json/tests/NewtonsoftTests/ImmutableCollectionsTests.cs#L90-L92