Browse Source

Improved converters

pull/438/head
Ionite 10 months ago
parent
commit
6828245e94
No known key found for this signature in database
  1. 97
      StabilityMatrix.Core/Converters/Json/ParsableStringValueJsonConverter.cs
  2. 45
      StabilityMatrix.Core/Converters/Json/StringJsonConverter.cs

97
StabilityMatrix.Core/Converters/Json/ParsableStringValueJsonConverter.cs

@ -0,0 +1,97 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using StabilityMatrix.Core.Models;
namespace StabilityMatrix.Core.Converters.Json;
public class ParsableStringValueJsonConverter<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T
> : JsonConverter<T>
where T : StringValue, IParsable<T>
{
/// <inheritdoc />
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
{
throw new JsonException();
}
var value = reader.GetString();
if (value is null)
{
return default;
}
// Use TryParse result if available
if (T.TryParse(value, CultureInfo.InvariantCulture, out var result))
{
return result;
}
// Otherwise use Activator
return (T?)Activator.CreateInstance(typeToConvert, value);
}
/// <inheritdoc />
public override T ReadAsPropertyName(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
if (reader.TokenType != JsonTokenType.String)
{
throw new JsonException();
}
var value = reader.GetString();
if (value is null)
{
throw new JsonException("Property name cannot be null");
}
// Use TryParse result if available
if (T.TryParse(value, CultureInfo.InvariantCulture, out var result))
{
return result;
}
// Otherwise use Activator
return (T?)Activator.CreateInstance(typeToConvert, value)
?? throw new JsonException("Property name cannot be null");
}
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
if (value is IFormattable formattable)
{
writer.WriteStringValue(formattable.ToString(null, CultureInfo.InvariantCulture));
}
else
{
writer.WriteStringValue(value.ToString());
}
}
/// <inheritdoc />
public override void WriteAsPropertyName(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
if (value is null)
{
throw new JsonException("Property name cannot be null");
}
if (value is IFormattable formattable)
{
writer.WriteStringValue(formattable.ToString(null, CultureInfo.InvariantCulture));
}
else
{
writer.WriteStringValue(value.ToString());
}
}
}

45
StabilityMatrix.Core/Converters/Json/StringJsonConverter.cs

@ -12,8 +12,9 @@ namespace StabilityMatrix.Core.Converters.Json;
/// Types implementing <see cref="IFormattable"/> will be formatted with <see cref="CultureInfo.InvariantCulture"/> /// Types implementing <see cref="IFormattable"/> will be formatted with <see cref="CultureInfo.InvariantCulture"/>
/// </summary> /// </summary>
[PublicAPI] [PublicAPI]
public class StringJsonConverter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T> public class StringJsonConverter<
: JsonConverter<T> [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T
> : JsonConverter<T>
{ {
/// <inheritdoc /> /// <inheritdoc />
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
@ -32,6 +33,28 @@ public class StringJsonConverter<[DynamicallyAccessedMembers(DynamicallyAccessed
return (T?)Activator.CreateInstance(typeToConvert, value); return (T?)Activator.CreateInstance(typeToConvert, value);
} }
/// <inheritdoc />
public override T ReadAsPropertyName(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
if (reader.TokenType != JsonTokenType.String)
{
throw new JsonException();
}
var value = reader.GetString();
if (value is null)
{
throw new JsonException("Property name cannot be null");
}
return (T?)Activator.CreateInstance(typeToConvert, value)
?? throw new JsonException("Property name cannot be null");
}
/// <inheritdoc /> /// <inheritdoc />
public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
{ {
@ -50,4 +73,22 @@ public class StringJsonConverter<[DynamicallyAccessedMembers(DynamicallyAccessed
writer.WriteStringValue(value.ToString()); writer.WriteStringValue(value.ToString());
} }
} }
/// <inheritdoc />
public override void WriteAsPropertyName(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
if (value is null)
{
throw new JsonException("Property name cannot be null");
}
if (value is IFormattable formattable)
{
writer.WriteStringValue(formattable.ToString(null, CultureInfo.InvariantCulture));
}
else
{
writer.WriteStringValue(value.ToString());
}
}
} }

Loading…
Cancel
Save