Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure all JSON converters are public to allow referencing by JSON source generators #253

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Tingle.AspNetCore.Tokens;
/// <item><see cref="TimedContinuationToken{T}"/></item>
/// </list>
/// </summary>
internal class ContinuationTokenJsonConverter : JsonConverter<IToken>
public class ContinuationTokenJsonConverter : JsonConverter<IToken>
{
/// <inheritdoc/>
public override bool CanConvert(Type typeToConvert) => typeof(IToken).IsAssignableFrom(typeToConvert);
Expand Down
26 changes: 1 addition & 25 deletions src/Tingle.Extensions.Primitives/ConnectionStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using Tingle.Extensions.Primitives.Converters;
using Tingle.Extensions.Primitives.Properties;

namespace Tingle.Extensions.Primitives;
Expand Down Expand Up @@ -417,30 +417,6 @@ readonly object IConvertible.ToType(Type conversionType, IFormatProvider? provid

#endregion

internal class ConnectionStringBuilderJsonConverter : JsonConverter<ConnectionStringBuilder>
{
public ConnectionStringBuilderJsonConverter() { }

/// <inheritdoc/>
public override ConnectionStringBuilder Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null) return default;
if (reader.TokenType != JsonTokenType.String)
{
throw new InvalidOperationException("Only strings are supported");
}

var str = reader.GetString();
return new ConnectionStringBuilder(str!);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, ConnectionStringBuilder value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}

internal class ConnectionStringBuilderTypeConverter : TypeConverter
{
/// <inheritdoc/>
Expand Down
18 changes: 1 addition & 17 deletions src/Tingle.Extensions.Primitives/Continent.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.ComponentModel;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using Tingle.Extensions.Primitives.Converters;

namespace Tingle.Extensions.Primitives;

Expand Down Expand Up @@ -147,22 +147,6 @@ object IConvertible.ToType(Type conversionType, IFormatProvider? provider)

#endregion

internal class ContinentJsonConverter : JsonConverter<Continent>
{
/// <inheritdoc/>
public override Continent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var s = reader.GetString();
return string.IsNullOrWhiteSpace(s) ? null : new Continent(s);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, Continent value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.Name);
}
}

