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
10 changes: 10 additions & 0 deletions sandbox/DynamicCodeDumper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,13 @@ public EntityBase()
//// }
////}
}


#pragma warning disable

namespace MessagePack.Internal
{
internal sealed class PreserveAttribute : System.Attribute
{
}
}
29 changes: 29 additions & 0 deletions src/MessagePack.UnityClient/Assets/Scripts/Tests/IL2CPPTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#nullable enable

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using MessagePack;
using NUnit.Framework;
Expand Down Expand Up @@ -73,6 +75,33 @@ public void LZ4BlockArray()
var ys = MessagePackSerializer.Deserialize<MyClass[]>(bin, lz4Option);
CollectionAssert.AreEqual(xs, ys);
}

[Test]
public void EnumSerialize()
{
var bin = MessagePackSerializer.Serialize(MessagePackCompression.Lz4BlockArray);
var v2 = MessagePackSerializer.Deserialize<MessagePackCompression>(bin);

Assert.AreEqual(MessagePackCompression.Lz4BlockArray, v2);
}

[Test]
public void ListSerialize()
{
var bin = MessagePackSerializer.Serialize(new List<ulong> { 10UL });
var v2 = MessagePackSerializer.Deserialize<List<ulong>>(bin);

CollectionAssert.AreEqual(new List<ulong> { 10UL }, v2);
}

[Test]
public void ConcurrentBagSerialize()
{
var bin = MessagePackSerializer.Serialize(new ConcurrentBag<ulong> { 10UL });
var v2 = MessagePackSerializer.Deserialize<ConcurrentBag<ulong>>(bin);

CollectionAssert.AreEqual(new ConcurrentBag<ulong> { 10UL }, v2);
}
}

[MessagePackObject]
Expand Down
31 changes: 31 additions & 0 deletions src/MessagePack/Formatters/CollectionFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using MessagePack.Internal;

#pragma warning disable SA1402 // File may only contain a single type
#pragma warning disable SA1649 // File name should match first type name

