-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Comments
|
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 @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 |
…#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
@steveharter is there a plan to support |
@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
|
@layomia, there are still 8 tests (all appear to be immutable collections tests) disabled against this issue, e.g. runtime/src/libraries/System.Text.Json/tests/NewtonsoftTests/ImmutableCollectionsTests.cs Lines 90 to 92 in 04f2226
|
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:
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 implementsIEnumerable<T>
where the only public members are those used to implementIEnumerable<T>
. The internal class then forwards eachIEnumerable<T>
member call to an instance ofList<T>
which is stored in a private field and thus not accessible publically and not castable toList<T>
or other interfaces likeIList<T>
.System.Collections.Immutable.ImmutableArray
)UPDATE
Also support
System.Collections.Immutable
andSystem.Collections.Generic
(actual list TBD). These are not interfaces, but the same mechanism can be used to support them.The text was updated successfully, but these errors were encountered: