Skip to content

Commit

Permalink
[OneBot] Better LightApp parse (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
pk5ls20 authored Oct 31, 2024
1 parent 54def3f commit 0f14c8c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
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

0 comments on commit 0f14c8c

Please sign in to comment.