-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2166 from erri120/fix/windowdata-serialization
Add `Optional<T>` JSON converter
- Loading branch information
Showing
2 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/NexusMods.DataModel/JsonConverters/OptionalConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System.Reflection; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using DynamicData.Kernel; | ||
using JetBrains.Annotations; | ||
|
||
namespace NexusMods.DataModel.JsonConverters; | ||
|
||
/// <summary> | ||
/// Converter factory for <see cref="Optional{T}"/> | ||
/// </summary> | ||
[UsedImplicitly] | ||
public class OptionalConverterFactory : JsonConverterFactory | ||
{ | ||
/// <inheritdoc/> | ||
public override bool CanConvert(Type typeToConvert) | ||
{ | ||
if (!typeToConvert.IsGenericType) return false; | ||
return typeToConvert.GetGenericTypeDefinition() == typeof(Optional<>); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var typeArguments = typeToConvert.GetGenericArguments(); | ||
var valueType = typeArguments[0]; | ||
|
||
var converter = (JsonConverter)Activator.CreateInstance( | ||
type: typeof(OptionalConverter<>).MakeGenericType(valueType), | ||
bindingAttr: BindingFlags.Instance | BindingFlags.Public, | ||
binder: null, | ||
args: [options], | ||
culture: null | ||
)!; | ||
|
||
return converter; | ||
} | ||
|
||
private class OptionalConverter<TValue> : JsonConverter<Optional<TValue>> | ||
where TValue : notnull | ||
{ | ||
private readonly JsonConverter<TValue> _valueConverter; | ||
|
||
[UsedImplicitly] | ||
public OptionalConverter(JsonSerializerOptions options) | ||
{ | ||
_valueConverter = (JsonConverter<TValue>)options.GetConverter(typeof(TValue)); | ||
} | ||
|
||
public override Optional<TValue> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.Null) return Optional<TValue>.None; | ||
var value = _valueConverter.Read(ref reader, typeToConvert, options); | ||
return Optional<TValue>.Create(value); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, Optional<TValue> value, JsonSerializerOptions options) | ||
{ | ||
if (!value.HasValue) | ||
{ | ||
writer.WriteNullValue(); | ||
} | ||
else | ||
{ | ||
_valueConverter.Write(writer, value.Value, options); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters