|
1 | | -# ValueOf.Extensions |
| 1 | +ValueOf.Extensions |
| 2 | +================== |
2 | 3 |
|
3 | | -This library contains a set of adapters/utilities you might need when using the |
4 | | -popular [ValueOf](https://github.com/mcintyre321/ValueOf) library. It helps you in these areas: |
| 4 | +This library contains a set of adapters/utilities you might need when using the popular [ValueOf](https://github.com/mcintyre321/ValueOf) library. |
5 | 5 |
|
6 | | -1. JSON serialization |
7 | | -2. Dapper/EF.Core |
8 | | -3. Swagger/OpenAPI |
| 6 | +Below is a list of features supported in each package: |
9 | 7 |
|
10 | | -## Feature 1: JSON serialization adapter for System.Text.Json & Newtonsoft.Json |
| 8 | +# Package: ValueOf.Extensions |
11 | 9 |
|
12 | | -For System.Text.JSON, you will need an adapter (see [the reason]( https://github.com/dotnet/runtime/issues/38812 )) |
| 10 | +## System.Text.Json JSON serialization/deserialization with `ValueOfJsonAdapterFactory` |
13 | 11 |
|
14 | | -```csharp |
15 | | - var options = new JsonSerializerOptions(); |
16 | | - options.Converters.Add(new TypeConverterJsonAdapterFactory()); |
17 | | - System.Text.Json.JsonSerializer.Serialize(_dut, options); |
| 12 | +```cs |
| 13 | +var options = new JsonSerializerOptions |
| 14 | +{ |
| 15 | + Converters = { new ValueOfJsonAdapterFactory() }, |
| 16 | +}; |
18 | 17 | ``` |
19 | 18 |
|
20 | | -For Newtonsoft.Json, it is automatic. |
| 19 | +or in ASP.NET Core: |
| 20 | + |
| 21 | +```cs |
| 22 | +builder.Services.AddControllers().AddJsonOptions(options => |
| 23 | +{ |
| 24 | + options.JsonSerializerOptions.Converters.Add(new ValueOfJsonAdapterFactory()); |
| 25 | +}); |
| 26 | +``` |
| 27 | + |
| 28 | +## TypeConverter with `ValueOfTypeConverter<TU, T>` |
| 29 | + |
| 30 | +This is needed if you want to be able to parse a `ValueOf` type from strings. Typically you'll need it when you want to bind API parameters e.g. `[FromRoute] UserId id` & `[FromQuery] EmailAddress email` in ASP.NET Core. |
| 31 | + |
| 32 | +You can use this provided extension method to add all `ValueOf` types collectively. |
| 33 | + |
| 34 | +```cs |
| 35 | + ValueOfTypeExtensions.ConfigureValueOfTypeConverters(typeof(UserId).Assembly); |
| 36 | +``` |
| 37 | + |
| 38 | +# Package: ValueOf.Extensions.NewtonsoftJson |
| 39 | + |
| 40 | +If you are using Newtonsoft.JSON, this package fixes serialization/deserialization with a custom JsonConverter `ValueOfNewtonsoftConverter`. |
| 41 | + |
| 42 | +```cs |
| 43 | +var settings = new JsonSerializerSettings |
| 44 | +{ |
| 45 | + Converters = [new ValueOfNewtonsoftConverter()], |
| 46 | +}; |
| 47 | +``` |
| 48 | + |
| 49 | +# Package: ValueOf.Extensions.Dapper |
| 50 | + |
| 51 | +With this package, you can have your column be of `ValueOf` types. |
| 52 | + |
| 53 | +You can use the provided extension method to register all `ValueOfDapperTypeHandler`s from assemblies with ease: |
| 54 | + |
| 55 | +```cs |
| 56 | +ValueOfDapperExtensions.ConfigureValueOfDapperTypeHandlers(typeof(UserId).Assembly); |
| 57 | +``` |
| 58 | + |
| 59 | +# Package: ValueOf.Extensions.Swagger |
| 60 | + |
| 61 | +If you `ValueOf`, swagger will still consider a `ValueOf` type a complex type instead of a simple type. This package helps you fix that by picking the most suitable `type` & `format` based on the underlying type. |
| 62 | + |
| 63 | +This extension method helps you map all your `ValueOf` types to the correct schema. |
| 64 | +```cs |
| 65 | + opts.MapValueOfTypesInAssemblies(null, typeof(EmailAddress).Assembly); |
| 66 | +``` |
| 67 | + |
| 68 | +If `type` & `format` picked for you doesn't suit your needs, you can use the `SwaggerTypeMap? typeMapOverride` parameter in `MapValueOfTypesInAssemblies` to customize. |
| 69 | + |
| 70 | +NOTE: currently I only handled `Swashbuckle.AspNetCore`. Let me know or send me PR if you want integration for other Swagger/Open API solutions. |
| 71 | + |
| 72 | +# References |
21 | 73 |
|
22 | 74 | see [Document a System.Text.Json TypeConverter to JsonConverter Adapter · Issue #1761 · dotnet/runtime]( https://github.com/dotnet/runtime/issues/1761 ) |
23 | 75 |
|
|
0 commit comments