Skip to content

Commit 0c8faae

Browse files
committed
Fix built-in MessagePackDictionarySerializer does not handle nested collection correctly. issue #42
1 parent e4cfdfa commit 0c8faae

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/MsgPack/Serialization/DefaultSerializers/MsgPack_MessagePackObjectDictionaryMessagePackSerializer.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,20 @@ private static void UnpackToCore( Unpacker unpacker, int count, MessagePackObjec
7575
throw SerializationExceptions.NewUnexpectedEndOfStream();
7676
}
7777

78-
collection.Add( key, unpacker.LastReadData );
78+
if ( unpacker.IsCollectionHeader )
79+
{
80+
MessagePackObject value;
81+
if ( !unpacker.UnpackSubtreeDataCore( out value ) )
82+
{
83+
throw SerializationExceptions.NewUnexpectedEndOfStream();
84+
}
85+
86+
collection.Add( key, value );
87+
}
88+
else
89+
{
90+
collection.Add( key, unpacker.LastReadData );
91+
}
7992
}
8093
}
8194
}

test/MsgPack.UnitTest/Serialization/MessagePackSerializerTTest.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,29 @@ public void TestIssue28()
541541
}
542542
}
543543

544+
[Test]
545+
public void TestIssue41()
546+
{
547+
using ( var buffer = new MemoryStream( new byte[] { 0x84, 0x01, 0x81, 0x0a, 0x14, 0x02, 0x93, 0x14, 0x1e, 0x28, 0x03, MessagePackCode.NilValue, 0x04, 0x0 } ) )
548+
//using ( var unpacker = Unpacker.Create( buffer ) )
549+
{
550+
//unpacker.Read();
551+
var serializer = MessagePackSerializer.Get<MessagePackObjectDictionary>();
552+
//var result = serializer.UnpackFrom( unpacker );
553+
var result = serializer.Unpack( buffer );
554+
Assert.That( result.Count, Is.EqualTo( 4 ) );
555+
Assert.That( result[ 1 ].AsDictionary().Count, Is.EqualTo( 1 ) );
556+
Assert.That( result[ 1 ].AsDictionary()[ 10 ], Is.EqualTo( ( MessagePackObject )0x14 ) );
557+
Assert.That( result[ 2 ].AsList().Count, Is.EqualTo( 3 ) );
558+
Assert.That( result[ 2 ].AsList()[ 0 ], Is.EqualTo( ( MessagePackObject )0x14 ) );
559+
Assert.That( result[ 2 ].AsList()[ 1 ], Is.EqualTo( ( MessagePackObject )0x1E ) );
560+
Assert.That( result[ 2 ].AsList()[ 2 ], Is.EqualTo( ( MessagePackObject )0x28 ) );
561+
Assert.That( result[ 3 ].IsNil );
562+
Assert.That( result[ 4 ], Is.EqualTo( ( MessagePackObject )0x0 ) );
563+
}
564+
565+
}
566+
544567
private void TestIssue10_Reader( Inner inner )
545568
{
546569
var serializer = CreateTarget<Outer>();
@@ -593,6 +616,6 @@ public class Inner
593616
public class WithReadOnlyProperty
594617
{
595618
public int Number { get; set; }
596-
public string AsString{get { return this.Number.ToString(); }}
619+
public string AsString { get { return this.Number.ToString(); } }
597620
}
598621
}

0 commit comments

Comments
 (0)