Initial commit, basic proxying working
This commit is contained in:
20
Myriad/Serialization/JsonSerializerOptionsExtensions.cs
Normal file
20
Myriad/Serialization/JsonSerializerOptionsExtensions.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Myriad.Serialization
|
||||
{
|
||||
public static class JsonSerializerOptionsExtensions
|
||||
{
|
||||
public static JsonSerializerOptions ConfigureForNewcord(this JsonSerializerOptions opts)
|
||||
{
|
||||
opts.PropertyNamingPolicy = new JsonSnakeCaseNamingPolicy();
|
||||
opts.NumberHandling = JsonNumberHandling.AllowReadingFromString;
|
||||
opts.IncludeFields = true;
|
||||
|
||||
opts.Converters.Add(new PermissionSetJsonConverter());
|
||||
opts.Converters.Add(new ShardInfoJsonConverter());
|
||||
|
||||
return opts;
|
||||
}
|
||||
}
|
||||
}
|
88
Myriad/Serialization/JsonSnakeCaseNamingPolicy.cs
Normal file
88
Myriad/Serialization/JsonSnakeCaseNamingPolicy.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Myriad.Serialization
|
||||
{
|
||||
// From https://github.com/J0rgeSerran0/JsonNamingPolicy/blob/master/JsonSnakeCaseNamingPolicy.cs, no NuGet :/
|
||||
public class JsonSnakeCaseNamingPolicy: JsonNamingPolicy
|
||||
{
|
||||
private readonly string _separator = "_";
|
||||
|
||||
public override string ConvertName(string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name) || string.IsNullOrWhiteSpace(name)) return string.Empty;
|
||||
|
||||
ReadOnlySpan<char> spanName = name.Trim();
|
||||
|
||||
var stringBuilder = new StringBuilder();
|
||||
var addCharacter = true;
|
||||
|
||||
var isPreviousSpace = false;
|
||||
var isPreviousSeparator = false;
|
||||
var isCurrentSpace = false;
|
||||
var isNextLower = false;
|
||||
var isNextUpper = false;
|
||||
var isNextSpace = false;
|
||||
|
||||
for (var position = 0; position < spanName.Length; position++)
|
||||
{
|
||||
if (position != 0)
|
||||
{
|
||||
isCurrentSpace = spanName[position] == 32;
|
||||
isPreviousSpace = spanName[position - 1] == 32;
|
||||
isPreviousSeparator = spanName[position - 1] == 95;
|
||||
|
||||
if (position + 1 != spanName.Length)
|
||||
{
|
||||
isNextLower = spanName[position + 1] > 96 && spanName[position + 1] < 123;
|
||||
isNextUpper = spanName[position + 1] > 64 && spanName[position + 1] < 91;
|
||||
isNextSpace = spanName[position + 1] == 32;
|
||||
}
|
||||
|
||||
if (isCurrentSpace &&
|
||||
(isPreviousSpace ||
|
||||
isPreviousSeparator ||
|
||||
isNextUpper ||
|
||||
isNextSpace))
|
||||
{
|
||||
addCharacter = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
var isCurrentUpper = spanName[position] > 64 && spanName[position] < 91;
|
||||
var isPreviousLower = spanName[position - 1] > 96 && spanName[position - 1] < 123;
|
||||
var isPreviousNumber = spanName[position - 1] > 47 && spanName[position - 1] < 58;
|
||||
|
||||
if (isCurrentUpper &&
|
||||
(isPreviousLower ||
|
||||
isPreviousNumber ||
|
||||
isNextLower ||
|
||||
isNextSpace ||
|
||||
isNextLower && !isPreviousSpace))
|
||||
{
|
||||
stringBuilder.Append(_separator);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isCurrentSpace &&
|
||||
!isPreviousSpace &&
|
||||
!isNextSpace)
|
||||
{
|
||||
stringBuilder.Append(_separator);
|
||||
addCharacter = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (addCharacter)
|
||||
stringBuilder.Append(spanName[position]);
|
||||
else
|
||||
addCharacter = true;
|
||||
}
|
||||
|
||||
return stringBuilder.ToString().ToLower();
|
||||
}
|
||||
}
|
||||
}
|
22
Myriad/Serialization/JsonStringConverter.cs
Normal file
22
Myriad/Serialization/JsonStringConverter.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Myriad.Serialization
|
||||
{
|
||||
public class JsonStringConverter: JsonConverter<object>
|
||||
{
|
||||
public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var str = JsonSerializer.Deserialize<string>(ref reader);
|
||||
var inner = JsonSerializer.Deserialize(str!, typeToConvert, options);
|
||||
return inner;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
|
||||
{
|
||||
var inner = JsonSerializer.Serialize(value, options);
|
||||
writer.WriteStringValue(inner);
|
||||
}
|
||||
}
|
||||
}
|
24
Myriad/Serialization/PermissionSetJsonConverter.cs
Normal file
24
Myriad/Serialization/PermissionSetJsonConverter.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using Myriad.Types;
|
||||
|
||||
namespace Myriad.Serialization
|
||||
{
|
||||
public class PermissionSetJsonConverter: JsonConverter<PermissionSet>
|
||||
{
|
||||
public override PermissionSet Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var str = reader.GetString();
|
||||
if (str == null) return default;
|
||||
|
||||
return (PermissionSet) ulong.Parse(str);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, PermissionSet value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(((ulong) value).ToString());
|
||||
}
|
||||
}
|
||||
}
|
28
Myriad/Serialization/ShardInfoJsonConverter.cs
Normal file
28
Myriad/Serialization/ShardInfoJsonConverter.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using Myriad.Gateway;
|
||||
|
||||
namespace Myriad.Serialization
|
||||
{
|
||||
public class ShardInfoJsonConverter: JsonConverter<ShardInfo>
|
||||
{
|
||||
public override ShardInfo? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var arr = JsonSerializer.Deserialize<int[]>(ref reader);
|
||||
if (arr?.Length != 2)
|
||||
throw new JsonException("Expected shard info as array of length 2");
|
||||
|
||||
return new ShardInfo(arr[0], arr[1]);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, ShardInfo value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStartArray();
|
||||
writer.WriteNumberValue(value.ShardId);
|
||||
writer.WriteNumberValue(value.NumShards);
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user