This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for generic interface-based collections in JsonSerializer (…
…#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
- Loading branch information
Showing
13 changed files
with
1,432 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...tem.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultEnumerableConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization.Policies; | ||
|
||
namespace System.Text.Json.Serialization.Converters | ||
{ | ||
internal class JsonEnumerableT<T> : ICollection<T>, IEnumerable<T>, IList<T>, IReadOnlyCollection<T>, IReadOnlyList<T> | ||
{ | ||
List<T> _list; | ||
|
||
public JsonEnumerableT(IList sourceList) | ||
{ | ||
// TODO: Change sourceList from IList to List<T> so we can do a direct assignment here. | ||
_list = new List<T>(); | ||
|
||
foreach (object item in sourceList) | ||
{ | ||
if (item is T itemT) | ||
{ | ||
_list.Add(itemT); | ||
} | ||
} | ||
} | ||
|
||
public T this[int index] { get => (T)_list[index]; set => _list[index] = value; } | ||
|
||
public int Count => _list.Count; | ||
|
||
public bool IsReadOnly => false; | ||
|
||
public void Add(T item) | ||
{ | ||
_list.Add(item); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
_list.Clear(); | ||
} | ||
|
||
public bool Contains(T item) | ||
{ | ||
return _list.Contains(item); | ||
} | ||
|
||
public void CopyTo(T[] array, int arrayIndex) | ||
{ | ||
_list.CopyTo(array, arrayIndex); | ||
} | ||
|
||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
return _list.GetEnumerator(); | ||
} | ||
|
||
public int IndexOf(T item) | ||
{ | ||
return _list.IndexOf(item); | ||
} | ||
|
||
public void Insert(int index, T item) | ||
{ | ||
_list.Insert(index, item); | ||
} | ||
|
||
public bool Remove(T item) | ||
{ | ||
return _list.Remove(item); | ||
} | ||
|
||
public void RemoveAt(int index) | ||
{ | ||
_list.RemoveAt(index); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
} | ||
|
||
internal sealed class DefaultEnumerableConverter : JsonEnumerableConverter | ||
{ | ||
public override IEnumerable CreateFromList(Type elementType, IList sourceList) | ||
{ | ||
Type t = typeof(JsonEnumerableT<>).MakeGenericType(elementType); | ||
return (IEnumerable)Activator.CreateInstance(t, sourceList); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.