|
|
@@ -1,7 +1,7 @@
|
|
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
|
|
-//
|
|
|
+//
|
|
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
|
|
-//
|
|
|
+//
|
|
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
|
|
|
|
|
using System.Text.Json;
|
|
|
@@ -22,10 +22,10 @@ public class CustomJsonPropertyConverter : JsonConverter<object>
|
|
|
|
|
|
// 缓存类型信息避免重复反射
|
|
|
private static readonly ConcurrentDictionary<Type, IReadOnlyList<PropertyMeta>> PropertyCache = new();
|
|
|
-
|
|
|
+
|
|
|
// 日期时间格式化配置
|
|
|
private readonly string _dateTimeFormat;
|
|
|
-
|
|
|
+
|
|
|
public CustomJsonPropertyConverter(string dateTimeFormat = "yyyy-MM-dd HH:mm:ss")
|
|
|
{
|
|
|
_dateTimeFormat = dateTimeFormat;
|
|
|
@@ -33,7 +33,7 @@ public class CustomJsonPropertyConverter : JsonConverter<object>
|
|
|
|
|
|
public override bool CanConvert(Type typeToConvert)
|
|
|
{
|
|
|
- return PropertyCache.GetOrAdd(typeToConvert, type =>
|
|
|
+ return PropertyCache.GetOrAdd(typeToConvert, type =>
|
|
|
type.GetProperties()
|
|
|
.Where(p => p.GetCustomAttribute<CustomJsonPropertyAttribute>() != null)
|
|
|
.Select(p => new PropertyMeta(p))
|
|
|
@@ -52,7 +52,7 @@ public class CustomJsonPropertyConverter : JsonConverter<object>
|
|
|
if (jsonDoc.RootElement.TryGetProperty(prop.JsonName, out var value))
|
|
|
{
|
|
|
object propertyValue;
|
|
|
-
|
|
|
+
|
|
|
// 特殊处理日期时间类型
|
|
|
if (IsDateTimeType(prop.PropertyType))
|
|
|
{
|
|
|
@@ -61,16 +61,16 @@ public class CustomJsonPropertyConverter : JsonConverter<object>
|
|
|
else
|
|
|
{
|
|
|
propertyValue = JsonSerializer.Deserialize(
|
|
|
- value.GetRawText(),
|
|
|
- prop.PropertyType,
|
|
|
+ value.GetRawText(),
|
|
|
+ prop.PropertyType,
|
|
|
options
|
|
|
);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
prop.SetValue(instance, propertyValue);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return instance;
|
|
|
}
|
|
|
|
|
|
@@ -82,9 +82,9 @@ public class CustomJsonPropertyConverter : JsonConverter<object>
|
|
|
foreach (var prop in properties)
|
|
|
{
|
|
|
var propertyValue = prop.GetValue(value);
|
|
|
-
|
|
|
+
|
|
|
writer.WritePropertyName(prop.JsonName);
|
|
|
-
|
|
|
+
|
|
|
// 特殊处理日期时间类型
|
|
|
if (propertyValue != null && IsDateTimeType(prop.PropertyType))
|
|
|
{
|
|
|
@@ -95,7 +95,7 @@ public class CustomJsonPropertyConverter : JsonConverter<object>
|
|
|
JsonSerializer.Serialize(writer, propertyValue, options);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
writer.WriteEndObject();
|
|
|
}
|
|
|
|
|
|
@@ -112,8 +112,8 @@ public class CustomJsonPropertyConverter : JsonConverter<object>
|
|
|
if (string.IsNullOrEmpty(dateStr)) return null;
|
|
|
|
|
|
var date = DateTime.Parse(dateStr);
|
|
|
- return targetType == typeof(DateTimeOffset)
|
|
|
- ? new DateTimeOffset(date)
|
|
|
+ return targetType == typeof(DateTimeOffset)
|
|
|
+ ? new DateTimeOffset(date)
|
|
|
: (object)date;
|
|
|
}
|
|
|
|
|
|
@@ -138,7 +138,7 @@ public class CustomJsonPropertyConverter : JsonConverter<object>
|
|
|
private readonly PropertyInfo _property;
|
|
|
private readonly Func<object, object> _getter;
|
|
|
private readonly Action<object, object> _setter;
|
|
|
-
|
|
|
+
|
|
|
public string JsonName { get; }
|
|
|
public Type PropertyType => _property.PropertyType;
|
|
|
|
|
|
@@ -146,28 +146,28 @@ public class CustomJsonPropertyConverter : JsonConverter<object>
|
|
|
{
|
|
|
_property = property;
|
|
|
JsonName = property.GetCustomAttribute<CustomJsonPropertyAttribute>()?.Name ?? property.Name;
|
|
|
-
|
|
|
+
|
|
|
// 编译表达式树优化属性访问
|
|
|
var instanceParam = Expression.Parameter(typeof(object), "instance");
|
|
|
var valueParam = Expression.Parameter(typeof(object), "value");
|
|
|
-
|
|
|
+
|
|
|
// Getter
|
|
|
var getterExpr = Expression.Lambda<Func<object, object>>(
|
|
|
Expression.Convert(
|
|
|
Expression.Property(
|
|
|
- Expression.Convert(instanceParam, property.DeclaringType),
|
|
|
+ Expression.Convert(instanceParam, property.DeclaringType),
|
|
|
property),
|
|
|
typeof(object)),
|
|
|
instanceParam);
|
|
|
_getter = getterExpr.Compile();
|
|
|
-
|
|
|
+
|
|
|
// Setter
|
|
|
if (property.CanWrite)
|
|
|
{
|
|
|
var setterExpr = Expression.Lambda<Action<object, object>>(
|
|
|
Expression.Assign(
|
|
|
Expression.Property(
|
|
|
- Expression.Convert(instanceParam, property.DeclaringType),
|
|
|
+ Expression.Convert(instanceParam, property.DeclaringType),
|
|
|
property),
|
|
|
Expression.Convert(valueParam, property.PropertyType)),
|
|
|
instanceParam, valueParam);
|
|
|
@@ -176,7 +176,7 @@ public class CustomJsonPropertyConverter : JsonConverter<object>
|
|
|
}
|
|
|
|
|
|
public object GetValue(object instance) => _getter(instance);
|
|
|
-
|
|
|
+
|
|
|
public void SetValue(object instance, object value)
|
|
|
{
|
|
|
if (_setter != null)
|