22using System . Diagnostics . CodeAnalysis ;
33using System . Text ;
44using System . Text . Json ;
5+ using System . Text . Json . Nodes ;
56using System . Text . Json . Serialization . Metadata ;
67using Npgsql . Internal . Converters ;
78using Npgsql . Internal . Postgres ;
@@ -10,12 +11,12 @@ namespace Npgsql.Internal.Resolvers;
1011
1112[ RequiresUnreferencedCode ( "Json serializer may perform reflection on trimmed types." ) ]
1213[ RequiresDynamicCode ( "Serializing arbitary types to json can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling." ) ]
13- class SystemTextJsonPocoTypeInfoResolver : DynamicTypeInfoResolver , IPgTypeInfoResolver
14+ class SystemTextJsonDynamicTypeInfoResolver : DynamicTypeInfoResolver , IPgTypeInfoResolver
1415{
1516 protected TypeInfoMappingCollection Mappings { get ; } = new ( ) ;
1617 protected JsonSerializerOptions _serializerOptions ;
1718
18- public SystemTextJsonPocoTypeInfoResolver ( Type [ ] ? jsonbClrTypes = null , Type [ ] ? jsonClrTypes = null , JsonSerializerOptions ? serializerOptions = null )
19+ public SystemTextJsonDynamicTypeInfoResolver ( Type [ ] ? jsonbClrTypes = null , Type [ ] ? jsonClrTypes = null , JsonSerializerOptions ? serializerOptions = null )
1920 {
2021#if NET7_0_OR_GREATER
2122 _serializerOptions = serializerOptions ??= JsonSerializerOptions . Default ;
@@ -31,6 +32,20 @@ void AddMappings(TypeInfoMappingCollection mappings, Type[] jsonbClrTypes, Type[
3132 // We do GetTypeInfo calls directly so we need a resolver.
3233 serializerOptions . TypeInfoResolver ??= new DefaultJsonTypeInfoResolver ( ) ;
3334
35+ // These live in the RUC/RDC part as JsonValues can contain any .NET type.
36+ foreach ( var dataTypeName in new [ ] { DataTypeNames . Jsonb , DataTypeNames . Json } )
37+ {
38+ var jsonb = dataTypeName == DataTypeNames . Jsonb ;
39+ mappings . AddType < JsonNode > ( dataTypeName , ( options , mapping , _ ) =>
40+ mapping . CreateInfo ( options , new SystemTextJsonConverter < JsonNode , JsonNode > ( jsonb , options . TextEncoding , serializerOptions ) ) ) ;
41+ mappings . AddType < JsonObject > ( dataTypeName , ( options , mapping , _ ) =>
42+ mapping . CreateInfo ( options , new SystemTextJsonConverter < JsonObject , JsonObject > ( jsonb , options . TextEncoding , serializerOptions ) ) ) ;
43+ mappings . AddType < JsonArray > ( dataTypeName , ( options , mapping , _ ) =>
44+ mapping . CreateInfo ( options , new SystemTextJsonConverter < JsonArray , JsonArray > ( jsonb , options . TextEncoding , serializerOptions ) ) ) ;
45+ mappings . AddType < JsonValue > ( dataTypeName , ( options , mapping , _ ) =>
46+ mapping . CreateInfo ( options , new SystemTextJsonConverter < JsonValue , JsonValue > ( jsonb , options . TextEncoding , serializerOptions ) ) ) ;
47+ }
48+
3449 AddUserMappings ( jsonb : true , jsonbClrTypes ) ;
3550 AddUserMappings ( jsonb : false , jsonClrTypes ) ;
3651
@@ -62,6 +77,14 @@ protected void AddArrayInfos(TypeInfoMappingCollection mappings, TypeInfoMapping
6277 if ( baseMappings . Items . Count == 0 )
6378 return ;
6479
80+ foreach ( var dataTypeName in new [ ] { DataTypeNames . Jsonb , DataTypeNames . Json } )
81+ {
82+ mappings . AddArrayType < JsonNode > ( dataTypeName ) ;
83+ mappings . AddArrayType < JsonObject > ( dataTypeName ) ;
84+ mappings . AddArrayType < JsonArray > ( dataTypeName ) ;
85+ mappings . AddArrayType < JsonValue > ( dataTypeName ) ;
86+ }
87+
6588 var dynamicMappings = CreateCollection ( baseMappings ) ;
6689 foreach ( var mapping in baseMappings . Items )
6790 dynamicMappings . AddArrayMapping ( mapping . Type , mapping . DataTypeName ) ;
@@ -102,11 +125,11 @@ static PgConverter CreateSystemTextJsonConverter(Type valueType, bool jsonb, Enc
102125
103126[ RequiresUnreferencedCode ( "Json serializer may perform reflection on trimmed types." ) ]
104127[ RequiresDynamicCode ( "Serializing arbitary types to json can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling." ) ]
105- sealed class SystemTextJsonPocoArrayTypeInfoResolver : SystemTextJsonPocoTypeInfoResolver , IPgTypeInfoResolver
128+ sealed class SystemTextJsonDynamicArrayTypeInfoResolver : SystemTextJsonDynamicTypeInfoResolver , IPgTypeInfoResolver
106129{
107130 new TypeInfoMappingCollection Mappings { get ; }
108131
109- public SystemTextJsonPocoArrayTypeInfoResolver ( Type [ ] ? jsonbClrTypes = null , Type [ ] ? jsonClrTypes = null , JsonSerializerOptions ? serializerOptions = null )
132+ public SystemTextJsonDynamicArrayTypeInfoResolver ( Type [ ] ? jsonbClrTypes = null , Type [ ] ? jsonClrTypes = null , JsonSerializerOptions ? serializerOptions = null )
110133 : base ( jsonbClrTypes , jsonClrTypes , serializerOptions )
111134 {
112135 Mappings = new TypeInfoMappingCollection ( base . Mappings ) ;
0 commit comments