Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json serializer support for collections #29180

Closed
35 tasks done
steveharter opened this issue Apr 5, 2019 · 7 comments
Closed
35 tasks done

Json serializer support for collections #29180

steveharter opened this issue Apr 5, 2019 · 7 comments
Assignees
Labels
area-System.Text.Json blocking Marks issues that we want to fast track in order to unblock other important work disabled-test The test is disabled in source code against the issue enhancement Product code improvement that does NOT require public API changes/additions
Milestone

Comments

@steveharter
Copy link
Member

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

  • IEnumerable
  • ICollection
  • IList
  • IDictionary
  • Stack
  • Queue
  • Hashtable
  • ArrayList
  • SortedList

Future: 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>.

  1. 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.

@analogrelay
Copy link
Contributor

IReadOnlyList<T> as well. cc @BrennanConroy @davidfowl @rynowak

@steveharter
Copy link
Member Author

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

layomia referenced this issue in dotnet/corefx Apr 13, 2019
…#36756)

* Add support for generic interface-based collections in JsonSerializer

Specifically, add enumerable converters for:

* IEnumerable<T>
* ICollection<T>
* IList<T>
* IReadOnlyCollection<T>
* IReadOnlyList<T>

This partially addresses https://github.com/dotnet/corefx/issues/36643

* Address review comments

* Add serialization tests for generic interface collection as members of class objects
@pakrym
Copy link
Contributor

pakrym commented Apr 18, 2019

@steveharter is there a plan to support IDictionary<,> as part of this work item?

@pranavkm
Copy link
Contributor

@pakrym it's being tracked by https://github.com/dotnet/corefx/issues/36024 (both serialization and deserialization) cc @layomia

@layomia layomia changed the title Json serializer support for interface-based collections Json serializer support for collections Apr 23, 2019
@buyaa-n
Copy link
Contributor

buyaa-n commented May 21, 2019

@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 layomia closed this as completed Jun 18, 2019
@stephentoub
Copy link
Member

@layomia, there are still 8 tests (all appear to be immutable collections tests) disabled against this issue, e.g.

[ActiveIssue("https://github.com/dotnet/corefx/issues/36643")]
[Fact]
public void SerializeArray()

@stephentoub stephentoub reopened this Jan 23, 2020
@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 3.0 milestone Feb 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json blocking Marks issues that we want to fast track in order to unblock other important work disabled-test The test is disabled in source code against the issue enhancement Product code improvement that does NOT require public API changes/additions
Projects
None yet
Development

No branches or pull requests

8 participants