Skip to content

Commit dacb2dc

Browse files
edit: Stores private serialization options as non-local variables instead of initializing it every time the serialize and deserialize function has been called in JsonConfigurationSerializer
1 parent f509b21 commit dacb2dc

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

src/AridityTeam.Ascorbic/AridityTeam.Ascorbic.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- Addition: HttpClient.GetStringAsync(string?, CancellationToken) extension (in AridityTeam.Net)
4242
- Addition: HttpClient.GetStringAsync(Uri?, CancellationToken) extension (in AridityTeam.Net)
4343
- Modified: LoggerSettings is made to be serializable now
44+
- Modified: Stores private serialization options as non-local variables instead of initializing it every time the serialize and deserialize function has been called in JsonConfigurationSerializer
4445
- Modified: StreamEx.CopyToAsync now throws an TaskCanceledException instead of a OperationCanceledException
4546
- Modified: The internal HttpClient variable inside of StreamEx will now automatically dispose itself
4647
- Modified: Fix the StreamReaderEx class not being public

src/AridityTeam.Ascorbic/Configuration/JsonConfigurationSerializer.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ namespace AridityTeam.Configuration
3636
public class JsonConfigurationSerializer<T> : ConfigurationSerializerBase<T>
3737
where T : class, new()
3838
{
39+
private readonly IJsonTypeInfoResolver _typeInfoResolver;
40+
private readonly JsonSerializerOptions _serializerOptions;
41+
private readonly JsonTypeInfo _typeInfo;
42+
43+
/// <summary>
44+
/// Initializes a new instance of the <seealso cref="JsonConfigurationSerializer{T}"/> class.
45+
/// </summary>
46+
public JsonConfigurationSerializer()
47+
{
48+
_typeInfoResolver = new DefaultJsonTypeInfoResolver();
49+
_serializerOptions = new JsonSerializerOptions(JsonSerializerOptions.Default);
50+
_serializerOptions.TypeInfoResolver = _typeInfoResolver;
51+
52+
var typeInfo = _typeInfoResolver.GetTypeInfo(typeof(T), _serializerOptions);
53+
Assumes.NotNull(typeInfo);
54+
_typeInfo = typeInfo;
55+
}
56+
3957
/// <summary>
4058
/// Deserializes the specified configuration instance.
4159
/// </summary>
@@ -53,12 +71,7 @@ public override async Task<T> DeserializeConfigAsync(Stream stream, Cancellation
5371
if (stream.Length == 0)
5472
return new T();
5573

56-
var typeInfoResolver = new DefaultJsonTypeInfoResolver();
57-
var serializationOptions = new JsonSerializerOptions();
58-
serializationOptions.TypeInfoResolver = typeInfoResolver;
59-
60-
var typeInfo = typeInfoResolver.GetTypeInfo(typeof(T), serializationOptions);
61-
var result = await JsonSerializer.DeserializeAsync(stream, typeInfo, token).AsTask();
74+
var result = await JsonSerializer.DeserializeAsync(stream, _typeInfo, token).AsTask();
6275
Assumes.NotNull(result);
6376
return (T)result;
6477
}
@@ -77,12 +90,7 @@ public override Task SerializeConfigAsync(T newConfig, Stream stream, Cancellati
7790

7891
Assumes.True(stream.CanWrite, SR.CannotWriteToStream);
7992

80-
var typeInfoResolver = new DefaultJsonTypeInfoResolver();
81-
var serializationOptions = new JsonSerializerOptions();
82-
serializationOptions.TypeInfoResolver = typeInfoResolver;
83-
84-
var typeInfo = typeInfoResolver.GetTypeInfo(typeof(T), serializationOptions);
85-
return JsonSerializer.SerializeAsync(stream, newConfig, typeInfo, token);
93+
return JsonSerializer.SerializeAsync(stream, newConfig, _typeInfo, token);
8694
}
8795
}
8896
}

0 commit comments

Comments
 (0)