Skip to content

Commit 4f2785b

Browse files
authored
Concrete INpgsqlTypeMapper return types (#5918)
1 parent 7949c45 commit 4f2785b

13 files changed

+357
-28
lines changed

src/Npgsql.GeoJSON/NpgsqlGeoJSONExtensions.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Npgsql;
1010
/// </summary>
1111
public static class NpgsqlGeoJSONExtensions
1212
{
13+
// Note: defined for binary compatibility and NpgsqlConnection.GlobalTypeMapper.
1314
/// <summary>
1415
/// Sets up GeoJSON mappings for the PostGIS types.
1516
/// </summary>
@@ -22,6 +23,7 @@ public static INpgsqlTypeMapper UseGeoJson(this INpgsqlTypeMapper mapper, GeoJSO
2223
return mapper;
2324
}
2425

26+
// Note: defined for binary compatibility and NpgsqlConnection.GlobalTypeMapper.
2527
/// <summary>
2628
/// Sets up GeoJSON mappings for the PostGIS types.
2729
/// </summary>
@@ -34,4 +36,31 @@ public static INpgsqlTypeMapper UseGeoJson(this INpgsqlTypeMapper mapper, CrsMap
3436
mapper.AddTypeInfoResolverFactory(new GeoJSONTypeInfoResolverFactory(options, geographyAsDefault, crsMap));
3537
return mapper;
3638
}
39+
40+
/// <summary>
41+
/// Sets up GeoJSON mappings for the PostGIS types.
42+
/// </summary>
43+
/// <param name="mapper">The type mapper to set up (global or connection-specific)</param>
44+
/// <param name="options">Options to use when constructing objects.</param>
45+
/// <param name="geographyAsDefault">Specifies that the geography type is used for mapping by default.</param>
46+
public static TMapper UseGeoJson<TMapper>(this TMapper mapper, GeoJSONOptions options = GeoJSONOptions.None, bool geographyAsDefault = false)
47+
where TMapper : INpgsqlTypeMapper
48+
{
49+
mapper.AddTypeInfoResolverFactory(new GeoJSONTypeInfoResolverFactory(options, geographyAsDefault, crsMap: null));
50+
return mapper;
51+
}
52+
53+
/// <summary>
54+
/// Sets up GeoJSON mappings for the PostGIS types.
55+
/// </summary>
56+
/// <param name="mapper">The type mapper to set up (global or connection-specific)</param>
57+
/// <param name="crsMap">A custom crs map that might contain more or less entries than the default well-known crs map.</param>
58+
/// <param name="options">Options to use when constructing objects.</param>
59+
/// <param name="geographyAsDefault">Specifies that the geography type is used for mapping by default.</param>
60+
public static TMapper UseGeoJson<TMapper>(this TMapper mapper, CrsMap crsMap, GeoJSONOptions options = GeoJSONOptions.None, bool geographyAsDefault = false)
61+
where TMapper : INpgsqlTypeMapper
62+
{
63+
mapper.AddTypeInfoResolverFactory(new GeoJSONTypeInfoResolverFactory(options, geographyAsDefault, crsMap));
64+
return mapper;
65+
}
3766
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
static Npgsql.NpgsqlGeoJSONExtensions.UseGeoJson<TMapper>(this TMapper mapper, Npgsql.GeoJSON.CrsMap! crsMap, Npgsql.GeoJSONOptions options = Npgsql.GeoJSONOptions.None, bool geographyAsDefault = false) -> TMapper
3+
static Npgsql.NpgsqlGeoJSONExtensions.UseGeoJson<TMapper>(this TMapper mapper, Npgsql.GeoJSONOptions options = Npgsql.GeoJSONOptions.None, bool geographyAsDefault = false) -> TMapper

src/Npgsql.Json.NET/NpgsqlJsonNetExtensions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace Npgsql;
1313
/// </summary>
1414
public static class NpgsqlJsonNetExtensions
1515
{
16+
// Note: defined for binary compatibility and NpgsqlConnection.GlobalTypeMapper.
1617
/// <summary>
1718
/// Sets up JSON.NET mappings for the PostgreSQL json and jsonb types.
1819
/// </summary>
@@ -37,4 +38,30 @@ public static INpgsqlTypeMapper UseJsonNet(
3738
mapper.AddTypeInfoResolverFactory(new JsonNetTypeInfoResolverFactory(settings));
3839
return mapper;
3940
}
41+
42+
/// <summary>
43+
/// Sets up JSON.NET mappings for the PostgreSQL json and jsonb types.
44+
/// </summary>
45+
/// <param name="mapper">The type mapper to set up.</param>
46+
/// <param name="settings">Optional settings to customize JSON serialization.</param>
47+
/// <param name="jsonbClrTypes">
48+
/// A list of CLR types to map to PostgreSQL <c>jsonb</c> (no need to specify <see cref="NpgsqlDbType.Jsonb" />).
49+
/// </param>
50+
/// <param name="jsonClrTypes">
51+
/// A list of CLR types to map to PostgreSQL <c>json</c> (no need to specify <see cref="NpgsqlDbType.Json" />).
52+
/// </param>
53+
[RequiresUnreferencedCode("Json serializer may perform reflection on trimmed types.")]
54+
[RequiresDynamicCode("Serializing arbitrary types to json can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
55+
public static TMapper UseJsonNet<TMapper>(
56+
this TMapper mapper,
57+
JsonSerializerSettings? settings = null,
58+
Type[]? jsonbClrTypes = null,
59+
Type[]? jsonClrTypes = null)
60+
where TMapper : INpgsqlTypeMapper
61+
{
62+
// Reverse order
63+
mapper.AddTypeInfoResolverFactory(new JsonNetPocoTypeInfoResolverFactory(jsonbClrTypes, jsonClrTypes, settings));
64+
mapper.AddTypeInfoResolverFactory(new JsonNetTypeInfoResolverFactory(settings));
65+
return mapper;
66+
}
4067
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
#nullable enable
2+
static Npgsql.NpgsqlJsonNetExtensions.UseJsonNet<TMapper>(this TMapper mapper, Newtonsoft.Json.JsonSerializerSettings? settings = null, System.Type![]? jsonbClrTypes = null, System.Type![]? jsonClrTypes = null) -> TMapper

src/Npgsql.NetTopologySuite/NpgsqlNetTopologySuiteExtensions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Npgsql;
1010
/// </summary>
1111
public static class NpgsqlNetTopologySuiteExtensions
1212
{
13+
// Note: defined for binary compatibility and NpgsqlConnection.GlobalTypeMapper.
1314
/// <summary>
1415
/// Sets up NetTopologySuite mappings for the PostGIS types.
1516
/// </summary>
@@ -30,4 +31,26 @@ public static INpgsqlTypeMapper UseNetTopologySuite(
3031
mapper.AddTypeInfoResolverFactory(new NetTopologySuiteTypeInfoResolverFactory(coordinateSequenceFactory, precisionModel, handleOrdinates, geographyAsDefault));
3132
return mapper;
3233
}
34+
35+
/// <summary>
36+
/// Sets up NetTopologySuite mappings for the PostGIS types.
37+
/// </summary>
38+
/// <param name="mapper">The type mapper to set up (global or connection-specific).</param>
39+
/// <param name="coordinateSequenceFactory">The factory which knows how to build a particular implementation of ICoordinateSequence from an array of Coordinates.</param>
40+
/// <param name="precisionModel">Specifies the grid of allowable points.</param>
41+
/// <param name="handleOrdinates">Specifies the ordinates which will be handled. Not specified ordinates will be ignored.
42+
/// If <see cref="F:GeoAPI.Geometries.Ordiantes.None" /> is specified, an actual value will be taken from
43+
/// the <see cref="P:GeoAPI.Geometries.ICoordinateSequenceFactory.Ordinates"/> property of <paramref name="coordinateSequenceFactory"/>.</param>
44+
/// <param name="geographyAsDefault">Specifies that the geography type is used for mapping by default.</param>
45+
public static TMapper UseNetTopologySuite<TMapper>(
46+
this TMapper mapper,
47+
CoordinateSequenceFactory? coordinateSequenceFactory = null,
48+
PrecisionModel? precisionModel = null,
49+
Ordinates handleOrdinates = Ordinates.None,
50+
bool geographyAsDefault = false)
51+
where TMapper : INpgsqlTypeMapper
52+
{
53+
mapper.AddTypeInfoResolverFactory(new NetTopologySuiteTypeInfoResolverFactory(coordinateSequenceFactory, precisionModel, handleOrdinates, geographyAsDefault));
54+
return mapper;
55+
}
3356
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
#nullable enable
2+
static Npgsql.NpgsqlNetTopologySuiteExtensions.UseNetTopologySuite<TMapper>(this TMapper mapper, NetTopologySuite.Geometries.CoordinateSequenceFactory? coordinateSequenceFactory = null, NetTopologySuite.Geometries.PrecisionModel? precisionModel = null, NetTopologySuite.Geometries.Ordinates handleOrdinates = NetTopologySuite.Geometries.Ordinates.None, bool geographyAsDefault = false) -> TMapper

src/Npgsql.NodaTime/NpgsqlNodaTimeExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Npgsql;
99
/// </summary>
1010
public static class NpgsqlNodaTimeExtensions
1111
{
12+
// Note: defined for binary compatibility and NpgsqlConnection.GlobalTypeMapper.
1213
/// <summary>
1314
/// Sets up NodaTime mappings for the PostgreSQL date/time types.
1415
/// </summary>
@@ -18,4 +19,14 @@ public static INpgsqlTypeMapper UseNodaTime(this INpgsqlTypeMapper mapper)
1819
mapper.AddTypeInfoResolverFactory(new NodaTimeTypeInfoResolverFactory());
1920
return mapper;
2021
}
22+
23+
/// <summary>
24+
/// Sets up NodaTime mappings for the PostgreSQL date/time types.
25+
/// </summary>
26+
/// <param name="mapper">The type mapper to set up (global or connection-specific)</param>
27+
public static TMapper UseNodaTime<TMapper>(this TMapper mapper) where TMapper : INpgsqlTypeMapper
28+
{
29+
mapper.AddTypeInfoResolverFactory(new NodaTimeTypeInfoResolverFactory());
30+
return mapper;
31+
}
2132
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
#nullable enable
2+
static Npgsql.NpgsqlNodaTimeExtensions.UseNodaTime<TMapper>(this TMapper mapper) -> TMapper

0 commit comments

Comments
 (0)