-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
234 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using BoardGameGeek.Dungeon.Extensions; | ||
using Flurl.Http; | ||
|
||
namespace BoardGameGeek.Dungeon.Converters | ||
{ | ||
public sealed class CookieConverter : JsonConverter<FlurlCookie> | ||
{ | ||
public override FlurlCookie Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
reader.CheckStartObject(); | ||
var originUrl = reader.ReadString("OriginUrl"); | ||
var dateReceived = reader.ReadDateTimeOffset("DateReceived"); | ||
var name = reader.ReadString("Name"); | ||
var value = reader.ReadString("Value"); | ||
var expires = reader.ReadNullableDateTimeOffset("Expires"); | ||
var maxAge = reader.ReadNullableInt32("MaxAge"); | ||
var domain = reader.ReadString("Domain"); | ||
var path = reader.ReadString("Path"); | ||
var secure = reader.ReadBoolean("Secure"); | ||
var httpOnly = reader.ReadBoolean("HttpOnly"); | ||
var sameSite = reader.ReadNullableEnum<SameSite>("SameSite"); | ||
reader.ReadEndObject(); | ||
|
||
return new FlurlCookie(name, value, originUrl, dateReceived) | ||
{ | ||
Expires = expires, | ||
MaxAge = maxAge, | ||
Domain = domain, | ||
Path = path, | ||
Secure = secure, | ||
HttpOnly = httpOnly, | ||
SameSite = sameSite | ||
}; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, FlurlCookie value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WriteString("OriginUrl", value.OriginUrl); | ||
writer.WriteDateTimeOffset("DateReceived", value.DateReceived); | ||
writer.WriteString("Name", value.Name); | ||
writer.WriteString("Value", value.Value); | ||
writer.WriteNullableDateTimeOffset("Expires", value.Expires); | ||
writer.WriteNullableNumber("MaxAge", value.MaxAge); | ||
writer.WriteString("Domain", value.Domain); | ||
writer.WriteString("Path", value.Path); | ||
writer.WriteBoolean("Secure", value.Secure); | ||
writer.WriteBoolean("HttpOnly", value.HttpOnly); | ||
writer.WriteNullableEnum("SameSite", value.SameSite); | ||
writer.WriteEndObject(); | ||
} | ||
} | ||
} |
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,92 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Text.Json; | ||
|
||
namespace BoardGameGeek.Dungeon.Extensions | ||
{ | ||
public static class Utf8JsonReaderExtensions | ||
{ | ||
public static void CheckStartObject(this ref Utf8JsonReader reader) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartObject) | ||
{ | ||
throw new JsonException(); | ||
} | ||
} | ||
|
||
public static void CheckEndObject(this ref Utf8JsonReader reader) | ||
{ | ||
if (reader.TokenType != JsonTokenType.EndObject) | ||
{ | ||
throw new JsonException(); | ||
} | ||
} | ||
|
||
public static T GetEnum<T>(this ref Utf8JsonReader reader) where T : struct, Enum => Enum.Parse<T>(reader.GetString()!); | ||
|
||
public static DateTime GetDateTime(this ref Utf8JsonReader reader) => DateTime.Parse(reader.GetString()!); | ||
|
||
public static DateTimeOffset GetDateTimeOffset(this ref Utf8JsonReader reader) => DateTimeOffset.Parse(reader.GetString()!); | ||
|
||
public static TimeSpan GetTimeSpan(this ref Utf8JsonReader reader) => TimeSpan.Parse(reader.GetString()!, CultureInfo.InvariantCulture); | ||
|
||
public static bool? GetNullableBoolean(this ref Utf8JsonReader reader) => reader.TokenType != JsonTokenType.Null ? reader.GetBoolean() : (bool?)null; | ||
|
||
public static int? GetNullableInt32(this ref Utf8JsonReader reader) => reader.TokenType != JsonTokenType.Null ? reader.GetInt32() : (int?)null; | ||
|
||
public static T? GetNullableEnum<T>(this ref Utf8JsonReader reader) where T : struct, Enum => reader.TokenType != JsonTokenType.Null ? reader.GetEnum<T>() : (T?)null; | ||
|
||
public static DateTime? GetNullableDateTime(this ref Utf8JsonReader reader) => reader.TokenType != JsonTokenType.Null ? reader.GetDateTime() : (DateTime?)null; | ||
|
||
public static DateTimeOffset? GetNullableDateTimeOffset(this ref Utf8JsonReader reader) => reader.TokenType != JsonTokenType.Null ? reader.GetDateTimeOffset() : (DateTimeOffset?)null; | ||
|
||
public static TimeSpan? GetNullableTimeSpan(this ref Utf8JsonReader reader) => reader.TokenType != JsonTokenType.Null ? reader.GetTimeSpan() : (TimeSpan?)null; | ||
|
||
public static void ReadStartObject(this ref Utf8JsonReader reader) | ||
{ | ||
reader.Read(); | ||
reader.CheckStartObject(); | ||
} | ||
|
||
public static void ReadEndObject(this ref Utf8JsonReader reader) | ||
{ | ||
reader.Read(); | ||
reader.CheckEndObject(); | ||
} | ||
|
||
public static bool ReadBoolean(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetBoolean(); | ||
|
||
public static string ReadString(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetString(); | ||
|
||
public static T ReadEnum<T>(this ref Utf8JsonReader reader, string propertyName) where T : struct, Enum => reader.ReadProperty(propertyName).GetEnum<T>(); | ||
|
||
public static DateTime ReadDateTime(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetDateTime(); | ||
|
||
public static DateTimeOffset ReadDateTimeOffset(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetDateTimeOffset(); | ||
|
||
public static TimeSpan ReadTimeSpan(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetTimeSpan(); | ||
|
||
public static bool? ReadNullableBoolean(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetNullableBoolean(); | ||
|
||
public static int? ReadNullableInt32(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetNullableInt32(); | ||
|
||
public static T? ReadNullableEnum<T>(this ref Utf8JsonReader reader, string propertyName) where T : struct, Enum => reader.ReadProperty(propertyName).GetNullableEnum<T>(); | ||
|
||
public static DateTime? ReadNullableDateTime(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetNullableDateTime(); | ||
|
||
public static DateTimeOffset? ReadNullableDateTimeOffset(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetNullableDateTimeOffset(); | ||
|
||
public static TimeSpan? ReadNullableTimeSpan(this ref Utf8JsonReader reader, string propertyName) => reader.ReadProperty(propertyName).GetNullableTimeSpan(); | ||
|
||
private static ref Utf8JsonReader ReadProperty(this ref Utf8JsonReader reader, string propertyName) | ||
{ | ||
reader.Read(); | ||
if (reader.TokenType != JsonTokenType.PropertyName || reader.GetString() != propertyName) | ||
{ | ||
throw new JsonException(); | ||
} | ||
reader.Read(); | ||
return ref reader; | ||
} | ||
} | ||
} |
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,41 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Runtime.CompilerServices; | ||
using System.Text.Json; | ||
|
||
namespace BoardGameGeek.Dungeon.Extensions | ||
{ | ||
public static class Utf8JsonWriterExtensions | ||
{ | ||
public static void WriteEnum<T>(this Utf8JsonWriter writer, string propertyName, T value) where T : struct, Enum => writer.WriteString(propertyName, value.ToString()); | ||
|
||
public static void WriteDateTime(this Utf8JsonWriter writer, string propertyName, DateTime value) => writer.WriteString(propertyName, value); | ||
|
||
public static void WriteDateTimeOffset(this Utf8JsonWriter writer, string propertyName, DateTimeOffset value) => writer.WriteString(propertyName, value); | ||
|
||
public static void WriteTimeSpan(this Utf8JsonWriter writer, string propertyName, TimeSpan value) => writer.WriteString(propertyName, value.ToString(null, CultureInfo.InvariantCulture)); | ||
|
||
public static void WriteNullableBoolean(this Utf8JsonWriter writer, string propertyName, bool? value) => writer.WriteNull(propertyName, value)?.WriteBoolean(propertyName, value!.Value); | ||
|
||
public static void WriteNullableNumber(this Utf8JsonWriter writer, string propertyName, int? value) => writer.WriteNull(propertyName, value)?.WriteNumber(propertyName, value!.Value); | ||
|
||
public static void WriteNullableEnum<T>(this Utf8JsonWriter writer, string propertyName, T? value) where T : struct, Enum => writer.WriteNull(propertyName, value)?.WriteEnum(propertyName, value!.Value); | ||
|
||
public static void WriteNullableDateTime(this Utf8JsonWriter writer, string propertyName, DateTime? value) => writer.WriteNull(propertyName, value)?.WriteDateTime(propertyName, value!.Value); | ||
|
||
public static void WriteNullableDateTimeOffset(this Utf8JsonWriter writer, string propertyName, DateTimeOffset? value) => writer.WriteNull(propertyName, value)?.WriteDateTimeOffset(propertyName, value!.Value); | ||
|
||
public static void WriteNullableTimeSpan(this Utf8JsonWriter writer, string propertyName, TimeSpan? value) => writer.WriteNull(propertyName, value)?.WriteTimeSpan(propertyName, value!.Value); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Utf8JsonWriter WriteNull<T>(this Utf8JsonWriter writer, string propertyName, T? value) where T : struct | ||
{ | ||
if (!value.HasValue) | ||
{ | ||
writer.WriteNull(propertyName); | ||
return null; | ||
} | ||
return writer; | ||
} | ||
} | ||
} |
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.