namespace MessagePack.Formatters
{
[Preserve]
public sealed class ArrayFormatter<T> : IMessagePackFormatter<T[]?>
{
public void Serialize(ref MessagePackWriter writer, T[]? value, MessagePackSerializerOptions options)
Expand Down Expand Up @@ -158,6 +160,7 @@ public ArraySegment<byte> Deserialize(ref MessagePackReader reader, MessagePackS
}
}

[Preserve]
public sealed class MemoryFormatter<T> : IMessagePackFormatter<Memory<T>>
{
public void Serialize(ref MessagePackWriter writer, Memory<T> value, MessagePackSerializerOptions options)
Expand All @@ -172,6 +175,7 @@ public Memory<T> Deserialize(ref MessagePackReader reader, MessagePackSerializer
}
}

[Preserve]
public sealed class ReadOnlyMemoryFormatter<T> : IMessagePackFormatter<ReadOnlyMemory<T>>
{
public void Serialize(ref MessagePackWriter writer, ReadOnlyMemory<T> value, MessagePackSerializerOptions options)
Expand All @@ -194,6 +198,7 @@ public ReadOnlyMemory<T> Deserialize(ref MessagePackReader reader, MessagePackSe
}
}

[Preserve]
public sealed class ReadOnlySequenceFormatter<T> : IMessagePackFormatter<ReadOnlySequence<T>>
{
public void Serialize(ref MessagePackWriter writer, ReadOnlySequence<T> value, MessagePackSerializerOptions options)
Expand All @@ -218,6 +223,7 @@ public ReadOnlySequence<T> Deserialize(ref MessagePackReader reader, MessagePack
}
}

[Preserve]
public sealed class ArraySegmentFormatter<T> : IMessagePackFormatter<ArraySegment<T>>
{
public void Serialize(ref MessagePackWriter writer, ArraySegment<T> value, MessagePackSerializerOptions options)
Expand Down Expand Up @@ -248,6 +254,7 @@ public ArraySegment<T> Deserialize(ref MessagePackReader reader, MessagePackSeri
}

// List<T> is popular format, should avoid abstraction.
[Preserve]
public sealed class ListFormatter<T> : IMessagePackFormatter<List<T>?>
{
public void Serialize(ref MessagePackWriter writer, List<T>? value, MessagePackSerializerOptions options)
Expand Down Expand Up @@ -464,6 +471,7 @@ protected sealed override TCollection Complete(TCollection intermediateCollectio
}
}

[Preserve]
public sealed class GenericCollectionFormatter<TElement, TCollection> : CollectionFormatterBase<TElement, TCollection>
where TCollection : ICollection<TElement>, new()
{
Expand All @@ -478,6 +486,7 @@ protected override void Add(TCollection collection, int index, TElement value, M
}
}

[Preserve]
public sealed class GenericEnumerableFormatter<TElement, TCollection> : CollectionFormatterBase<TElement, TElement[], TCollection>
where TCollection : IEnumerable<TElement>
{
Expand All @@ -497,6 +506,7 @@ protected override TCollection Complete(TElement[] intermediateCollection)
}
}

[Preserve]
public sealed class LinkedListFormatter<T> : CollectionFormatterBase<T, LinkedList<T>, LinkedList<T>.Enumerator, LinkedList<T>>
{
protected override void Add(LinkedList<T> collection, int index, T value, MessagePackSerializerOptions options)
Expand All @@ -520,6 +530,7 @@ protected override LinkedList<T>.Enumerator GetSourceEnumerator(LinkedList<T> so
}
}

[Preserve]
public sealed class QueueFormatter<T> : CollectionFormatterBase<T, Queue<T>, Queue<T>.Enumerator, Queue<T>>
{
protected override int? GetCount(Queue<T> sequence)
Expand Down Expand Up @@ -640,6 +651,7 @@ public void Serialize(ref MessagePackWriter writer, PriorityQueue<TElement, TPri
#endif

// should deserialize reverse order.
[Preserve]
public sealed class StackFormatter<T> : CollectionFormatterBase<T, T[], Stack<T>.Enumerator, Stack<T>>
{
protected override int? GetCount(Stack<T> sequence)
Expand Down Expand Up @@ -669,6 +681,7 @@ protected override Stack<T> Complete(T[] intermediateCollection)
}
}

[Preserve]
public sealed class HashSetFormatter<T> : CollectionFormatterBase<T, HashSet<T>, HashSet<T>.Enumerator, HashSet<T>>
{
protected override int? GetCount(HashSet<T> sequence)
Expand Down Expand Up @@ -697,6 +710,7 @@ protected override HashSet<T>.Enumerator GetSourceEnumerator(HashSet<T> source)
}
}

[Preserve]
public sealed class ReadOnlyCollectionFormatter<T> : CollectionFormatterBase<T, T[], ReadOnlyCollection<T>>
{
protected override void Add(T[] collection, int index, T value, MessagePackSerializerOptions options)
Expand Down Expand Up @@ -753,6 +767,7 @@ protected override ICollection<T> Complete(T[] intermediateCollection)
}
}

[Preserve]
public sealed class InterfaceListFormatter2<T> : CollectionFormatterBase<T, List<T>, IList<T>>
{
protected override void Add(List<T> collection, int index, T value, MessagePackSerializerOptions options)
Expand All @@ -771,6 +786,7 @@ protected override IList<T> Complete(List<T> intermediateCollection)
}
}

[Preserve]
public sealed class InterfaceCollectionFormatter2<T> : CollectionFormatterBase<T, List<T>, ICollection<T>>
{
protected override void Add(List<T> collection, int index, T value, MessagePackSerializerOptions options)
Expand All @@ -789,6 +805,7 @@ protected override ICollection<T> Complete(List<T> intermediateCollection)
}
}

[Preserve]
public sealed class InterfaceEnumerableFormatter<T> : CollectionFormatterBase<T, T[], IEnumerable<T>>
{
protected override void Add(T[] collection, int index, T value, MessagePackSerializerOptions options)
Expand All @@ -808,6 +825,7 @@ protected override IEnumerable<T> Complete(T[] intermediateCollection)
}

// [Key, [Array]]
[Preserve]
public sealed class InterfaceGroupingFormatter<TKey, TElement> : IMessagePackFormatter<IGrouping<TKey, TElement>?>
{
public void Serialize(ref MessagePackWriter writer, IGrouping<TKey, TElement>? value, MessagePackSerializerOptions options)
Expand Down Expand Up @@ -854,6 +872,7 @@ public void Serialize(ref MessagePackWriter writer, IGrouping<TKey, TElement>? v
}
}

