-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
67 changed files
with
784 additions
and
539 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
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
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
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
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
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,38 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using System.Linq.Expressions; | ||
|
||
namespace Snap.Hutao.Core.Convert; | ||
|
||
/// <summary> | ||
/// Class to cast to type <see cref="TTo"/> | ||
/// </summary> | ||
/// <typeparam name="TTo">Target type</typeparam> | ||
public static class CastTo<TTo> | ||
{ | ||
/// <summary> | ||
/// Casts <see cref="s"/> to <see cref="TTo"/>. | ||
/// This does not cause boxing for value types. | ||
/// Useful in generic methods. | ||
/// </summary> | ||
/// <typeparam name="TFrom">Source type to cast from. Usually a generic type.</typeparam> | ||
/// <param name="from">from value</param> | ||
/// <returns>target value</returns> | ||
public static TTo From<TFrom>(TFrom from) | ||
{ | ||
return Cache<TFrom>.Caster(from); | ||
} | ||
|
||
private static class Cache<TCachedFrom> | ||
{ | ||
public static readonly Func<TCachedFrom, TTo> Caster = Get(); | ||
|
||
private static Func<TCachedFrom, TTo> Get() | ||
{ | ||
ParameterExpression param = Expression.Parameter(typeof(TCachedFrom)); | ||
UnaryExpression convert = Expression.ConvertChecked(param, typeof(TTo)); | ||
return Expression.Lambda<Func<TCachedFrom, TTo>>(convert, param).Compile(); | ||
} | ||
} | ||
} |
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
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
32 changes: 32 additions & 0 deletions
32
src/Snap.Hutao/Snap.Hutao/Core/Json/Annotation/JsonEnumAttribute.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,32 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
namespace Snap.Hutao.Core.Json.Annotation; | ||
|
||
/// <summary> | ||
/// Json 枚举类型 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] | ||
internal class JsonEnumAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// 构造一个新的Json枚举声明 | ||
/// </summary> | ||
/// <param name="readAs">读取</param> | ||
/// <param name="writeAs">写入</param> | ||
public JsonEnumAttribute(JsonSerializeType readAs, JsonSerializeType writeAs) | ||
{ | ||
ReadAs = readAs; | ||
WriteAs = writeAs; | ||
} | ||
|
||
/// <summary> | ||
/// 读取形式 | ||
/// </summary> | ||
public JsonSerializeType ReadAs { get; init; } | ||
|
||
/// <summary> | ||
/// 写入形式 | ||
/// </summary> | ||
public JsonSerializeType WriteAs { get; init; } | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Snap.Hutao/Snap.Hutao/Core/Json/Annotation/JsonSerializeType.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,25 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
namespace Snap.Hutao.Core.Json.Annotation; | ||
|
||
/// <summary> | ||
/// Json 文本字符串序列化类型 | ||
/// </summary> | ||
public enum JsonSerializeType | ||
{ | ||
/// <summary> | ||
/// 数字 | ||
/// </summary> | ||
Int32, | ||
|
||
/// <summary> | ||
/// 字符串包裹的数字 | ||
/// </summary> | ||
Int32AsString, | ||
|
||
/// <summary> | ||
/// 名称字符串 | ||
/// </summary> | ||
String, | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Snap.Hutao/Snap.Hutao/Core/Json/Converter/ConfigurableEnumConverter{TEnum}.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,62 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using Snap.Hutao.Core.Convert; | ||
using Snap.Hutao.Core.Json.Annotation; | ||
|
||
namespace Snap.Hutao.Core.Json.Converter; | ||
|
||
/// <summary> | ||
/// 枚举转换器 | ||
/// </summary> | ||
/// <typeparam name="TEnum">枚举的类型</typeparam> | ||
internal class ConfigurableEnumConverter<TEnum> : JsonConverter<TEnum> | ||
where TEnum : struct, Enum | ||
{ | ||
private readonly JsonSerializeType readAs; | ||
private readonly JsonSerializeType writeAs; | ||
|
||
/// <summary> | ||
/// 构造一个新的枚举转换器 | ||
/// </summary> | ||
/// <param name="readAs">读取</param> | ||
/// <param name="writeAs">写入</param> | ||
public ConfigurableEnumConverter(JsonSerializeType readAs, JsonSerializeType writeAs) | ||
{ | ||
this.readAs = readAs; | ||
this.writeAs = writeAs; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (readAs == JsonSerializeType.Int32) | ||
{ | ||
return CastTo<TEnum>.From(reader.GetInt32()); | ||
} | ||
|
||
if (reader.GetString() is string str) | ||
{ | ||
return Enum.Parse<TEnum>(str); | ||
} | ||
|
||
throw Must.NeverHappen(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) | ||
{ | ||
switch (writeAs) | ||
{ | ||
case JsonSerializeType.Int32: | ||
writer.WriteNumberValue(CastTo<int>.From(value)); | ||
break; | ||
case JsonSerializeType.Int32AsString: | ||
writer.WriteStringValue(value.ToString("D")); | ||
break; | ||
default: | ||
writer.WriteStringValue(value.ToString()); | ||
break; | ||
} | ||
} | ||
} |
Oops, something went wrong.