-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OneBot] Better LightApp parse (#666)
- Loading branch information
Showing
5 changed files
with
47 additions
and
3 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
22 changes: 22 additions & 0 deletions
22
Lagrange.OneBot/Core/Operation/Converters/ForwardConverter.cs
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,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
20
Lagrange.OneBot/Core/Operation/Converters/NullIfEmptyConverter.cs
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,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); | ||
} |
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