Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OneBot] Better LightApp parse #666

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lagrange.OneBot/Core/Entity/Common/LightApp.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Meta>))] public Meta Meta { get; set; }

[JsonPropertyName("extra")] public Extra? Extra { get; set; }
[JsonPropertyName("extra")] [JsonConverter(typeof(NullIfEmptyConverter<Extra>))] public Extra? Extra { get; set; }

[JsonPropertyName("prompt")] public string Prompt { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};
Expand Down
22 changes: 22 additions & 0 deletions Lagrange.OneBot/Core/Operation/Converters/ForwardConverter.cs
Original file line number Diff line number Diff line change
@@ -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<int>
{
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);
}
20 changes: 20 additions & 0 deletions Lagrange.OneBot/Core/Operation/Converters/NullIfEmptyConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Lagrange.OneBot.Core.Operation.Converters;

public class NullIfEmptyConverter<T> : JsonConverter<T> 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<T>(ref reader, options),
_ => throw new JsonException()
};
}

public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options) => JsonSerializer.Serialize(writer, value, options);
}
2 changes: 1 addition & 1 deletion Lagrange.OneBot/Message/Entity/LocationSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down