diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs index 2b18677e1..f1550a985 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs @@ -78,24 +78,26 @@ public void Serialize(ref MessagePackWriter writer, TDictionary? value, MessageP } else { - IFormatterResolver resolver = options.Resolver; - IMessagePackFormatter? keyFormatter = resolver.GetFormatterWithVerify(); - IMessagePackFormatter? valueFormatter = resolver.GetFormatterWithVerify(); - var len = reader.ReadMapHeader(); TIntermediate dict = this.Create(len, options); options.Security.DepthStep(ref reader); try { - for (int i = 0; i < len; i++) + if (len > 0) { - reader.CancellationToken.ThrowIfCancellationRequested(); - TKey key = keyFormatter.Deserialize(ref reader, options); + IFormatterResolver resolver = options.Resolver; + IMessagePackFormatter? keyFormatter = resolver.GetFormatterWithVerify(); + IMessagePackFormatter? valueFormatter = resolver.GetFormatterWithVerify(); + for (int i = 0; i < len; i++) + { + reader.CancellationToken.ThrowIfCancellationRequested(); + TKey key = keyFormatter.Deserialize(ref reader, options); - TValue value = valueFormatter.Deserialize(ref reader, options); + TValue value = valueFormatter.Deserialize(ref reader, options); - this.Add(dict, i, key, value, options); + this.Add(dict, i, key, value, options); + } } } finally diff --git a/src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/DictionaryTest.cs b/src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/DictionaryTest.cs index cc4d3b078..9fc6fdc5c 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/DictionaryTest.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/DictionaryTest.cs @@ -53,12 +53,14 @@ public void InterfaceDictionaryTest() public void ConcurrentDictionaryTest() { var cd = new ConcurrentDictionary(); + ConcurrentDictionary conv = this.Convert(cd); + conv.Count.Is(0); cd.TryAdd(1, 100); cd.TryAdd(2, 200); cd.TryAdd(3, 300); - ConcurrentDictionary conv = this.Convert(cd); + conv = this.Convert(cd); conv[1].Is(100); conv[2].Is(200); conv[3].Is(300);