forked from nventive/UnoApplicationTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerializationConfiguration.cs
76 lines (66 loc) · 2.48 KB
/
SerializationConfiguration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
using ApplicationTemplate.DataAccess;
using Microsoft.Extensions.DependencyInjection;
using Nventive.Persistence;
namespace ApplicationTemplate;
// TODO: Add the types you want to use in JSON source-generator-based deserialization here.
[JsonSerializable(typeof(AuthenticationToken))]
[JsonSerializable(typeof(AuthenticationData))]
[JsonSerializable(typeof(ApplicationSettings))]
[JsonSerializable(typeof(Refit.ProblemDetails))]
[JsonSerializable(typeof(PostData))]
[JsonSerializable(typeof(PostData[]))]
[JsonSerializable(typeof(DadJokesResponse))]
[JsonSerializable(typeof(DadJokesErrorResponse))]
[JsonSerializable(typeof(UserProfileData))]
[JsonSerializable(typeof(DadJokesData))]
[JsonSerializable(typeof(DadJokeChildData))]
[JsonSerializable(typeof(DadJokeContentData))]
[JsonSerializable(typeof(Dictionary<string, string>))]
[JsonSerializable(typeof(IDictionary<string, string>))]
[ExcludeFromCodeCoverage]
public partial class JsonContext : JsonSerializerContext
{
}
/// <summary>
/// This class is used for serialization configuration.
/// - Configures the serializers.
/// </summary>
public static class SerializationConfiguration
{
public static JsonSerializerOptions DefaultJsonSerializerOptions { get; } = GetOptionsWithSourceGeneration();
public static JsonSerializerOptions NoSourceGenerationJsonSerializerOptions { get; } = GetBaseOptions();
private static JsonSerializerOptions GetBaseOptions()
{
// These options allow some more cases than just the default.
var options = new JsonSerializerOptions
{
AllowTrailingCommas = true,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
PropertyNameCaseInsensitive = true,
};
return options;
}
private static JsonSerializerOptions GetOptionsWithSourceGeneration()
{
var options = GetBaseOptions();
options.TypeInfoResolver = JsonContext.Default;
return options;
}
/// <summary>
/// Adds the serialization services to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">Service collection.</param>
/// <returns><see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddSerialization(this IServiceCollection services)
{
services
.AddSingleton(DefaultJsonSerializerOptions)
.AddSingleton<ISettingsSerializer>(c => new JsonSerializerToSettingsSerializerAdapter(DefaultJsonSerializerOptions));
return services;
}
}