3535#endif
3636
3737using System;
38+ using System.Collections.Generic;
3839
3940namespace MsgPack
4041{
@@ -115,8 +116,10 @@ private static readonly TypeCodeMapping[] TypeCodeMappings =
115116
116117private void WriteReadBody( string typeName, string fullTypeName, bool isForSubtree, Action bodyWriter )
117118{
119+ var firstParameter = ( isForSubtree && typeName == "Object" ) ? "bool isDeep, " : String.Empty;
120+ var firstArgument = ( !isForSubtree && typeName == "Object" ) ? "/* isDeep */true, " : String.Empty;
118121#>
119- <#= isForSubtree ? "internal" : "public override" #> bool Read<#= isForSubtree ? "Subtree" : String.Empty #><#= typeName #>( out <#= fullTypeName #> result )
122+ <#= isForSubtree ? "internal" : "public override" #> bool Read<#= isForSubtree ? "Subtree" : String.Empty #><#= typeName #>( <#= firstParameter #> out <#= fullTypeName #> result )
120123{
121124<#+
122125 if( !isForSubtree )
@@ -126,7 +129,7 @@ private void WriteReadBody( string typeName, string fullTypeName, bool isForSubt
126129 this.EnsureNotInSubtreeMode();
127130#endif // !UNITY
128131
129- return this.ReadSubtree<#= typeName #>( out result );
132+ return this.ReadSubtree<#= typeName #>( <#= firstArgument #> out result );
130133<#+
131134 }
132135 else
@@ -385,16 +388,69 @@ case ReadValueResult.<#= entry.Code #>:
385388<#+
386389 } // foreach -- scalar
387390
388- foreach( var collection in new [] { "Array", "Map" } )
389- {
391+ // Array/Map
390392#>
391- case ReadValueResult.<#= collection #>Length :
393+ case ReadValueResult.ArrayLength :
392394{
393- <#= resultVariable #> = <#= CastIfNecessary( typeof( long ), typeof( uint ), false, "this.Read"+ collection + "LengthCore( " + valueVariable.Integral + " )" ) #>;
395+ var length = <#= CastIfNecessary( typeof( long ), typeof( uint ), false, "this.ReadArrayLengthCore( " + valueVariable.Integral + " )", false ) #>;
396+ if ( !isDeep )
397+ {
398+ <#= resultVariable #> = length;
399+ return true;
400+ }
401+
402+ this.CheckLength( length, ReadValueResult.ArrayLength );
403+ var collection = new List<MessagePackObject>( unchecked( ( int ) length ) );
404+ for( var i = 0; i < length; i++ )
405+ {
406+ MessagePackObject item;
407+ if( !this.ReadSubtreeObject( /* isDeep */true, out item ) )
408+ {
409+ <#= resultVariable #> = default( MessagePackObject );
410+ return false;
411+ }
412+
413+ collection.Add( item );
414+ }
415+ <#= resultVariable #> = new MessagePackObject( collection, /* isImmutable */true );
394416 return true;
395417}
418+ case ReadValueResult.MapLength:
419+ {
420+ var length = <#= CastIfNecessary( typeof( long ), typeof( uint ), false, "this.ReadMapLengthCore( " + valueVariable.Integral + " )", false ) #>;
421+ if ( !isDeep )
422+ {
423+ <#= resultVariable #> = length;
424+ return true;
425+ }
426+
427+ this.CheckLength( length, ReadValueResult.MapLength );
428+ var collection = new MessagePackObjectDictionary( unchecked( ( int ) length ) );
429+ for( var i = 0; i < length; i++ )
430+ {
431+ MessagePackObject key;
432+ if( !this.ReadSubtreeObject( /* isDeep */true, out key ) )
433+ {
434+ <#= resultVariable #> = default( MessagePackObject );
435+ return false;
436+ }
437+
438+ MessagePackObject value;
439+ if( !this.ReadSubtreeObject( /* isDeep */true, out value ) )
440+ {
441+ <#= resultVariable #> = default( MessagePackObject );
442+ return false;
443+ }
444+
445+ collection.Add( key, value );
446+ }
447+ <#= resultVariable #> = new MessagePackObject( collection, /* isImmutable */true );
448+ return true;
449+ }
450+
396451<#+
397- } // foreach -- collection
452+ // Array/Map
453+
398454#>
399455case ReadValueResult.String:
400456{
0 commit comments