Skip to content

Commit

Permalink
Merge pull request #57 from ilyfairy/master
Browse files Browse the repository at this point in the history
CqImageMsgDataModel.subType 数字转 string 避免异常
  • Loading branch information
SlimeNull authored Aug 18, 2024
2 parents 4be5675 + 35281c4 commit c69acf1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
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

0 comments on commit c69acf1

Please sign in to comment.