Deserialize mutable collection interfaces with mutable concrete types by AArnott · Pull Request #778 · MessagePack-CSharp/MessagePack-CSharp · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sandbox/Sandbox/Generated.cs
4 changes: 2 additions & 2 deletions src/MessagePack.GeneratorCore/CodeAnalysis/TypeCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ public class TypeCollector
{ "System.Collections.Generic.Stack<>", "global::MessagePack.Formatters.StackFormatter<TREPLACE>" },
{ "System.Collections.Generic.HashSet<>", "global::MessagePack.Formatters.HashSetFormatter<TREPLACE>" },
{ "System.Collections.ObjectModel.ReadOnlyCollection<>", "global::MessagePack.Formatters.ReadOnlyCollectionFormatter<TREPLACE>" },
{ "System.Collections.Generic.IList<>", "global::MessagePack.Formatters.InterfaceListFormatter<TREPLACE>" },
{ "System.Collections.Generic.ICollection<>", "global::MessagePack.Formatters.InterfaceCollectionFormatter<TREPLACE>" },
{ "System.Collections.Generic.IList<>", "global::MessagePack.Formatters.InterfaceListFormatter2<TREPLACE>" },
{ "System.Collections.Generic.ICollection<>", "global::MessagePack.Formatters.InterfaceCollectionFormatter2<TREPLACE>" },
{ "System.Collections.Generic.IEnumerable<>", "global::MessagePack.Formatters.InterfaceEnumerableFormatter<TREPLACE>" },
{ "System.Collections.Generic.Dictionary<,>", "global::MessagePack.Formatters.DictionaryFormatter<TREPLACE>" },
{ "System.Collections.Generic.IDictionary<,>", "global::MessagePack.Formatters.InterfaceDictionaryFormatter<TREPLACE>" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -452,6 +453,7 @@ protected override T[] Create(int count, MessagePackSerializerOptions options)
}
}

[Obsolete("Use " + nameof(InterfaceListFormatter2<int>) + " instead.")]
public sealed class InterfaceListFormatter<T> : CollectionFormatterBase<T, T[], IList<T>>
{
protected override void Add(T[] collection, int index, T value, MessagePackSerializerOptions options)
Expand All @@ -470,6 +472,7 @@ protected override IList<T> Complete(T[] intermediateCollection)
}
}

[Obsolete("Use " + nameof(InterfaceCollectionFormatter2<int>) + " instead.")]
public sealed class InterfaceCollectionFormatter<T> : CollectionFormatterBase<T, T[], ICollection<T>>
{
protected override void Add(T[] collection, int index, T value, MessagePackSerializerOptions options)
Expand All @@ -488,6 +491,42 @@ protected override ICollection<T> Complete(T[] intermediateCollection)
}
}

public sealed class InterfaceListFormatter2<T> : CollectionFormatterBase<T, List<T>, IList<T>>
{
protected override void Add(List<T> collection, int index, T value, MessagePackSerializerOptions options)
{
collection.Add(value);
}

protected override List<T> Create(int count, MessagePackSerializerOptions options)
{
return new List<T>(count);
}

protected override IList<T> Complete(List<T> intermediateCollection)
{
return intermediateCollection;
}
}

public sealed class InterfaceCollectionFormatter2<T> : CollectionFormatterBase<T, List<T>, ICollection<T>>
{
protected override void Add(List<T> collection, int index, T value, MessagePackSerializerOptions options)
{
collection.Add(value);
}

protected override List<T> Create(int count, MessagePackSerializerOptions options)
{
return new List<T>(count);
}

protected override ICollection<T> Complete(List<T> intermediateCollection)
{
return intermediateCollection;
}
}

public sealed class InterfaceEnumerableFormatter<T> : CollectionFormatterBase<T, T[], IEnumerable<T>>
{
protected override void Add(T[] collection, int index, T value, MessagePackSerializerOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ internal static class DynamicGenericResolverGetFormatterHelper
{ typeof(Stack<>), typeof(StackFormatter<>) },
{ typeof(HashSet<>), typeof(HashSetFormatter<>) },
{ typeof(ReadOnlyCollection<>), typeof(ReadOnlyCollectionFormatter<>) },
{ typeof(IList<>), typeof(InterfaceListFormatter<>) },
{ typeof(ICollection<>), typeof(InterfaceCollectionFormatter<>) },
{ typeof(IList<>), typeof(InterfaceListFormatter2<>) },
{ typeof(ICollection<>), typeof(InterfaceCollectionFormatter2<>) },
{ typeof(IEnumerable<>), typeof(InterfaceEnumerableFormatter<>) },
{ typeof(Dictionary<,>), typeof(DictionaryFormatter<,>) },
{ typeof(IDictionary<,>), typeof(InterfaceDictionaryFormatter<,>) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ public void InterfaceCollectionTest()
this.Convert(g).Is(g);
}

[Fact]
public void InterfaceCollectionsAreDeserializedMutable()
{
var list = this.Convert<IList<int>>(new[] { 1, 2, 3 });
list.Add(4);
Assert.Equal(new[] { 1, 2, 3, 4 }, list);

var collection = this.Convert<ICollection<int>>(new[] { 1, 2, 3 });
collection.Add(4);
Assert.Equal(new[] { 1, 2, 3, 4 }, collection);

var setCollection = this.Convert<ISet<int>>(new HashSet<int> { 1, 2, 3 });
setCollection.Add(4);
Assert.Equal(new[] { 1, 2, 3, 4 }, setCollection.OrderBy(n => n).ToArray());
}

[Fact]
public void StackTest()
{
Expand Down
4 changes: 4 additions & 0 deletions src/MessagePack/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
MessagePack.ExtensionHeader.Equals(MessagePack.ExtensionHeader other) -> bool
MessagePack.Formatters.InterfaceCollectionFormatter2<T>
MessagePack.Formatters.InterfaceCollectionFormatter2<T>.InterfaceCollectionFormatter2() -> void
MessagePack.Formatters.InterfaceListFormatter2<T>
MessagePack.Formatters.InterfaceListFormatter2<T>.InterfaceListFormatter2() -> void
MessagePack.MessagePackReader.ReadDateTime(MessagePack.ExtensionHeader header) -> System.DateTime
MessagePack.MessagePackReader.TryReadArrayHeader(out int count) -> bool
MessagePack.MessagePackReader.TryReadExtensionFormatHeader(out MessagePack.ExtensionHeader extensionHeader) -> bool
Expand Down
4 changes: 2 additions & 2 deletions src/MessagePackAnalyzer/TypeCollector.cs