internal class ContinentTypeConverter : TypeConverter
{
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Tingle.Extensions.Primitives.Converters;

/// <summary>
/// A <see cref="JsonConverter{T}"/> for <see cref="ConnectionStringBuilder"/>
/// </summary>
public class ConnectionStringBuilderJsonConverter : JsonConverter<ConnectionStringBuilder>
{
/// <inheritdoc/>
public override ConnectionStringBuilder Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null) return default;
if (reader.TokenType != JsonTokenType.String)
{
throw new InvalidOperationException("Only strings are supported");
}

var str = reader.GetString();
return new ConnectionStringBuilder(str!);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, ConnectionStringBuilder value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Tingle.Extensions.Primitives.Converters;

/// <summary>
/// A <see cref="JsonConverter{T}"/> for <see cref="Continent"/>
/// </summary>
public class ContinentJsonConverter : JsonConverter<Continent>
{
/// <inheritdoc/>
public override Continent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var s = reader.GetString();
return string.IsNullOrWhiteSpace(s) ? null : new Continent(s);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, Continent value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.Name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Tingle.Extensions.Primitives.Converters;

/// <summary>
/// A <see cref="JsonConverter{T}"/> for <see cref="Country"/>
/// </summary>
public class CountryJsonConverter : JsonConverter<Country>
{
/// <inheritdoc/>
public override Country? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var s = reader.GetString();
return string.IsNullOrWhiteSpace(s) ? null : Country.FromCode(s);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, Country value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ThreeLetterCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Tingle.Extensions.Primitives.Converters;

/// <summary>
/// A <see cref="JsonConverter{T}"/> for <see cref="Currency"/>
/// </summary>
public class CurrencyJsonConverter : JsonConverter<Currency>
{
/// <inheritdoc/>
public override Currency? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var s = reader.GetString();
return string.IsNullOrWhiteSpace(s) ? null : Currency.FromCode(s);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, Currency value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.Code);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Tingle.Extensions.Primitives.Converters;

/// <summary>
/// A <see cref="JsonConverter{T}"/> for <see cref="Duration"/>
/// </summary>
public class DurationJsonConverter : JsonConverter<Duration>
{
/// <inheritdoc/>
public override Duration Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null) return default;
if (reader.TokenType != JsonTokenType.String)
{
throw new InvalidOperationException("Only strings are supported");
}

var str = reader.GetString();
return Duration.Parse(str!);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, Duration value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Tingle.Extensions.Primitives;

/// <summary>
/// A <see cref="JsonConverter{T}"/> for <see cref="Language"/>
/// </summary>
public class LanguageJsonConverter : JsonConverter<Language>
{
/// <inheritdoc/>
public override Language? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var s = reader.GetString();
return string.IsNullOrWhiteSpace(s) ? null : Language.FromCode(s);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, Language value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ThreeLetterCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Tingle.Extensions.Primitives.Converters;

/// <summary>
/// A <see cref="JsonConverter{T}"/> for <see cref="SequenceNumber"/>
/// </summary>
public class SequenceNumberJsonConverter : JsonConverter<SequenceNumber>
{
/// <inheritdoc/>
public override SequenceNumber Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.Number)
{
throw new InvalidOperationException("Only numbers are supported");
}

var value = reader.GetInt64();
return new SequenceNumber(value);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, SequenceNumber value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Tingle.Extensions.Primitives.Converters;

/// <summary>
/// A <see cref="JsonConverter{T}"/> for <see cref="SwiftCode"/>
/// </summary>
public class SwiftCodeJsonConverter : JsonConverter<SwiftCode>
{
/// <inheritdoc/>
public override SwiftCode? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var s = reader.GetString();
return string.IsNullOrWhiteSpace(s) ? null : SwiftCode.Parse(s);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, SwiftCode value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
18 changes: 1 addition & 17 deletions src/Tingle.Extensions.Primitives/Country.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using Tingle.Extensions.Primitives.Converters;

namespace Tingle.Extensions.Primitives;

Expand Down Expand Up @@ -161,22 +161,6 @@ object IConvertible.ToType(Type conversionType, IFormatProvider? provider)

#endregion

internal class CountryJsonConverter : JsonConverter<Country>
{
/// <inheritdoc/>
public override Country? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var s = reader.GetString();
return string.IsNullOrWhiteSpace(s) ? null : FromCode(s);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, Country value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ThreeLetterCode);
}
}

internal class CountryTypeConverter : TypeConverter
{
/// <inheritdoc/>
Expand Down
18 changes: 1 addition & 17 deletions src/Tingle.Extensions.Primitives/Currency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using Tingle.Extensions.Primitives.Converters;

namespace Tingle.Extensions.Primitives;

Expand Down Expand Up @@ -157,22 +157,6 @@ object IConvertible.ToType(Type conversionType, IFormatProvider? provider)

#endregion

internal class CurrencyJsonConverter : JsonConverter<Currency>
{
/// <inheritdoc/>
public override Currency? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var s = reader.GetString();
return string.IsNullOrWhiteSpace(s) ? null : FromCode(s);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, Currency value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.Code);
}
}

internal class CurrencyTypeConverter : TypeConverter
{
/// <inheritdoc/>
Expand Down
26 changes: 1 addition & 25 deletions src/Tingle.Extensions.Primitives/Duration.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Tingle.Extensions.Primitives.Converters;

namespace Tingle.Extensions.Primitives;

Expand Down Expand Up @@ -445,30 +445,6 @@ object IConvertible.ToType(Type conversionType, IFormatProvider? provider)

#endregion

internal class DurationJsonConverter : JsonConverter<Duration>
{
public DurationJsonConverter() { }

/// <inheritdoc/>
public override Duration Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null) return default;
if (reader.TokenType != JsonTokenType.String)
{
throw new InvalidOperationException("Only strings are supported");
}

var str = reader.GetString();
return Parse(str!);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, Duration value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}

internal class DurationTypeConverter : TypeConverter
{
/// <inheritdoc/>
Expand Down
Loading