-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use source generation on json api serialization/deserialization.
- Loading branch information
1 parent
31b207a
commit 519cfa9
Showing
16 changed files
with
198 additions
and
118 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace E5Renewer.Controllers | ||
{ | ||
internal static class IQueryCollectionExtends | ||
{ | ||
public static JsonAPIV1Request ToJsonAPIV1Request(this IQueryCollection queryCollection) | ||
{ | ||
StringValues value; | ||
long timestamp = queryCollection.TryGetValue(nameof(timestamp), out value) && | ||
long.TryParse(value, out long parsedValue) | ||
? parsedValue : default; | ||
string method; | ||
if (queryCollection.TryGetValue(nameof(method), out value)) | ||
{ | ||
method = value.FirstOrDefault() ?? string.Empty; | ||
} | ||
else | ||
{ | ||
method = string.Empty; | ||
} | ||
|
||
Dictionary<string, string?> args = | ||
queryCollection.TakeWhile((kv) => kv.Key != nameof(timestamp) && kv.Key != nameof(method)) | ||
.Select((kv) => new KeyValuePair<string, string?>(kv.Key, kv.Value.FirstOrDefault())) | ||
.ToDictionary(); | ||
|
||
return new(timestamp, method, new(args)); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections.ObjectModel; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace E5Renewer.Controllers | ||
{ | ||
/// <summary>Readonly struct to store json api request.</summary> | ||
public readonly struct JsonAPIV1Request | ||
{ | ||
/// <value>If this protocol is valid.</value> | ||
[JsonIgnore] | ||
public bool valid { get => timestamp > 0 && !string.IsNullOrEmpty(method) && string.IsNullOrWhiteSpace(method); } | ||
|
||
/// <value>The timestamp.</value> | ||
public long timestamp { get; } | ||
|
||
/// <value>Request method.</value> | ||
/// <remarks>Is **NOT** HttpContext.Request.Method</remarks> | ||
public string? method { get; } | ||
|
||
/// <value>The params to be passed to request method.</value> | ||
public ReadOnlyDictionary<string, string?> args { get; } | ||
|
||
/// <summary>Initialize <see cref="JsonAPIV1Request"/> with arguments given.</summary> | ||
[JsonConstructor] | ||
public JsonAPIV1Request(long timestamp, string? method, ReadOnlyDictionary<string, string?> args) => | ||
(this.timestamp, this.method, this.args) = (timestamp, method, args); | ||
} | ||
|
||
[JsonSourceGenerationOptions( | ||
WriteIndented = true, | ||
PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower, | ||
IncludeFields = true | ||
)] | ||
[JsonSerializable(typeof(JsonAPIV1Request))] | ||
internal partial class JsonAPIV1RequestJsonSerializerContext : JsonSerializerContext { } | ||
} |
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,45 @@ | ||
using System.Collections.ObjectModel; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace E5Renewer.Controllers | ||
{ | ||
/// <summary>Readonly struct to store json api response.</summary> | ||
public readonly struct JsonAPIV1Response | ||
{ | ||
/// <value>If this protocol is valid.</value> | ||
[JsonIgnore] | ||
public bool valid { get => timestamp > 0 && !string.IsNullOrEmpty(method) && string.IsNullOrWhiteSpace(method); } | ||
|
||
/// <value>The timestamp.</value> | ||
public long timestamp { get; } | ||
|
||
/// <value>Request method.</value> | ||
/// <remarks>Is **NOT** HttpContext.Request.Method</remarks> | ||
public string method { get; } | ||
|
||
/// <value>The request result.</value> | ||
public object? result { get; } | ||
|
||
/// <value>The params to be passed to request method.</value> | ||
public ReadOnlyDictionary<string, string?> args { get; } | ||
|
||
/// <summary>Initialize <see cref="JsonAPIV1Response"/> with arguments given.</summary> | ||
[JsonConstructor] | ||
public JsonAPIV1Response(long timestamp, string method, object? result, ReadOnlyDictionary<string, string?> args) => | ||
(this.timestamp, this.method, this.result, this.args) = (timestamp, method, result, args); | ||
} | ||
|
||
[JsonSourceGenerationOptions( | ||
WriteIndented = true, | ||
PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower, | ||
IncludeFields = true | ||
)] | ||
[JsonSerializable(typeof(JsonAPIV1Response))] | ||
[JsonSerializable(typeof(IEnumerable<string>))] | ||
[JsonSerializable(typeof(IEnumerable<object>))] | ||
[JsonSerializable(typeof(bool))] | ||
[JsonSerializable(typeof(int))] | ||
[JsonSerializable(typeof(string))] | ||
[JsonSerializable(typeof(object))] | ||
internal partial class JsonAPIV1ResponseJsonSerializerContext : JsonSerializerContext { } | ||
} |
Oops, something went wrong.