diff --git a/Lagrange.OneBot/Core/Entity/Common/LightApp.cs b/Lagrange.OneBot/Core/Entity/Common/LightApp.cs index 0fad7fdc7..f2aa95e25 100644 --- a/Lagrange.OneBot/Core/Entity/Common/LightApp.cs +++ b/Lagrange.OneBot/Core/Entity/Common/LightApp.cs @@ -1,4 +1,5 @@ using System.Text.Json.Serialization; +using Lagrange.OneBot.Core.Operation.Converters; namespace Lagrange.OneBot.Core.Entity.Common; #pragma warning disable CS8618 @@ -14,9 +15,9 @@ public class LightApp [JsonPropertyName("from")] public long From { get; set; } - [JsonPropertyName("meta")] public Meta Meta { get; set; } + [JsonPropertyName("meta")] [JsonConverter(typeof(NullIfEmptyConverter))] public Meta Meta { get; set; } - [JsonPropertyName("extra")] public Extra? Extra { get; set; } + [JsonPropertyName("extra")] [JsonConverter(typeof(NullIfEmptyConverter))] public Extra? Extra { get; set; } [JsonPropertyName("prompt")] public string Prompt { get; set; } diff --git a/Lagrange.OneBot/Core/Operation/Converters/AutosizeConverter.cs b/Lagrange.OneBot/Core/Operation/Converters/AutosizeConverter.cs index 2bc8620e9..0bbc1512e 100644 --- a/Lagrange.OneBot/Core/Operation/Converters/AutosizeConverter.cs +++ b/Lagrange.OneBot/Core/Operation/Converters/AutosizeConverter.cs @@ -14,6 +14,7 @@ public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSer JsonTokenType.True => true, JsonTokenType.False => false, JsonTokenType.String when Utf8Parser.TryParse(reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan, out bool value, out _) => value, + JsonTokenType.String when Utf8Parser.TryParse(reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan, out int intValue, out _) => intValue != 0, JsonTokenType.Number when reader.TryGetInt32(out int val) => Convert.ToBoolean(val), _ => throw new JsonException() }; diff --git a/Lagrange.OneBot/Core/Operation/Converters/ForwardConverter.cs b/Lagrange.OneBot/Core/Operation/Converters/ForwardConverter.cs new file mode 100644 index 000000000..d3976174e --- /dev/null +++ b/Lagrange.OneBot/Core/Operation/Converters/ForwardConverter.cs @@ -0,0 +1,22 @@ +using System.Buffers; +using System.Buffers.Text; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Lagrange.OneBot.Core.Operation.Converters; + +public class ForwardConverter : JsonConverter +{ + public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return reader.TokenType switch + { + JsonTokenType.True => 1, + JsonTokenType.False => 0, + JsonTokenType.Number when reader.TryGetInt32(out int intValue) => intValue, + _ => throw new JsonException() + }; + } + + public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options) => writer.WriteNumberValue(value); +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Operation/Converters/NullIfEmptyConverter.cs b/Lagrange.OneBot/Core/Operation/Converters/NullIfEmptyConverter.cs new file mode 100644 index 000000000..eae49f967 --- /dev/null +++ b/Lagrange.OneBot/Core/Operation/Converters/NullIfEmptyConverter.cs @@ -0,0 +1,20 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Lagrange.OneBot.Core.Operation.Converters; + +public class NullIfEmptyConverter : JsonConverter where T : class +{ + public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return reader.TokenType switch + { + JsonTokenType.String when reader.ValueSpan.Length == 0 => null, + JsonTokenType.StartObject => JsonSerializer.Deserialize(ref reader, options), + _ => throw new JsonException() + }; + } + + public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options) => JsonSerializer.Serialize(writer, value, options); +} \ No newline at end of file diff --git a/Lagrange.OneBot/Message/Entity/LocationSegment.cs b/Lagrange.OneBot/Message/Entity/LocationSegment.cs index f69b437a1..cad592b6a 100644 --- a/Lagrange.OneBot/Message/Entity/LocationSegment.cs +++ b/Lagrange.OneBot/Message/Entity/LocationSegment.cs @@ -24,7 +24,7 @@ public LocationSegment() : this(0f, 0f) { } [SegmentSubscriber(typeof(LightAppEntity), "location")] public partial class LocationSegment : SegmentBase { - private static readonly JsonSerializerOptions Options = new() { Converters = { new AutosizeConverter() }, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; + private static readonly JsonSerializerOptions Options = new() { Converters = { new AutosizeConverter(), new ForwardConverter() }, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; public override void Build(MessageBuilder builder, SegmentBase segment) {