Runtime: Json serializer support for collections

Created on 6 Apr 2019  路  7Comments  路  Source: dotnet/runtime

This issue is to add built-in support for specific collections including:

System.Collections.Generic

  • [x] IEnumerable<T>
  • [x] ICollection<T>
  • [x] IList<T>
  • [x] IReadOnlyCollection<T>
  • [x] IReadOnlyList<T>
  • [x] ISet<T>
  • [x] Stack<T>
  • [x] Queue<T>
  • [x] HashSet<T>
  • [x] LinkedList<T>
  • [x] SortedSet<T>
  • [x] SortedDictionary<TKey, TValue>
  • [x] KeyValuePair<TKey, TValue>

Future KeyedByCollection<TItem>, LinkedListNode<T>, SynchronizedCollection<T>, SynchronizedKeyCollection<K, T>, SynchronizedReadOnlyCollection<T>

Sytem.Collections

  • [x] IEnumerable
  • [x] ICollection
  • [x] IList
  • [x] IDictionary
  • [x] Stack
  • [x] Queue
  • [x] Hashtable
  • [x] ArrayList
  • [x] SortedList

Future: BitArray

System.Collections.Immutable

  • [x] IImmutableList<T>
  • [x] IImmutableQueue<T>
  • [x] IImmutableSet<T>
  • [x] IImmutableStack<T>
  • [x] IImmutableDictionary<TKey, TValue>
  • [x] ImmutableArray<T>
  • [x] ImmutableHashSet<T>
  • [x] ImmutableList<T>
  • [x] ImmutableQueue<T>
  • [x] ImmutableSortedSet<T>
  • [x] ImmutableStack<T>
  • [x] ImmutableDictionary<TKey, TValue>
  • [x] 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.

area-System.Text.Json blocking disabled-test enhancement

All 7 comments

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.");
}

@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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matty-hall picture matty-hall  路  3Comments

omariom picture omariom  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments

btecu picture btecu  路  3Comments

sahithreddyk picture sahithreddyk  路  3Comments