Skip to content

Commit 9f9072a

Browse files
committed
improved readme
1 parent e1ad7c7 commit 9f9072a

File tree

4 files changed

+75
-18
lines changed

4 files changed

+75
-18
lines changed

README.md

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,75 @@
1-
# ValueOf.Extensions
1+
ValueOf.Extensions
2+
==================
23

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.
55

6-
1. JSON serialization
7-
2. Dapper/EF.Core
8-
3. Swagger/OpenAPI
6+
Below is a list of features supported in each package:
97

10-
## Feature 1: JSON serialization adapter for System.Text.Json & Newtonsoft.Json
8+
# Package: ValueOf.Extensions
119

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`
1311

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+
};
1817
```
1918

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
2173

2274
see [Document a System.Text.Json TypeConverter to JsonConverter Adapter · Issue #1761 · dotnet/runtime]( https://github.com/dotnet/runtime/issues/1761 )
2375

ValueOf.Extensions.Examples/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
opts.IgnoreObsoleteActions();
3232
opts.IgnoreObsoleteProperties();
3333
opts.UseAllOfForInheritance();
34-
opts.MapValueOfTypesInAssemblies(typeof(EmailAddress).Assembly);
34+
opts.MapValueOfTypesInAssemblies(null, typeof(EmailAddress).Assembly);
3535
});
3636

3737
builder.Services.AddDbContext<DemoDbContext>(opts => { opts.UseSqlite("Data Source=demo.db"); });
@@ -51,4 +51,6 @@
5151

5252
app.Run();
5353

54-
public partial class Program { }
54+
public partial class Program
55+
{
56+
}

ValueOf.Extensions.SwashbuckleSwagger/SwaggerValueOfExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,16 @@ public static void MapValueOfTypes(this SwaggerGenOptions opts, IEnumerable<Type
7575
}
7676
}
7777

78-
public static void MapValueOfTypesInAssemblies(this SwaggerGenOptions opts, params Assembly[] assemblies)
78+
public static void MapValueOfTypesInAssemblies(this SwaggerGenOptions opts,
79+
SwaggerTypeMap? typeMapOverride = null,
80+
params Assembly[] assemblies
81+
)
7982
{
8083
if (opts == null) throw new ArgumentNullException(nameof(opts));
8184
var valueOfTypes = from a in assemblies
8285
from t in a.DefinedTypes.Select(t => t.AsType())
8386
select t;
84-
MapValueOfTypes(opts, valueOfTypes);
87+
MapValueOfTypes(opts, valueOfTypes, typeMapOverride);
8588
}
8689
}
8790
}

ValueOf.Extensions/BuiltinTypeParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ValueOf.Extensions
44
{
5-
public static class BuiltinTypeParser
5+
internal static class BuiltinTypeParser
66
{
77
private static class Cache<T>
88
{

0 commit comments

Comments
 (0)