-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from ilyfairy/master
CqImageMsgDataModel.subType 数字转 string 避免异常
- Loading branch information
Showing
4 changed files
with
38 additions
and
1 deletion.
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
31 changes: 31 additions & 0 deletions
31
src/EleCho.GoCqHttpSdk/JsonConverter/ToStringJsonConverter.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,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
|
||
namespace EleCho.GoCqHttpSdk.JsonConverter | ||
{ | ||
internal class ToStringJsonConverter : JsonConverter<string> | ||
{ | ||
public override unsafe string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
return reader.TokenType switch | ||
{ | ||
JsonTokenType.String => reader.GetString(), | ||
JsonTokenType.Number => Encoding.UTF8.GetString((byte*)Unsafe.AsPointer(ref Unsafe.AsRef(in reader.ValueSpan[0])), reader.ValueSpan.Length), | ||
JsonTokenType.True => "true", | ||
JsonTokenType.False => "false", | ||
_ => throw new NotSupportedException(), | ||
}; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value); | ||
} | ||
} | ||
} |
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