[Preserve]
public sealed class InterfaceLookupFormatter<TKey, TElement> : CollectionFormatterBase<IGrouping<TKey, TElement>, Dictionary<TKey, IGrouping<TKey, TElement>>, ILookup<TKey, TElement>>
where TKey : notnull
{
Expand Down Expand Up @@ -947,6 +966,7 @@ IEnumerator IEnumerable.GetEnumerator()

/* NonGenerics */

[Preserve]
public sealed class NonGenericListFormatter<T> : IMessagePackFormatter<T?>
where T : class, IList, new()
{
Expand Down Expand Up @@ -998,6 +1018,7 @@ public void Serialize(ref MessagePackWriter writer, T? value, MessagePackSeriali
}
}

[Preserve]
public sealed class NonGenericInterfaceCollectionFormatter : IMessagePackFormatter<ICollection?>
{
public static readonly IMessagePackFormatter<ICollection?> Instance = new NonGenericInterfaceCollectionFormatter();
Expand Down Expand Up @@ -1058,6 +1079,7 @@ public void Serialize(ref MessagePackWriter writer, ICollection? value, MessageP
}
}

[Preserve]
public sealed class NonGenericInterfaceEnumerableFormatter : IMessagePackFormatter<IEnumerable?>
{
public static readonly IMessagePackFormatter<IEnumerable?> Instance = new NonGenericInterfaceEnumerableFormatter();
Expand Down Expand Up @@ -1199,6 +1221,7 @@ public void Serialize(ref MessagePackWriter writer, IList? value, MessagePackSer
}
}

[Preserve]
public sealed class NonGenericDictionaryFormatter<T> : IMessagePackFormatter<T?>
where T : class, IDictionary, new()
{
Expand Down Expand Up @@ -1312,6 +1335,7 @@ public void Serialize(ref MessagePackWriter writer, IDictionary? value, MessageP
}
}

[Preserve]
public sealed class ObservableCollectionFormatter<T> : CollectionFormatterBase<T, ObservableCollection<T>>
{
protected override void Add(ObservableCollection<T> collection, int index, T value, MessagePackSerializerOptions options)
Expand All @@ -1325,6 +1349,7 @@ protected override ObservableCollection<T> Create(int count, MessagePackSerializ
}
}

[Preserve]
public sealed class ReadOnlyObservableCollectionFormatter<T> : CollectionFormatterBase<T, ObservableCollection<T>, ReadOnlyObservableCollection<T>>
{
protected override void Add(ObservableCollection<T> collection, int index, T value, MessagePackSerializerOptions options)
Expand All @@ -1343,6 +1368,7 @@ protected override ReadOnlyObservableCollection<T> Complete(ObservableCollection
}
}

[Preserve]
public sealed class InterfaceReadOnlyListFormatter<T> : CollectionFormatterBase<T, T[], IReadOnlyList<T>>
{
protected override void Add(T[] collection, int index, T value, MessagePackSerializerOptions options)
Expand All @@ -1361,6 +1387,7 @@ protected override IReadOnlyList<T> Complete(T[] intermediateCollection)
}
}

[Preserve]
public sealed class InterfaceReadOnlyCollectionFormatter<T> : CollectionFormatterBase<T, T[], IReadOnlyCollection<T>>
{
protected override void Add(T[] collection, int index, T value, MessagePackSerializerOptions options)
Expand All @@ -1379,6 +1406,7 @@ protected override IReadOnlyCollection<T> Complete(T[] intermediateCollection)
}
}

