From 3d89df5af7125f3daa7debe1f8914eba7b33113f Mon Sep 17 00:00:00 2001 From: Maxwell Weru Date: Thu, 16 May 2024 06:28:32 +0300 Subject: [PATCH] Make sure all JSON converters are public to allow referencing by JSON source generators --- .../ContinuationTokenJsonConverter.cs | 2 +- .../ConnectionStringBuilder.cs | 26 +---------------- src/Tingle.Extensions.Primitives/Continent.cs | 18 +----------- .../ConnectionStringBuilderJsonConverter.cs | 29 +++++++++++++++++++ .../Converters/ContinentJsonConverter.cs | 23 +++++++++++++++ .../Converters/CountryJsonConverter.cs | 23 +++++++++++++++ .../Converters/CurrencyJsonConverter.cs | 23 +++++++++++++++ .../Converters/DurationJsonConverter.cs | 29 +++++++++++++++++++ .../Converters/LanguageJsonConverter.cs | 23 +++++++++++++++ .../Converters/SequenceNumberJsonConverter.cs | 28 ++++++++++++++++++ .../Converters/SwiftCodeJsonConverter.cs | 23 +++++++++++++++ src/Tingle.Extensions.Primitives/Country.cs | 18 +----------- src/Tingle.Extensions.Primitives/Currency.cs | 18 +----------- src/Tingle.Extensions.Primitives/Duration.cs | 26 +---------------- src/Tingle.Extensions.Primitives/Language.cs | 17 ----------- .../SequenceNumber.cs | 23 +-------------- src/Tingle.Extensions.Primitives/SwiftCode.cs | 18 +----------- .../TokenJsonConverterTests.cs | 21 +++++++++++++- .../ByteSizeTests.cs | 11 ++++++- .../ConnectionStringBuilderTests.cs | 11 ++++++- .../ContinentTests.cs | 11 ++++++- .../CountryTests.cs | 11 ++++++- .../CurrencyTests.cs | 11 ++++++- .../DurationTests.cs | 11 ++++++- .../EtagTests.cs | 11 ++++++- .../KsuidTests.cs | 11 ++++++- .../LanguageTests.cs | 11 ++++++- .../MoneyTests.cs | 11 ++++++- .../SequenceNumberTests.cs | 11 ++++++- .../SwiftCodeTests.cs | 11 ++++++- .../TestJsonSerializerContext.cs | 18 ++++++++++++ 31 files changed, 367 insertions(+), 171 deletions(-) create mode 100644 src/Tingle.Extensions.Primitives/Converters/ConnectionStringBuilderJsonConverter.cs create mode 100644 src/Tingle.Extensions.Primitives/Converters/ContinentJsonConverter.cs create mode 100644 src/Tingle.Extensions.Primitives/Converters/CountryJsonConverter.cs create mode 100644 src/Tingle.Extensions.Primitives/Converters/CurrencyJsonConverter.cs create mode 100644 src/Tingle.Extensions.Primitives/Converters/DurationJsonConverter.cs create mode 100644 src/Tingle.Extensions.Primitives/Converters/LanguageJsonConverter.cs create mode 100644 src/Tingle.Extensions.Primitives/Converters/SequenceNumberJsonConverter.cs create mode 100644 src/Tingle.Extensions.Primitives/Converters/SwiftCodeJsonConverter.cs create mode 100644 tests/Tingle.Extensions.Primitives.Tests/TestJsonSerializerContext.cs diff --git a/src/Tingle.AspNetCore.Tokens/ContinuationTokenJsonConverter.cs b/src/Tingle.AspNetCore.Tokens/ContinuationTokenJsonConverter.cs index 59e29ad0..88ae7cc2 100644 --- a/src/Tingle.AspNetCore.Tokens/ContinuationTokenJsonConverter.cs +++ b/src/Tingle.AspNetCore.Tokens/ContinuationTokenJsonConverter.cs @@ -11,7 +11,7 @@ namespace Tingle.AspNetCore.Tokens; /// /// /// -internal class ContinuationTokenJsonConverter : JsonConverter +public class ContinuationTokenJsonConverter : JsonConverter { /// public override bool CanConvert(Type typeToConvert) => typeof(IToken).IsAssignableFrom(typeToConvert); diff --git a/src/Tingle.Extensions.Primitives/ConnectionStringBuilder.cs b/src/Tingle.Extensions.Primitives/ConnectionStringBuilder.cs index ecabe931..3b7824a4 100644 --- a/src/Tingle.Extensions.Primitives/ConnectionStringBuilder.cs +++ b/src/Tingle.Extensions.Primitives/ConnectionStringBuilder.cs @@ -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; @@ -417,30 +417,6 @@ readonly object IConvertible.ToType(Type conversionType, IFormatProvider? provid #endregion - internal class ConnectionStringBuilderJsonConverter : JsonConverter - { - public ConnectionStringBuilderJsonConverter() { } - - /// - 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!); - } - - /// - public override void Write(Utf8JsonWriter writer, ConnectionStringBuilder value, JsonSerializerOptions options) - { - writer.WriteStringValue(value.ToString()); - } - } - internal class ConnectionStringBuilderTypeConverter : TypeConverter { /// diff --git a/src/Tingle.Extensions.Primitives/Continent.cs b/src/Tingle.Extensions.Primitives/Continent.cs index 47e72632..00c5a61a 100644 --- a/src/Tingle.Extensions.Primitives/Continent.cs +++ b/src/Tingle.Extensions.Primitives/Continent.cs @@ -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; @@ -147,22 +147,6 @@ object IConvertible.ToType(Type conversionType, IFormatProvider? provider) #endregion - internal class ContinentJsonConverter : JsonConverter - { - /// - public override Continent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - var s = reader.GetString(); - return string.IsNullOrWhiteSpace(s) ? null : new Continent(s); - } - - /// - public override void Write(Utf8JsonWriter writer, Continent value, JsonSerializerOptions options) - { - writer.WriteStringValue(value.Name); - } - } - internal class ContinentTypeConverter : TypeConverter { /// diff --git a/src/Tingle.Extensions.Primitives/Converters/ConnectionStringBuilderJsonConverter.cs b/src/Tingle.Extensions.Primitives/Converters/ConnectionStringBuilderJsonConverter.cs new file mode 100644 index 00000000..0afa5744 --- /dev/null +++ b/src/Tingle.Extensions.Primitives/Converters/ConnectionStringBuilderJsonConverter.cs @@ -0,0 +1,29 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Tingle.Extensions.Primitives.Converters; + +/// +/// A for +/// +public class ConnectionStringBuilderJsonConverter : JsonConverter +{ + /// + 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!); + } + + /// + public override void Write(Utf8JsonWriter writer, ConnectionStringBuilder value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString()); + } +} diff --git a/src/Tingle.Extensions.Primitives/Converters/ContinentJsonConverter.cs b/src/Tingle.Extensions.Primitives/Converters/ContinentJsonConverter.cs new file mode 100644 index 00000000..ad8d5af8 --- /dev/null +++ b/src/Tingle.Extensions.Primitives/Converters/ContinentJsonConverter.cs @@ -0,0 +1,23 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Tingle.Extensions.Primitives.Converters; + +/// +/// A for +/// +public class ContinentJsonConverter : JsonConverter +{ + /// + public override Continent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var s = reader.GetString(); + return string.IsNullOrWhiteSpace(s) ? null : new Continent(s); + } + + /// + public override void Write(Utf8JsonWriter writer, Continent value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.Name); + } +} diff --git a/src/Tingle.Extensions.Primitives/Converters/CountryJsonConverter.cs b/src/Tingle.Extensions.Primitives/Converters/CountryJsonConverter.cs new file mode 100644 index 00000000..1df49add --- /dev/null +++ b/src/Tingle.Extensions.Primitives/Converters/CountryJsonConverter.cs @@ -0,0 +1,23 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Tingle.Extensions.Primitives.Converters; + +/// +/// A for +/// +public class CountryJsonConverter : JsonConverter +{ + /// + public override Country? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var s = reader.GetString(); + return string.IsNullOrWhiteSpace(s) ? null : Country.FromCode(s); + } + + /// + public override void Write(Utf8JsonWriter writer, Country value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ThreeLetterCode); + } +} diff --git a/src/Tingle.Extensions.Primitives/Converters/CurrencyJsonConverter.cs b/src/Tingle.Extensions.Primitives/Converters/CurrencyJsonConverter.cs new file mode 100644 index 00000000..4966831a --- /dev/null +++ b/src/Tingle.Extensions.Primitives/Converters/CurrencyJsonConverter.cs @@ -0,0 +1,23 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Tingle.Extensions.Primitives.Converters; + +/// +/// A for +/// +public class CurrencyJsonConverter : JsonConverter +{ + /// + public override Currency? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var s = reader.GetString(); + return string.IsNullOrWhiteSpace(s) ? null : Currency.FromCode(s); + } + + /// + public override void Write(Utf8JsonWriter writer, Currency value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.Code); + } +} diff --git a/src/Tingle.Extensions.Primitives/Converters/DurationJsonConverter.cs b/src/Tingle.Extensions.Primitives/Converters/DurationJsonConverter.cs new file mode 100644 index 00000000..ef7097b9 --- /dev/null +++ b/src/Tingle.Extensions.Primitives/Converters/DurationJsonConverter.cs @@ -0,0 +1,29 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Tingle.Extensions.Primitives.Converters; + +/// +/// A for +/// +public class DurationJsonConverter : JsonConverter +{ + /// + 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!); + } + + /// + public override void Write(Utf8JsonWriter writer, Duration value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString()); + } +} diff --git a/src/Tingle.Extensions.Primitives/Converters/LanguageJsonConverter.cs b/src/Tingle.Extensions.Primitives/Converters/LanguageJsonConverter.cs new file mode 100644 index 00000000..d7d44aa9 --- /dev/null +++ b/src/Tingle.Extensions.Primitives/Converters/LanguageJsonConverter.cs @@ -0,0 +1,23 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Tingle.Extensions.Primitives; + +/// +/// A for +/// +public class LanguageJsonConverter : JsonConverter +{ + /// + public override Language? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var s = reader.GetString(); + return string.IsNullOrWhiteSpace(s) ? null : Language.FromCode(s); + } + + /// + public override void Write(Utf8JsonWriter writer, Language value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ThreeLetterCode); + } +} diff --git a/src/Tingle.Extensions.Primitives/Converters/SequenceNumberJsonConverter.cs b/src/Tingle.Extensions.Primitives/Converters/SequenceNumberJsonConverter.cs new file mode 100644 index 00000000..39557c87 --- /dev/null +++ b/src/Tingle.Extensions.Primitives/Converters/SequenceNumberJsonConverter.cs @@ -0,0 +1,28 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Tingle.Extensions.Primitives.Converters; + +/// +/// A for +/// +public class SequenceNumberJsonConverter : JsonConverter +{ + /// + 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); + } + + /// + public override void Write(Utf8JsonWriter writer, SequenceNumber value, JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } +} diff --git a/src/Tingle.Extensions.Primitives/Converters/SwiftCodeJsonConverter.cs b/src/Tingle.Extensions.Primitives/Converters/SwiftCodeJsonConverter.cs new file mode 100644 index 00000000..ac24f46e --- /dev/null +++ b/src/Tingle.Extensions.Primitives/Converters/SwiftCodeJsonConverter.cs @@ -0,0 +1,23 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Tingle.Extensions.Primitives.Converters; + +/// +/// A for +/// +public class SwiftCodeJsonConverter : JsonConverter +{ + /// + public override SwiftCode? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var s = reader.GetString(); + return string.IsNullOrWhiteSpace(s) ? null : SwiftCode.Parse(s); + } + + /// + public override void Write(Utf8JsonWriter writer, SwiftCode value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString()); + } +} diff --git a/src/Tingle.Extensions.Primitives/Country.cs b/src/Tingle.Extensions.Primitives/Country.cs index e95c9e55..b04d59be 100644 --- a/src/Tingle.Extensions.Primitives/Country.cs +++ b/src/Tingle.Extensions.Primitives/Country.cs @@ -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; @@ -161,22 +161,6 @@ object IConvertible.ToType(Type conversionType, IFormatProvider? provider) #endregion - internal class CountryJsonConverter : JsonConverter - { - /// - public override Country? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - var s = reader.GetString(); - return string.IsNullOrWhiteSpace(s) ? null : FromCode(s); - } - - /// - public override void Write(Utf8JsonWriter writer, Country value, JsonSerializerOptions options) - { - writer.WriteStringValue(value.ThreeLetterCode); - } - } - internal class CountryTypeConverter : TypeConverter { /// diff --git a/src/Tingle.Extensions.Primitives/Currency.cs b/src/Tingle.Extensions.Primitives/Currency.cs index 361ba646..4118fca5 100644 --- a/src/Tingle.Extensions.Primitives/Currency.cs +++ b/src/Tingle.Extensions.Primitives/Currency.cs @@ -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; @@ -157,22 +157,6 @@ object IConvertible.ToType(Type conversionType, IFormatProvider? provider) #endregion - internal class CurrencyJsonConverter : JsonConverter - { - /// - public override Currency? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - var s = reader.GetString(); - return string.IsNullOrWhiteSpace(s) ? null : FromCode(s); - } - - /// - public override void Write(Utf8JsonWriter writer, Currency value, JsonSerializerOptions options) - { - writer.WriteStringValue(value.Code); - } - } - internal class CurrencyTypeConverter : TypeConverter { /// diff --git a/src/Tingle.Extensions.Primitives/Duration.cs b/src/Tingle.Extensions.Primitives/Duration.cs index e2ed6e32..bf52c0be 100644 --- a/src/Tingle.Extensions.Primitives/Duration.cs +++ b/src/Tingle.Extensions.Primitives/Duration.cs @@ -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; @@ -445,30 +445,6 @@ object IConvertible.ToType(Type conversionType, IFormatProvider? provider) #endregion - internal class DurationJsonConverter : JsonConverter - { - public DurationJsonConverter() { } - - /// - 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!); - } - - /// - public override void Write(Utf8JsonWriter writer, Duration value, JsonSerializerOptions options) - { - writer.WriteStringValue(value.ToString()); - } - } - internal class DurationTypeConverter : TypeConverter { /// diff --git a/src/Tingle.Extensions.Primitives/Language.cs b/src/Tingle.Extensions.Primitives/Language.cs index c5ea54a8..bfbb0cba 100644 --- a/src/Tingle.Extensions.Primitives/Language.cs +++ b/src/Tingle.Extensions.Primitives/Language.cs @@ -2,7 +2,6 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; -using System.Text.Json; using System.Text.Json.Serialization; namespace Tingle.Extensions.Primitives; @@ -243,22 +242,6 @@ public enum LanguageScope Special } - internal class LanguageJsonConverter : JsonConverter - { - /// - public override Language? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - var s = reader.GetString(); - return string.IsNullOrWhiteSpace(s) ? null : FromCode(s); - } - - /// - public override void Write(Utf8JsonWriter writer, Language value, JsonSerializerOptions options) - { - writer.WriteStringValue(value.ThreeLetterCode); - } - } - internal class LanguageTypeConverter : TypeConverter { /// diff --git a/src/Tingle.Extensions.Primitives/SequenceNumber.cs b/src/Tingle.Extensions.Primitives/SequenceNumber.cs index b232cd77..4c7cd4cf 100644 --- a/src/Tingle.Extensions.Primitives/SequenceNumber.cs +++ b/src/Tingle.Extensions.Primitives/SequenceNumber.cs @@ -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; @@ -208,27 +208,6 @@ object IConvertible.ToType(Type conversionType, IFormatProvider? provider) #endregion - internal class SequenceNumberJsonConverter : JsonConverter - { - /// - 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); - } - - /// - public override void Write(Utf8JsonWriter writer, SequenceNumber value, JsonSerializerOptions options) - { - writer.WriteNumberValue(value.Value); - } - } - internal class SequenceNumberTypeConverter : TypeConverter { /// diff --git a/src/Tingle.Extensions.Primitives/SwiftCode.cs b/src/Tingle.Extensions.Primitives/SwiftCode.cs index f1e246e5..e3b3217d 100644 --- a/src/Tingle.Extensions.Primitives/SwiftCode.cs +++ b/src/Tingle.Extensions.Primitives/SwiftCode.cs @@ -1,9 +1,9 @@ using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; -using System.Text.Json; using System.Text.Json.Serialization; using System.Text.RegularExpressions; +using Tingle.Extensions.Primitives.Converters; namespace Tingle.Extensions.Primitives; @@ -224,22 +224,6 @@ object IConvertible.ToType(Type conversionType, IFormatProvider? provider) #endregion - internal class SwiftCodeJsonConverter : JsonConverter - { - /// - public override SwiftCode? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - var s = reader.GetString(); - return string.IsNullOrWhiteSpace(s) ? null : Parse(s); - } - - /// - public override void Write(Utf8JsonWriter writer, SwiftCode value, JsonSerializerOptions options) - { - writer.WriteStringValue(value.ToString()); - } - } - internal class SwiftCodeTypeConverter : TypeConverter { /// diff --git a/tests/Tingle.AspNetCore.Tokens.Tests/TokenJsonConverterTests.cs b/tests/Tingle.AspNetCore.Tokens.Tests/TokenJsonConverterTests.cs index 05d1e7d5..9e9f4a58 100644 --- a/tests/Tingle.AspNetCore.Tokens.Tests/TokenJsonConverterTests.cs +++ b/tests/Tingle.AspNetCore.Tokens.Tests/TokenJsonConverterTests.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using System.Text.Json; +using System.Text.Json.Serialization; namespace Tingle.AspNetCore.Tokens.Tests; @@ -38,9 +39,27 @@ public void Converter_Deserialization_Throws_NotSupportedException() + " Use model binding instead.", ex.Message); } - class TestModel + [Fact] + public void JsonSerializerContext_Works() + { + var src_json = @"{""token1"":""YyBpPyhOgEGAKQAkqvNFMg=="",""token2"":""GkTK64SntEWRw28wsnYQ5g==""}"; + var tdm = new TestDataClass { }; + var model = new TestModel + { + Token1 = new ContinuationToken(tdm, "YyBpPyhOgEGAKQAkqvNFMg=="), + Token2 = new TimedContinuationToken(tdm, "GkTK64SntEWRw28wsnYQ5g==", DateTimeOffset.UtcNow) + }; + var dst_json = JsonSerializer.Serialize(model, TestJsonSerializerContext.Default.TestModel); + Assert.Equal(src_json, dst_json); + } + + internal class TestModel { public ContinuationToken? Token1 { get; set; } public TimedContinuationToken? Token2 { get; set; } } } + +[JsonSerializable(typeof(TokenJsonConverterTests.TestModel))] +[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)] +internal partial class TestJsonSerializerContext : JsonSerializerContext { } diff --git a/tests/Tingle.Extensions.Primitives.Tests/ByteSizeTests.cs b/tests/Tingle.Extensions.Primitives.Tests/ByteSizeTests.cs index 0830f00c..7a573c5c 100644 --- a/tests/Tingle.Extensions.Primitives.Tests/ByteSizeTests.cs +++ b/tests/Tingle.Extensions.Primitives.Tests/ByteSizeTests.cs @@ -421,7 +421,16 @@ public void JsonConverter_RespectsBinaryFormat(long val, bool binary, string exp Assert.Equal(expected_json, dst_json); } - class TestModel + [Fact] + public void JsonSerializerContext_Works() + { + var src_json = "{\"size\":\"1 MiB\"}"; + var model = JsonSerializer.Deserialize(src_json, TestJsonSerializerContext.Default.ByteSizeTests_TestModel)!; + var dst_json = JsonSerializer.Serialize(model, TestJsonSerializerContext.Default.ByteSizeTests_TestModel); + Assert.Equal(src_json, dst_json); + } + + internal class TestModel { public ByteSize Size { get; set; } } diff --git a/tests/Tingle.Extensions.Primitives.Tests/ConnectionStringBuilderTests.cs b/tests/Tingle.Extensions.Primitives.Tests/ConnectionStringBuilderTests.cs index 287847f4..ef96d990 100644 --- a/tests/Tingle.Extensions.Primitives.Tests/ConnectionStringBuilderTests.cs +++ b/tests/Tingle.Extensions.Primitives.Tests/ConnectionStringBuilderTests.cs @@ -114,7 +114,16 @@ public void JsonConverter_Deserialization_Works(ConnectionStringBuilder value, s Assert.Equal(expected_json, dst_json); } - class TestModel + [Fact] + public void JsonSerializerContext_Works() + { + var src_json = "{\"value\":\"Hostname=contoso.com;Scheme=https;Key=abcd\"}"; + var model = JsonSerializer.Deserialize(src_json, TestJsonSerializerContext.Default.ConnectionStringBuilderTests_TestModel)!; + var dst_json = JsonSerializer.Serialize(model, TestJsonSerializerContext.Default.ConnectionStringBuilderTests_TestModel); + Assert.Equal(src_json, dst_json); + } + + internal class TestModel { public ConnectionStringBuilder Value { get; set; } } diff --git a/tests/Tingle.Extensions.Primitives.Tests/ContinentTests.cs b/tests/Tingle.Extensions.Primitives.Tests/ContinentTests.cs index 22621935..16c632f9 100644 --- a/tests/Tingle.Extensions.Primitives.Tests/ContinentTests.cs +++ b/tests/Tingle.Extensions.Primitives.Tests/ContinentTests.cs @@ -48,7 +48,16 @@ public void JsonConverter_Works() Assert.True(model.Continent!.IsKnown()); } - class TestModel + [Fact] + public void JsonSerializerContext_Works() + { + var src_json = "{\"continent\":\"Africa\"}"; + var model = JsonSerializer.Deserialize(src_json, TestJsonSerializerContext.Default.ContinentTests_TestModel)!; + var dst_json = JsonSerializer.Serialize(model, TestJsonSerializerContext.Default.ContinentTests_TestModel); + Assert.Equal(src_json, dst_json); + } + + internal class TestModel { public Continent? Continent { get; set; } } diff --git a/tests/Tingle.Extensions.Primitives.Tests/CountryTests.cs b/tests/Tingle.Extensions.Primitives.Tests/CountryTests.cs index 81ec5458..e1628ecd 100644 --- a/tests/Tingle.Extensions.Primitives.Tests/CountryTests.cs +++ b/tests/Tingle.Extensions.Primitives.Tests/CountryTests.cs @@ -139,7 +139,16 @@ public void JsonConverter_Works() Assert.Equal(src_json, dst_json); } - class TestModel + [Fact] + public void JsonSerializerContext_Works() + { + var src_json = "{\"country\":\"KEN\"}"; + var model = JsonSerializer.Deserialize(src_json, TestJsonSerializerContext.Default.CountryTests_TestModel)!; + var dst_json = JsonSerializer.Serialize(model, TestJsonSerializerContext.Default.CountryTests_TestModel); + Assert.Equal(src_json, dst_json); + } + + internal class TestModel { public Country? Country { get; set; } } diff --git a/tests/Tingle.Extensions.Primitives.Tests/CurrencyTests.cs b/tests/Tingle.Extensions.Primitives.Tests/CurrencyTests.cs index 196e2dd1..fd47bc61 100644 --- a/tests/Tingle.Extensions.Primitives.Tests/CurrencyTests.cs +++ b/tests/Tingle.Extensions.Primitives.Tests/CurrencyTests.cs @@ -125,7 +125,16 @@ public void JsonConverter_Works() Assert.Equal(src_json, dst_json); } - class TestModel + [Fact] + public void JsonSerializerContext_Works() + { + var src_json = "{\"currency\":\"KES\"}"; + var model = JsonSerializer.Deserialize(src_json, TestJsonSerializerContext.Default.CurrencyTests_TestModel)!; + var dst_json = JsonSerializer.Serialize(model, TestJsonSerializerContext.Default.CurrencyTests_TestModel); + Assert.Equal(src_json, dst_json); + } + + internal class TestModel { public Currency? Currency { get; set; } } diff --git a/tests/Tingle.Extensions.Primitives.Tests/DurationTests.cs b/tests/Tingle.Extensions.Primitives.Tests/DurationTests.cs index e4d588fb..9b76242e 100644 --- a/tests/Tingle.Extensions.Primitives.Tests/DurationTests.cs +++ b/tests/Tingle.Extensions.Primitives.Tests/DurationTests.cs @@ -244,7 +244,16 @@ public void JsonConverter_Deserialization_Works(Duration duration, string expect Assert.Equal(expected_json, dst_json); } - class TestModel + [Fact] + public void JsonSerializerContext_Works() + { + var src_json = "{\"duration\":\"P3M\"}"; + var model = JsonSerializer.Deserialize(src_json, TestJsonSerializerContext.Default.DurationTests_TestModel)!; + var dst_json = JsonSerializer.Serialize(model, TestJsonSerializerContext.Default.DurationTests_TestModel); + Assert.Equal(src_json, dst_json); + } + + internal class TestModel { public Duration Duration { get; set; } } diff --git a/tests/Tingle.Extensions.Primitives.Tests/EtagTests.cs b/tests/Tingle.Extensions.Primitives.Tests/EtagTests.cs index 02d3898b..137db986 100644 --- a/tests/Tingle.Extensions.Primitives.Tests/EtagTests.cs +++ b/tests/Tingle.Extensions.Primitives.Tests/EtagTests.cs @@ -205,7 +205,16 @@ public void JsonConverter_RespectsFormat(ulong val, string format, string expect Assert.Equal(expected_json, dst_json); } - class TestModel + [Fact] + public void JsonSerializerContext_Works() + { + var src_json = "{\"etag\":\"Fc1bBwAAAAA=\"}"; + var model = JsonSerializer.Deserialize(src_json, TestJsonSerializerContext.Default.EtagTests_TestModel)!; + var dst_json = JsonSerializer.Serialize(model, TestJsonSerializerContext.Default.EtagTests_TestModel); + Assert.Equal(src_json, dst_json); + } + + internal class TestModel { public Etag Etag { get; set; } } diff --git a/tests/Tingle.Extensions.Primitives.Tests/KsuidTests.cs b/tests/Tingle.Extensions.Primitives.Tests/KsuidTests.cs index 7ec174f6..2d1d1f65 100644 --- a/tests/Tingle.Extensions.Primitives.Tests/KsuidTests.cs +++ b/tests/Tingle.Extensions.Primitives.Tests/KsuidTests.cs @@ -204,7 +204,16 @@ public void JsonConverter_RespectsFormat(string val, string format, string expec Assert.Equal(expected_json, dst_json); } - class TestModel + [Fact] + public void JsonSerializerContext_Works() + { + var src_json = "{\"id\":\"0o5Fs0EELR0fUjHjbCnEtdUwQe3\"}"; + var model = JsonSerializer.Deserialize(src_json, TestJsonSerializerContext.Default.KsuidTests_TestModel)!; + var dst_json = JsonSerializer.Serialize(model, TestJsonSerializerContext.Default.KsuidTests_TestModel); + Assert.Equal(src_json, dst_json); + } + + internal class TestModel { public Ksuid? Id { get; set; } } diff --git a/tests/Tingle.Extensions.Primitives.Tests/LanguageTests.cs b/tests/Tingle.Extensions.Primitives.Tests/LanguageTests.cs index 2447abca..ccc43ba9 100644 --- a/tests/Tingle.Extensions.Primitives.Tests/LanguageTests.cs +++ b/tests/Tingle.Extensions.Primitives.Tests/LanguageTests.cs @@ -96,7 +96,16 @@ public void JsonConverter_Works() Assert.Equal(src_json, dst_json); } - class TestModel + [Fact] + public void JsonSerializerContext_Works() + { + var src_json = "{\"language\":\"eng\"}"; + var model = JsonSerializer.Deserialize(src_json, TestJsonSerializerContext.Default.LanguageTests_TestModel)!; + var dst_json = JsonSerializer.Serialize(model, TestJsonSerializerContext.Default.LanguageTests_TestModel); + Assert.Equal(src_json, dst_json); + } + + internal class TestModel { public Language? Language { get; set; } } diff --git a/tests/Tingle.Extensions.Primitives.Tests/MoneyTests.cs b/tests/Tingle.Extensions.Primitives.Tests/MoneyTests.cs index 18ef3e1a..f32ea586 100644 --- a/tests/Tingle.Extensions.Primitives.Tests/MoneyTests.cs +++ b/tests/Tingle.Extensions.Primitives.Tests/MoneyTests.cs @@ -269,7 +269,16 @@ public void JsonConverter_RespectsFormat(string val, string format, string expec Assert.Equal(expected_json, dst_json); } - class TestModel + [Fact] + public void JsonSerializerContext_Works() + { + var src_json = "{\"money\":\"KES 12.50\"}"; + var model = JsonSerializer.Deserialize(src_json, TestJsonSerializerContext.Default.MoneyTests_TestModel)!; + var dst_json = JsonSerializer.Serialize(model, TestJsonSerializerContext.Default.MoneyTests_TestModel); + Assert.Equal(src_json, dst_json); + } + + internal class TestModel { public Money? Money { get; set; } } diff --git a/tests/Tingle.Extensions.Primitives.Tests/SequenceNumberTests.cs b/tests/Tingle.Extensions.Primitives.Tests/SequenceNumberTests.cs index 1b6694d9..28e7754a 100644 --- a/tests/Tingle.Extensions.Primitives.Tests/SequenceNumberTests.cs +++ b/tests/Tingle.Extensions.Primitives.Tests/SequenceNumberTests.cs @@ -175,7 +175,16 @@ public void JsonConverter_Works(string raw, long val) Assert.Equal(val, model!.Position); } - class TestModel + [Fact] + public void JsonSerializerContext_Works() + { + var src_json = "{\"position\":123456789}"; + var model = JsonSerializer.Deserialize(src_json, TestJsonSerializerContext.Default.SequenceNumberTests_TestModel)!; + var dst_json = JsonSerializer.Serialize(model, TestJsonSerializerContext.Default.SequenceNumberTests_TestModel); + Assert.Equal(src_json, dst_json); + } + + internal class TestModel { public SequenceNumber Position { get; set; } } diff --git a/tests/Tingle.Extensions.Primitives.Tests/SwiftCodeTests.cs b/tests/Tingle.Extensions.Primitives.Tests/SwiftCodeTests.cs index ad706ac0..bb6dcfad 100644 --- a/tests/Tingle.Extensions.Primitives.Tests/SwiftCodeTests.cs +++ b/tests/Tingle.Extensions.Primitives.Tests/SwiftCodeTests.cs @@ -135,7 +135,16 @@ public void JsonConverter_Works() Assert.Equal(src_json, dst_json); } - class TestModel + [Fact] + public void JsonSerializerContext_Works() + { + var src_json = "{\"swiftCode\":\"KCBLKENXXXX\"}"; + var model = JsonSerializer.Deserialize(src_json, TestJsonSerializerContext.Default.SwiftCodeTests_TestModel)!; + var dst_json = JsonSerializer.Serialize(model, TestJsonSerializerContext.Default.SwiftCodeTests_TestModel); + Assert.Equal(src_json, dst_json); + } + + internal class TestModel { public SwiftCode? SwiftCode { get; set; } } diff --git a/tests/Tingle.Extensions.Primitives.Tests/TestJsonSerializerContext.cs b/tests/Tingle.Extensions.Primitives.Tests/TestJsonSerializerContext.cs new file mode 100644 index 00000000..34b8a222 --- /dev/null +++ b/tests/Tingle.Extensions.Primitives.Tests/TestJsonSerializerContext.cs @@ -0,0 +1,18 @@ +using System.Text.Json.Serialization; + +namespace Tingle.Extensions.Primitives.Tests; + +[JsonSerializable(typeof(ByteSizeTests.TestModel), TypeInfoPropertyName = "ByteSizeTests_TestModel")] +[JsonSerializable(typeof(ConnectionStringBuilderTests.TestModel), TypeInfoPropertyName = "ConnectionStringBuilderTests_TestModel")] +[JsonSerializable(typeof(ContinentTests.TestModel), TypeInfoPropertyName = "ContinentTests_TestModel")] +[JsonSerializable(typeof(CountryTests.TestModel), TypeInfoPropertyName = "CountryTests_TestModel")] +[JsonSerializable(typeof(CurrencyTests.TestModel), TypeInfoPropertyName = "CurrencyTests_TestModel")] +[JsonSerializable(typeof(DurationTests.TestModel), TypeInfoPropertyName = "DurationTests_TestModel")] +[JsonSerializable(typeof(EtagTests.TestModel), TypeInfoPropertyName = "EtagTests_TestModel")] +[JsonSerializable(typeof(KsuidTests.TestModel), TypeInfoPropertyName = "KsuidTests_TestModel")] +[JsonSerializable(typeof(LanguageTests.TestModel), TypeInfoPropertyName = "LanguageTests_TestModel")] +[JsonSerializable(typeof(MoneyTests.TestModel), TypeInfoPropertyName = "MoneyTests_TestModel")] +[JsonSerializable(typeof(SequenceNumberTests.TestModel), TypeInfoPropertyName = "SequenceNumberTests_TestModel")] +[JsonSerializable(typeof(SwiftCodeTests.TestModel), TypeInfoPropertyName = "SwiftCodeTests_TestModel")] +[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)] +internal partial class TestJsonSerializerContext : JsonSerializerContext { }