|
1 | 1 | using System.Text.Json; |
2 | 2 | using Microsoft.Extensions.DependencyInjection; |
3 | | -using Shuttle.Core.Contract; |
4 | 3 |
|
5 | 4 | namespace Shuttle.Core.Serialization; |
6 | 5 |
|
7 | 6 | public static class ServiceCollectionExtensions |
8 | 7 | { |
9 | 8 | extension(IServiceCollection services) |
10 | 9 | { |
11 | | - public IServiceCollection AddJsonSerializer(Action<JsonSerializerBuilder>? builder = null) |
| 10 | + public IServiceCollection AddJsonSerializer() |
12 | 11 | { |
13 | | - Guard.AgainstNull(services); |
14 | | - |
15 | | - var jsonSerializerBuilder = new JsonSerializerBuilder(services); |
16 | | - |
17 | | - builder?.Invoke(jsonSerializerBuilder); |
| 12 | + ArgumentNullException.ThrowIfNull(services); |
18 | 13 |
|
19 | 14 | services.AddSingleton<ISerializer, JsonSerializer>(); |
20 | 15 |
|
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 | + } |
48 | 18 |
|
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); |
53 | 23 |
|
54 | | - options.TypeInfoResolver = jsonSerializerBuilder.Options.TypeInfoResolver; |
55 | | - }); |
| 24 | + services.AddJsonSerializer(); |
| 25 | + services.Configure(configureOptions); |
56 | 26 |
|
57 | 27 | return services; |
58 | 28 | } |
|
0 commit comments