Skip to content

Commit ef12ae6

Browse files
committed
Configure options / Documentation.
1 parent dfa7922 commit ef12ae6

File tree

3 files changed

+21
-66
lines changed

3 files changed

+21
-66
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,40 @@ The `Shuttle.Core.Serialization` package provides a consistent interface for ser
88
dotnet add package Shuttle.Core.Serialization
99
```
1010

11-
## Interface
11+
## `ISerializer` interface
1212

1313
The core of the library is the `ISerializer` interface:
1414

1515
```csharp
1616
public interface ISerializer
1717
{
1818
string Name { get; }
19-
Task<Stream> SerializeAsync(object instance, CancellationToken cancellationToken = default);
2019
Task<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default);
20+
Task<Stream> SerializeAsync(object instance, CancellationToken cancellationToken = default);
2121
}
2222
```
2323

2424
The following implementation is provided:
2525

2626
- `JsonSerializer`: uses `System.Text.Json` for serialization.
27+
- `SerializerService`: used to manage multiple `ISerializer` instances.
2728

2829
## Usage
2930

30-
### AddJsonSerializer
31+
### `AddJsonSerializer`
3132

3233
To register the `JsonSerializer`, use the `AddJsonSerializer` extension method on `IServiceCollection`:
3334

3435
```csharp
35-
services.AddJsonSerializer(builder =>
36+
services.AddJsonSerializer(options =>
3637
{
37-
builder.Options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
38+
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
3839
});
3940
```
4041

41-
The `builder.Options` is of type [JsonSerializerOptions](https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions?view=net-6.0).
42+
The `options` is of type [JsonSerializerOptions](https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions?view=net-6.0).
4243

43-
### ISerializerService
44+
### `ISerializerService`
4445

4546
The `ISerializerService` can be used to manage multiple serializers:
4647

@@ -69,23 +70,23 @@ if (serializerService.Contains("Json"))
6970

7071
## Methods
7172

72-
### SerializeAsync
73+
### `SerializeAsync`
7374

7475
```csharp
7576
Task<Stream> SerializeAsync(object instance, CancellationToken cancellationToken = default);
7677
```
7778

7879
Returns the `object` as a `Stream`.
7980

80-
### DeserializeAsync
81+
### `DeserializeAsync`
8182

8283
```csharp
8384
Task<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default);
8485
```
8586

8687
Deserializes the `Stream` into an `object` of the given `Type`.
8788

88-
### DeserializeAsync&lt;T&gt; (Extension method)
89+
### `DeserializeAsync<T>` (Extension method)
8990

9091
```csharp
9192
Task<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default);

Shuttle.Core.Serialization/Json/JsonSerializerBuilder.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

Shuttle.Core.Serialization/ServiceCollectionExtensions.cs

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,28 @@
11
using System.Text.Json;
22
using Microsoft.Extensions.DependencyInjection;
3-
using Shuttle.Core.Contract;
43

54
namespace Shuttle.Core.Serialization;
65

76
public static class ServiceCollectionExtensions
87
{
98
extension(IServiceCollection services)
109
{
11-
public IServiceCollection AddJsonSerializer(Action<JsonSerializerBuilder>? builder = null)
10+
public IServiceCollection AddJsonSerializer()
1211
{
13-
Guard.AgainstNull(services);
14-
15-
var jsonSerializerBuilder = new JsonSerializerBuilder(services);
16-
17-
builder?.Invoke(jsonSerializerBuilder);
12+
ArgumentNullException.ThrowIfNull(services);
1813

1914
services.AddSingleton<ISerializer, JsonSerializer>();
2015

21-
services.Configure<JsonSerializerOptions>(options =>
22-
{
23-
options.AllowDuplicateProperties = jsonSerializerBuilder.Options.AllowDuplicateProperties;
24-
options.AllowOutOfOrderMetadataProperties = jsonSerializerBuilder.Options.AllowOutOfOrderMetadataProperties;
25-
options.AllowTrailingCommas = jsonSerializerBuilder.Options.AllowTrailingCommas;
26-
options.PropertyNamingPolicy = jsonSerializerBuilder.Options.PropertyNamingPolicy;
27-
options.PropertyNameCaseInsensitive = jsonSerializerBuilder.Options.PropertyNameCaseInsensitive;
28-
options.DictionaryKeyPolicy = jsonSerializerBuilder.Options.DictionaryKeyPolicy;
29-
options.DefaultIgnoreCondition = jsonSerializerBuilder.Options.DefaultIgnoreCondition;
30-
options.IgnoreReadOnlyProperties = jsonSerializerBuilder.Options.IgnoreReadOnlyProperties;
31-
options.IgnoreReadOnlyFields = jsonSerializerBuilder.Options.IgnoreReadOnlyFields;
32-
options.IncludeFields = jsonSerializerBuilder.Options.IncludeFields;
33-
options.NumberHandling = jsonSerializerBuilder.Options.NumberHandling;
34-
options.UnknownTypeHandling = jsonSerializerBuilder.Options.UnknownTypeHandling;
35-
options.UnmappedMemberHandling = jsonSerializerBuilder.Options.UnmappedMemberHandling;
36-
options.PreferredObjectCreationHandling = jsonSerializerBuilder.Options.PreferredObjectCreationHandling;
37-
options.RespectNullableAnnotations = jsonSerializerBuilder.Options.RespectNullableAnnotations;
38-
options.RespectRequiredConstructorParameters = jsonSerializerBuilder.Options.RespectRequiredConstructorParameters;
39-
options.ReadCommentHandling = jsonSerializerBuilder.Options.ReadCommentHandling;
40-
options.MaxDepth = jsonSerializerBuilder.Options.MaxDepth;
41-
options.WriteIndented = jsonSerializerBuilder.Options.WriteIndented;
42-
options.IndentCharacter = jsonSerializerBuilder.Options.IndentCharacter;
43-
options.IndentSize = jsonSerializerBuilder.Options.IndentSize;
44-
options.NewLine = jsonSerializerBuilder.Options.NewLine;
45-
options.Encoder = jsonSerializerBuilder.Options.Encoder;
46-
options.DefaultBufferSize = jsonSerializerBuilder.Options.DefaultBufferSize;
47-
options.ReferenceHandler = jsonSerializerBuilder.Options.ReferenceHandler;
16+
return services;
17+
}
4818

49-
foreach (var converter in jsonSerializerBuilder.Options.Converters)
50-
{
51-
options.Converters.Add(converter);
52-
}
19+
public IServiceCollection AddJsonSerializer(Action<JsonSerializerOptions> configureOptions)
20+
{
21+
ArgumentNullException.ThrowIfNull(services);
22+
ArgumentNullException.ThrowIfNull(configureOptions);
5323

54-
options.TypeInfoResolver = jsonSerializerBuilder.Options.TypeInfoResolver;
55-
});
24+
services.AddJsonSerializer();
25+
services.Configure(configureOptions);
5626

5727
return services;
5828
}

0 commit comments

Comments
 (0)