@@ -491,6 +491,134 @@ private static TValue ReadValueElement(
491491 }
492492}
493493
494+ public sealed class IDictionarySerializer < TKey , TValue > : Serializer < IDictionary < TKey , TValue > >
495+ where TKey : notnull
496+ {
497+ public override IDictionary < TKey , TValue > DefaultValue => null ! ;
498+
499+ public override void WriteData ( WriteContext context , in IDictionary < TKey , TValue > value , bool hasGenerics )
500+ {
501+ if ( NativeCollectionTypeCodec . Enabled ( context . TypeResolver ) )
502+ {
503+ NativeCollectionTypeId typeId = NativeCollectionTypeId . Dictionary ;
504+ if ( value is not null &&
505+ NativeCollectionTypeCodec . TryGetTypeId ( value . GetType ( ) , out NativeCollectionTypeId runtimeTypeId ) &&
506+ runtimeTypeId is
507+ NativeCollectionTypeId . Dictionary or
508+ NativeCollectionTypeId . SortedDictionary or
509+ NativeCollectionTypeId . SortedList or
510+ NativeCollectionTypeId . ConcurrentDictionary or
511+ NativeCollectionTypeId . NullableKeyDictionary )
512+ {
513+ typeId = runtimeTypeId ;
514+ NativeCollectionTypeCodec . WriteTypeId ( context , typeId ) ;
515+ TypeInfo typeInfo = context . TypeResolver . GetTypeInfo ( value . GetType ( ) ) ;
516+ context . TypeResolver . WriteDataObject ( typeInfo , context , value , hasGenerics ) ;
517+ return ;
518+ }
519+
520+ NativeCollectionTypeCodec . WriteTypeId ( context , typeId ) ;
521+ Dictionary < TKey , TValue > nativeFallback = value as Dictionary < TKey , TValue > ?? ( value is null ? [ ] : new Dictionary < TKey , TValue > ( value ) ) ;
522+ context . TypeResolver . GetSerializer < Dictionary < TKey , TValue > > ( ) . WriteData ( context , nativeFallback , hasGenerics ) ;
523+ return ;
524+ }
525+
526+ if ( value is not null &&
527+ NativeCollectionTypeCodec . TryGetTypeId ( value . GetType ( ) , out NativeCollectionTypeId xlangTypeId ) &&
528+ xlangTypeId is
529+ NativeCollectionTypeId . Dictionary or
530+ NativeCollectionTypeId . SortedDictionary or
531+ NativeCollectionTypeId . SortedList or
532+ NativeCollectionTypeId . ConcurrentDictionary or
533+ NativeCollectionTypeId . NullableKeyDictionary )
534+ {
535+ TypeInfo typeInfo = context . TypeResolver . GetTypeInfo ( value . GetType ( ) ) ;
536+ context . TypeResolver . WriteDataObject ( typeInfo , context , value , hasGenerics ) ;
537+ return ;
538+ }
539+
540+ Dictionary < TKey , TValue > map = value as Dictionary < TKey , TValue > ?? ( value is null ? [ ] : new Dictionary < TKey , TValue > ( value ) ) ;
541+ context . TypeResolver . GetSerializer < Dictionary < TKey , TValue > > ( ) . WriteData ( context , map , hasGenerics ) ;
542+ }
543+
544+ public override IDictionary < TKey , TValue > ReadData ( ReadContext context )
545+ {
546+ if ( ! NativeCollectionTypeCodec . Enabled ( context . TypeResolver ) )
547+ {
548+ return context . TypeResolver . GetSerializer < Dictionary < TKey , TValue > > ( ) . ReadData ( context ) ;
549+ }
550+
551+ NativeCollectionTypeId typeId = NativeCollectionTypeCodec . ReadTypeId ( context ) ;
552+ Type concreteType = NativeCollectionTypeCodec . ResolveDictionaryType < TKey , TValue > ( typeId ) ;
553+ TypeInfo typeInfo = context . TypeResolver . GetTypeInfo ( concreteType ) ;
554+ return ( IDictionary < TKey , TValue > ) context . TypeResolver . ReadDataObject ( typeInfo , context ) ! ;
555+ }
556+ }
557+
558+ public sealed class IReadOnlyDictionarySerializer < TKey , TValue > : Serializer < IReadOnlyDictionary < TKey , TValue > >
559+ where TKey : notnull
560+ {
561+ public override IReadOnlyDictionary < TKey , TValue > DefaultValue => null ! ;
562+
563+ public override void WriteData ( WriteContext context , in IReadOnlyDictionary < TKey , TValue > value , bool hasGenerics )
564+ {
565+ if ( NativeCollectionTypeCodec . Enabled ( context . TypeResolver ) )
566+ {
567+ NativeCollectionTypeId typeId = NativeCollectionTypeId . Dictionary ;
568+ if ( value is not null &&
569+ NativeCollectionTypeCodec . TryGetTypeId ( value . GetType ( ) , out NativeCollectionTypeId runtimeTypeId ) &&
570+ runtimeTypeId is
571+ NativeCollectionTypeId . Dictionary or
572+ NativeCollectionTypeId . SortedDictionary or
573+ NativeCollectionTypeId . SortedList or
574+ NativeCollectionTypeId . ConcurrentDictionary or
575+ NativeCollectionTypeId . NullableKeyDictionary )
576+ {
577+ typeId = runtimeTypeId ;
578+ NativeCollectionTypeCodec . WriteTypeId ( context , typeId ) ;
579+ TypeInfo typeInfo = context . TypeResolver . GetTypeInfo ( value . GetType ( ) ) ;
580+ context . TypeResolver . WriteDataObject ( typeInfo , context , value , hasGenerics ) ;
581+ return ;
582+ }
583+
584+ NativeCollectionTypeCodec . WriteTypeId ( context , typeId ) ;
585+ Dictionary < TKey , TValue > nativeFallback = value as Dictionary < TKey , TValue > ?? ( value is null ? [ ] : new Dictionary < TKey , TValue > ( value ) ) ;
586+ context . TypeResolver . GetSerializer < Dictionary < TKey , TValue > > ( ) . WriteData ( context , nativeFallback , hasGenerics ) ;
587+ return ;
588+ }
589+
590+ if ( value is not null &&
591+ NativeCollectionTypeCodec . TryGetTypeId ( value . GetType ( ) , out NativeCollectionTypeId xlangTypeId ) &&
592+ xlangTypeId is
593+ NativeCollectionTypeId . Dictionary or
594+ NativeCollectionTypeId . SortedDictionary or
595+ NativeCollectionTypeId . SortedList or
596+ NativeCollectionTypeId . ConcurrentDictionary or
597+ NativeCollectionTypeId . NullableKeyDictionary )
598+ {
599+ TypeInfo typeInfo = context . TypeResolver . GetTypeInfo ( value . GetType ( ) ) ;
600+ context . TypeResolver . WriteDataObject ( typeInfo , context , value , hasGenerics ) ;
601+ return ;
602+ }
603+
604+ Dictionary < TKey , TValue > map = value as Dictionary < TKey , TValue > ?? ( value is null ? [ ] : new Dictionary < TKey , TValue > ( value ) ) ;
605+ context . TypeResolver . GetSerializer < Dictionary < TKey , TValue > > ( ) . WriteData ( context , map , hasGenerics ) ;
606+ }
607+
608+ public override IReadOnlyDictionary < TKey , TValue > ReadData ( ReadContext context )
609+ {
610+ if ( ! NativeCollectionTypeCodec . Enabled ( context . TypeResolver ) )
611+ {
612+ return context . TypeResolver . GetSerializer < Dictionary < TKey , TValue > > ( ) . ReadData ( context ) ;
613+ }
614+
615+ NativeCollectionTypeId typeId = NativeCollectionTypeCodec . ReadTypeId ( context ) ;
616+ Type concreteType = NativeCollectionTypeCodec . ResolveDictionaryType < TKey , TValue > ( typeId ) ;
617+ TypeInfo typeInfo = context . TypeResolver . GetTypeInfo ( concreteType ) ;
618+ return ( IReadOnlyDictionary < TKey , TValue > ) context . TypeResolver . ReadDataObject ( typeInfo , context ) ! ;
619+ }
620+ }
621+
494622public class DictionarySerializer < TKey , TValue > : DictionaryLikeSerializer < Dictionary < TKey , TValue > , TKey , TValue >
495623 where TKey : notnull
496624{
0 commit comments