-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create system for registering custom LiteralConverters (#269)
Motivation ---------- Extensions may need to control serialization and deserialization of literals beyond the types including in-box. Modifications ------------- - Define `LiteralConverter` and `LiteralConverter<T>` abstract base types for converters - Create `LiteralConverterRegistry` for registering converters - Create converters for all currently supported types and register them automatically - Rewrite `LiteralSerializer` to use the singleton from `LiteralConverterRegistry.Instance` - Add `System.Collections.Immutable` as a dependency on generated SDKs when targeting downlevel frameworks to get access to `FrozenDictionary` Breaking Changes ---------------- The namespace of `LiteralSerializer` is changed to `XXX.Serialization.Literals` in generated SDKs. Results ------- A more extensible framework for extensions that want to support additional types in headers, path parameters, and query strings. A potential future use is an extension for NodaTime type support. SDK consumers that need to customize serialization may also do so by mutating the public `LiteralConverterRegistry.Instance`. Also fixes a bug deserializing nullable enums from literals. Resolves #268 Fixes #267
- Loading branch information
1 parent
81b3c2f
commit f18e744
Showing
35 changed files
with
939 additions
and
268 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
262 changes: 0 additions & 262 deletions
262
src/main/Yardarm.Client/Serialization/LiteralSerializer.cs
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
src/main/Yardarm.Client/Serialization/Literals/Converters/BooleanLiteralConverter.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; | ||
|
||
namespace RootNamespace.Serialization.Literals.Converters; | ||
|
||
internal sealed class BooleanLiteralConverter : ValueTypeLiteralConverter<bool> | ||
{ | ||
protected override bool ReadCore(string value, string? format) => | ||
bool.Parse(value); | ||
|
||
public override string Write(bool value, string? format) => value ? "true" : "false"; | ||
|
||
#if NET6_0_OR_GREATER | ||
|
||
public override bool TryWrite(bool value, ReadOnlySpan<char> format, Span<char> destination, out int charsWritten) | ||
{ | ||
var boolString = value ? "true" : "false"; | ||
if (boolString.TryCopyTo(destination)) | ||
{ | ||
charsWritten = boolString.Length; | ||
return true; | ||
} | ||
|
||
charsWritten = 0; | ||
return false; | ||
} | ||
|
||
#endif | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/Yardarm.Client/Serialization/Literals/Converters/ByteLiteralConverter.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,21 @@ | ||
using System; | ||
|
||
namespace RootNamespace.Serialization.Literals.Converters; | ||
|
||
internal sealed class ByteLiteralConverter : ValueTypeLiteralConverter<byte> | ||
{ | ||
// CultureInfo.InvariantCulture is not needed for unsigned integers | ||
|
||
protected override byte ReadCore(string value, string? format) => | ||
byte.Parse(value); | ||
|
||
public override string Write(byte value, string? format) => | ||
value.ToString(); | ||
|
||
#if NET6_0_OR_GREATER | ||
|
||
public override bool TryWrite(byte value, ReadOnlySpan<char> format, Span<char> destination, out int charsWritten) => | ||
value.TryFormat(destination, out charsWritten); | ||
|
||
#endif | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/Yardarm.Client/Serialization/Literals/Converters/DateTimeLiteralConverter.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,32 @@ | ||
using System; | ||
using System.Globalization; | ||
|
||
namespace RootNamespace.Serialization.Literals.Converters; | ||
|
||
internal sealed class DateTimeLiteralConverter : ValueTypeLiteralConverter<DateTime> | ||
{ | ||
protected override DateTime ReadCore(string value, string? format) => | ||
format switch | ||
{ | ||
"date" or "full-date" => DateTime.ParseExact(value, "yyyy-MM-dd", CultureInfo.InvariantCulture), | ||
_ => DateTime.Parse(value, CultureInfo.InvariantCulture) | ||
}; | ||
|
||
public override string Write(DateTime value, string? format) => | ||
format switch | ||
{ | ||
"date" or "full-date" => value.ToString("yyyy-MM-dd"), | ||
_ => value.ToString("O") | ||
}; | ||
|
||
#if NET6_0_OR_GREATER | ||
|
||
public override bool TryWrite(DateTime value, ReadOnlySpan<char> format, Span<char> destination, out int charsWritten) => | ||
format switch | ||
{ | ||
"date" or "full-date" => value.TryFormat(destination, out charsWritten, format: "yyyy-MM-dd"), | ||
_ => value.TryFormat(destination, out charsWritten, format: "O") | ||
}; | ||
|
||
#endif | ||
} |
Oops, something went wrong.