diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs index 0712e96d..09543005 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs @@ -40,6 +40,9 @@ namespace Yi.Framework.SqlSugarCore public IEntityChangeEventHelper EntityChangeEventHelper => LazyServiceProvider.LazyGetService(NullEntityChangeEventHelper.Instance); public DbConnOptions Options => LazyServiceProvider.LazyGetRequiredService>().Value; public AbpDbConnectionOptions ConnectionOptions => LazyServiceProvider.LazyGetRequiredService>().Value; + + public ISerializeService SerializeService=> LazyServiceProvider.LazyGetRequiredService(); + private ISqlSugarDbConnectionCreator _dbConnectionCreator; public void SetSqlSugarClient(ISqlSugarClient sqlSugarClient) @@ -63,6 +66,8 @@ namespace Yi.Framework.SqlSugarCore options.DbType = GetCurrentDbType(); })); connectionCreator.SetDbAop(SqlSugarClient); + //替换默认序列化器 + SqlSugarClient.CurrentConnectionConfig.ConfigureExternalServices.SerializeService = SerializeService; } /// diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarNonPublicSerializer.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarNonPublicSerializer.cs new file mode 100644 index 00000000..74a1bee5 --- /dev/null +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarNonPublicSerializer.cs @@ -0,0 +1,76 @@ +using System.Reflection; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using SqlSugar; + +namespace Yi.Framework.SqlSugarCore; + +public class NonPublicPropertiesResolver : DefaultContractResolver +{ + /// + /// 重写获取属性,存在get set方法就可以写入 + /// + /// + /// + /// + protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) + { + var prop = base.CreateProperty(member, memberSerialization); + if (member is PropertyInfo pi) + { + prop.Readable = (pi.GetMethod != null); + prop.Writable = (pi.SetMethod != null); + } + + return prop; + } +} + +public class SqlSugarNonPublicSerializer : ISerializeService +{ + /// + /// 默认的序列化服务 + /// + private readonly ISerializeService _serializeService = DefaultServices.Serialize; + + public string SerializeObject(object value) + { + //保留原有实现 + return _serializeService.SerializeObject(value); + } + + public string SugarSerializeObject(object value) + { //保留原有实现 + return _serializeService.SugarSerializeObject(value); + } + + /// + /// 重写对象反序列化支持NoPublic访问器 + /// + /// + /// + /// + public T DeserializeObject(string value) + { + if (typeof(T).FullName.StartsWith("System.Text.Json.")) + { + // 动态创建一个 JsonSerializer 实例 + Type serializerType =typeof(T).Assembly.GetType("System.Text.Json.JsonSerializer"); + + var methods = serializerType + .GetMethods().Where(it=>it.Name== "Deserialize") + .Where(it=>it.GetParameters().Any(z=>z.ParameterType==typeof(string))).First(); + + // 调用 SerializeObject 方法序列化对象 + T json = (T)methods.MakeGenericMethod(typeof(T)) + .Invoke(null, new object[] { value, null }); + return json; + } + var jSetting = new JsonSerializerSettings + { + NullValueHandling = NullValueHandling.Ignore, + ContractResolver =new NonPublicPropertiesResolver() //替换默认解析器使能支持protect + }; + return JsonConvert.DeserializeObject(value, jSetting); + } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs index 9718f18b..9f8aa952 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs @@ -39,8 +39,8 @@ namespace Yi.Framework.SqlSugarCore service.AddTransient(typeof(ISqlSugarRepository<,>), typeof(SqlSugarRepository<,>)); service.AddTransient(typeof(ISugarDbContextProvider<>), typeof(UnitOfWorkSqlsugarDbContextProvider<>)); - - + //替换Sqlsugar默认序列化器,用来解决.Select()不支持嵌套对象/匿名对象的非公有访问器 值无法绑定,如Id属性 + context.Services.AddSingleton (); return Task.CompletedTask; }