From 545bb8190015cc5622b6a93ceb5cb868d09638fa Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 10 May 2022 13:15:21 +0800 Subject: [PATCH] Update query json --- .../ResolveItems/BaseResolve.cs | 9 +++ .../SqlSugar/Infrastructure/ContextMethods.cs | 58 ++++++++++++------- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs index 24775b0a1..b9a49d0ff 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs @@ -472,6 +472,15 @@ namespace SqlSugar { asName = GetAsName(item, shortName, property); } + else if (comumnInfo!=null&&this.Context.SugarContext != null&&this.Context.SugarContext.Context != null) + { + var entityInfo=this.Context.SugarContext.Context.EntityMaintenance.GetEntityInfo(item.Type); + var entityColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyName == property.Name); + if (entityColumn != null && entityColumn.IsJson) + { + asName = GetAsName(item, shortName, property); + } + } } else if (isSameType) { diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs index 93a1a0d55..c794073a9 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs @@ -394,6 +394,7 @@ namespace SqlSugar return null; } var classProperties = type.GetProperties().ToList(); + var columns = this.Context.EntityMaintenance.GetEntityInfo(type).Columns; foreach (var prop in classProperties) { var name = prop.Name; @@ -403,29 +404,14 @@ namespace SqlSugar var suagrColumn=prop.GetCustomAttribute(); if (suagrColumn != null && suagrColumn.IsJson) { - var key = (typeName + "." + name).ToLower(); - if (readerValues.Any(it=>it.Key.EqualCase(key))) - { - var jsonString = readerValues.First(it => it.Key.EqualCase(key)).Value; - if (jsonString != null) - { - if (jsonString.ToString().First() == '{' && jsonString.ToString().Last() == '}') - { - result.Add(name, this.DeserializeObject>(jsonString + "")); - } - else if (jsonString.ToString().Replace(" ","")!="[]"&&!jsonString.ToString().Contains("{")&&!jsonString.ToString().Contains("}")) - { - result.Add(name, this.DeserializeObject(jsonString + "")); - } - else - { - result.Add(name, this.DeserializeObject>>(jsonString + "")); - - } - } - } - else + Json(readerValues, result, name, typeName); + } + else if (columns.Any(it => it.IsJson)) + { + var column = columns.FirstOrDefault(it => it.PropertyName == name); + if (column != null && column.IsJson) { + Json(readerValues, result, name, typeName); } } else @@ -462,6 +448,34 @@ namespace SqlSugar } return result; } + + private void Json(Dictionary readerValues, Dictionary result, string name, string typeName) + { + var key = (typeName + "." + name).ToLower(); + if (readerValues.Any(it => it.Key.EqualCase(key))) + { + var jsonString = readerValues.First(it => it.Key.EqualCase(key)).Value; + if (jsonString != null) + { + if (jsonString.ToString().First() == '{' && jsonString.ToString().Last() == '}') + { + result.Add(name, this.DeserializeObject>(jsonString + "")); + } + else if (jsonString.ToString().Replace(" ", "") != "[]" && !jsonString.ToString().Contains("{") && !jsonString.ToString().Contains("}")) + { + result.Add(name, this.DeserializeObject(jsonString + "")); + } + else + { + result.Add(name, this.DeserializeObject>>(jsonString + "")); + + } + } + } + else + { + } + } #endregion #region Serialize