Files
2021-02-20 16:45:44 +08:00

64 lines
2.0 KiB
C#

using MPIgnore = MessagePack.IgnoreMemberAttribute;
using MPKey = MessagePack.KeyAttribute;
using MPObject = MessagePack.MessagePackObjectAttribute;
using NJSONIgnore = Newtonsoft.Json.JsonIgnoreAttribute;
using NJsonProperty = Newtonsoft.Json.JsonPropertyAttribute;
using SJSONIgnore = System.Text.Json.Serialization.JsonIgnoreAttribute;
using SJsonProperty = System.Text.Json.Serialization.JsonPropertyNameAttribute;
namespace System.Application.Models
{
internal abstract class ApiResponseImplBase : Serializable, IApiResponse
{
ApiResponseCode mCode;
bool mIsSuccess;
[MPKey(0)]
[NJsonProperty("🦄")]
[SJsonProperty("🦄")]
public ApiResponseCode Code
{
get => mCode;
set
{
mCode = value;
// https://github.com/dotnet/corefx/blob/v3.1.6/src/System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs#L143
var code = (int)mCode;
mIsSuccess = code >= 200 && code <= 299;
}
}
[MPKey(LastMKeyIndex)]
[NJsonProperty("🐴")]
[SJsonProperty("🐴")]
public string? Message { get; set; }
/// <summary>
/// 最后一个 MessagePack 序列化 下标,继承自此类,新增需要序列化的字段/属性,标记此值+1,+2
/// </summary>
protected const int LastMKeyIndex = 1;
[MPIgnore]
[SJSONIgnore]
[NJSONIgnore]
public bool IsSuccess => mIsSuccess;
}
[MPObject]
internal class ApiResponseImpl<T> : ApiResponseImplBase, IApiResponse<T>
{
[MPKey(LastMKeyIndex + 1)]
[NJsonProperty("🦓")]
[SJsonProperty("🦓")]
public T? Content { get; set; }
}
[MPObject]
internal class ApiResponseImpl : ApiResponseImplBase, IApiResponse<object>
{
[MPIgnore]
[SJSONIgnore]
[NJSONIgnore]
public object? Content => null;
}
}