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

CqImageMsgDataModel.subType 数字转 string 避免异常 #57

Merged
merged 1 commit into from
Aug 18, 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
3 changes: 2 additions & 1 deletion src/EleCho.GoCqHttpSdk/DataStructure/Model/CqGroupModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma warning disable IDE1006 // Naming Styles


using EleCho.GoCqHttpSdk.Action.Model.ResultData;
using EleCho.GoCqHttpSdk.JsonConverter;
using System.Text.Json.Serialization;

namespace EleCho.GoCqHttpSdk.DataStructure.Model
Expand Down Expand Up @@ -62,6 +62,7 @@ public CqGroupMemberModel(long group_id, long user_id, string nickname, string c
public string area { get; } = string.Empty;
public int join_time { get; }
public int last_sent_time { get; }
[JsonConverter(typeof(ToStringJsonConverter))]
public string level { get; } = string.Empty;
public string role { get; } = string.Empty;
public bool unfriendly { get; }
Expand Down
1 change: 1 addition & 0 deletions src/EleCho.GoCqHttpSdk/EleCho.GoCqHttpSdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageIcon>logo-rect.png</PackageIcon>
<PackageTags>Go-CqHttp;go-cqhttp;gocqhttp;OneBot;SDK</PackageTags>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
31 changes: 31 additions & 0 deletions src/EleCho.GoCqHttpSdk/JsonConverter/ToStringJsonConverter.cs
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma warning disable CS8618

using System.Text.Json.Serialization;
using EleCho.GoCqHttpSdk.JsonConverter;

namespace EleCho.GoCqHttpSdk.Message.DataModel
{
internal record class CqImageMsgDataModel : CqMsgDataModel
Expand All @@ -21,6 +24,7 @@ public CqImageMsgDataModel(string? file, string? type, string? subType, string?

public string? file { get; set; }
public string? type { get; set; }
[JsonConverter(typeof(ToStringJsonConverter))]
public string? subType { get; set; }
public string? url { get; set; }
public int? cache { get; set; }
Expand Down
Loading