[Preserve]
public sealed class InterfaceSetFormatter<T> : CollectionFormatterBase<T, HashSet<T>, ISet<T>>
{
protected override void Add(HashSet<T> collection, int index, T value, MessagePackSerializerOptions options)
Expand Down Expand Up @@ -1441,6 +1469,7 @@ protected override HashSet<T> Create(int count, MessagePackSerializerOptions opt

#endif

[Preserve]
public sealed class ConcurrentBagFormatter<T> : CollectionFormatterBase<T, System.Collections.Concurrent.ConcurrentBag<T>>
{
protected override int? GetCount(ConcurrentBag<T> sequence)
Expand All @@ -1459,6 +1488,7 @@ protected override ConcurrentBag<T> Create(int count, MessagePackSerializerOptio
}
}

[Preserve]
public sealed class ConcurrentQueueFormatter<T> : CollectionFormatterBase<T, System.Collections.Concurrent.ConcurrentQueue<T>>
{
protected override int? GetCount(ConcurrentQueue<T> sequence)
Expand All @@ -1477,6 +1507,7 @@ protected override ConcurrentQueue<T> Create(int count, MessagePackSerializerOpt
}
}

[Preserve]
public sealed class ConcurrentStackFormatter<T> : CollectionFormatterBase<T, T[], ConcurrentStack<T>>
{
protected override int? GetCount(ConcurrentStack<T> sequence)
Expand Down
10 changes: 10 additions & 0 deletions src/MessagePack/Formatters/DictionaryFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using MessagePack.Internal;

#pragma warning disable SA1402 // File may only contain a single type
#pragma warning disable SA1649 // File name should match first type name
Expand Down Expand Up @@ -141,6 +142,7 @@ protected override TDictionary Complete(TDictionary intermediateCollection)
}
}

[Preserve]
public sealed class DictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, Dictionary<TKey, TValue>.Enumerator, Dictionary<TKey, TValue>>
where TKey : notnull
{
Expand All @@ -165,6 +167,7 @@ protected override Dictionary<TKey, TValue>.Enumerator GetSourceEnumerator(Dicti
}
}

[Preserve]
public sealed class GenericDictionaryFormatter<TKey, TValue, TDictionary> : DictionaryFormatterBase<TKey, TValue, TDictionary>
where TDictionary : class?, IDictionary<TKey, TValue>, new()
where TKey : notnull
Expand All @@ -180,6 +183,7 @@ protected override TDictionary Create(int count, MessagePackSerializerOptions op
}
}

[Preserve]
public sealed class GenericReadOnlyDictionaryFormatter<TKey, TValue, TDictionary> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, TDictionary>
where TDictionary : class?, IReadOnlyDictionary<TKey, TValue>
where TKey : notnull
Expand All @@ -200,6 +204,7 @@ protected override TDictionary Complete(Dictionary<TKey, TValue> intermediateCol
}
}

[Preserve]
public sealed class InterfaceDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, IDictionary<TKey, TValue>>
where TKey : notnull
{
Expand All @@ -219,6 +224,7 @@ protected override IDictionary<TKey, TValue> Complete(Dictionary<TKey, TValue> i
}
}

[Preserve]
public sealed class SortedListFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, SortedList<TKey, TValue>>
where TKey : notnull
{
Expand All @@ -233,6 +239,7 @@ protected override SortedList<TKey, TValue> Create(int count, MessagePackSeriali
}
}

[Preserve]
public sealed class SortedDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, SortedDictionary<TKey, TValue>, SortedDictionary<TKey, TValue>.Enumerator, SortedDictionary<TKey, TValue>>
where TKey : notnull
{
Expand All @@ -257,6 +264,7 @@ protected override SortedDictionary<TKey, TValue>.Enumerator GetSourceEnumerator
}
}

[Preserve]
public sealed class ReadOnlyDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, ReadOnlyDictionary<TKey, TValue>>
where TKey : notnull
{
Expand All @@ -276,6 +284,7 @@ protected override Dictionary<TKey, TValue> Create(int count, MessagePackSeriali
}
}

[Preserve]
public sealed class InterfaceReadOnlyDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue>>
where TKey : notnull
{
Expand All @@ -295,6 +304,7 @@ protected override Dictionary<TKey, TValue> Create(int count, MessagePackSeriali
}
}

[Preserve]
public sealed class ConcurrentDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>>
where TKey : notnull
{
Expand Down
1 change: 1 addition & 0 deletions src/MessagePack/Formatters/EnumAsStringFormatter`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace MessagePack.Formatters
{
// Note:This implementation is 'not' fastest, should more improve.
[Preserve]
public sealed class EnumAsStringFormatter<T> : IMessagePackFormatter<T>
where T : struct, Enum
{
Expand Down
Loading