-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure all JSON converters are public to allow referencing by JSON…
… source generators (#253)
- Loading branch information
1 parent
31d7735
commit 363500d
Showing
31 changed files
with
367 additions
and
171 deletions.
There are no files selected for viewing
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
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
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
29 changes: 29 additions & 0 deletions
29
src/Tingle.Extensions.Primitives/Converters/ConnectionStringBuilderJsonConverter.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,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()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Tingle.Extensions.Primitives/Converters/ContinentJsonConverter.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,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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Tingle.Extensions.Primitives/Converters/CountryJsonConverter.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,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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Tingle.Extensions.Primitives/Converters/CurrencyJsonConverter.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,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); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Tingle.Extensions.Primitives/Converters/DurationJsonConverter.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,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()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Tingle.Extensions.Primitives/Converters/LanguageJsonConverter.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,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); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Tingle.Extensions.Primitives/Converters/SequenceNumberJsonConverter.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,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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Tingle.Extensions.Primitives/Converters/SwiftCodeJsonConverter.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,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()); | ||
} | ||
} |
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
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
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
Oops, something went wrong.