-
Notifications
You must be signed in to change notification settings - Fork 865
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 增加 FreeSql.Extensions.JsonMap 扩展包,实现快速将对象映射为json字符串的方法; - 优化 表达式解析未实现的错误提醒,如 $"";
- Loading branch information
28810
authored and
28810
committed
Sep 12, 2019
1 parent
8520008
commit 62a095d
Showing
33 changed files
with
249 additions
and
53 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
13 changes: 13 additions & 0 deletions
13
Extensions/FreeSql.Extensions.JsonMap/DataAnnotations/JsonMapAttribute.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,13 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace FreeSql.DataAnnotations | ||
{ | ||
|
||
/// <summary> | ||
/// 当实体类属性为【对象】时,以JSON形式映射存储 | ||
/// </summary> | ||
public class JsonMapAttribute : Attribute | ||
{ | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Extensions/FreeSql.Extensions.JsonMap/FreeSql.Extensions.JsonMap.csproj
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,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks> | ||
<Version>0.9.15</Version> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Authors>YeXiangQin</Authors> | ||
<Description>FreeSql 扩展包,可实现实体类属性为对象时,以JSON形式映射存储.</Description> | ||
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/2881099/FreeSql</RepositoryUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<PackageTags>FreeSql;ORM</PackageTags> | ||
<PackageId>$(AssemblyName)</PackageId> | ||
<PackageIconUrl>https://github.com/2881099/FreeSql/blob/master/logo.png</PackageIconUrl> | ||
<Title>$(AssemblyName)</Title> | ||
<IsPackable>true</IsPackable> | ||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,58 @@ | ||
using FreeSql.DataAnnotations; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace FreeSql.Extensions | ||
{ | ||
public static class JsonMapCore | ||
{ | ||
static bool _isAoped = false; | ||
static object _isAopedLock = new object(); | ||
static ConcurrentDictionary<Type, bool> _dicTypes = new ConcurrentDictionary<Type, bool>(); | ||
static MethodInfo MethodJsonConvertDeserializeObject = typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string), typeof(Type) }); | ||
static MethodInfo MethodJsonConvertSerializeObject = typeof(JsonConvert).GetMethod("SerializeObject", new[] { typeof(object) }); | ||
|
||
/// <summary> | ||
/// 当实体类属性为【对象】时,并且标记特性 [JsonMap] 时,该属性将以JSON形式映射存储 | ||
/// </summary> | ||
/// <returns></returns> | ||
public static void UseJsonMap(this IFreeSql that) | ||
{ | ||
if (_isAoped == false) | ||
lock(_isAopedLock) | ||
if (_isAoped == false) | ||
{ | ||
_isAoped = true; | ||
|
||
FreeSql.Internal.Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add((LabelTarget returnTarget, Expression valueExp, Type type) => | ||
{ | ||
if (_dicTypes.ContainsKey(type)) return Expression.Return(returnTarget, Expression.TypeAs(Expression.Call(MethodJsonConvertDeserializeObject, Expression.Convert(valueExp, typeof(string)), Expression.Constant(type)), type)); | ||
return null; | ||
}); | ||
|
||
that.Aop.ConfigEntityProperty += new EventHandler<Aop.ConfigEntityPropertyEventArgs>((s, e) => | ||
{ | ||
if (e.Property.GetCustomAttribute<JsonMapAttribute>(false) != null) | ||
{ | ||
e.ModifyResult.MapType = typeof(string); | ||
if (_dicTypes.TryAdd(e.Property.PropertyType, true)) | ||
{ | ||
FreeSql.Internal.Utils.GetDataReaderValueBlockExpressionObjectToStringIfThenElse.Add((LabelTarget returnTarget, Expression valueExp, Expression elseExp, Type type) => | ||
{ | ||
return Expression.IfThenElse( | ||
Expression.TypeEqual(valueExp, e.Property.PropertyType), | ||
Expression.Return(returnTarget, Expression.Call(MethodJsonConvertSerializeObject, Expression.Convert(valueExp, typeof(object))), typeof(object)), | ||
elseExp); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} |
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,23 @@ | ||
FreeSql 扩展包,将值对象映射成 typeof(string),安装扩展包: | ||
|
||
> dotnet add package FreeSql.Extensions.JsonMap | ||
```csharp | ||
fsql.UseJsonMap(); //开启功能 | ||
class TestConfig | ||
{ | ||
public int clicks { get; set; } | ||
public string title { get; set; } | ||
} | ||
|
||
[Table(Name = "sysconfig")] | ||
public class S_SysConfig<T> | ||
{ | ||
[Column(IsPrimary = true)] | ||
public string Name { get; set; } | ||
|
||
[JsonMap] | ||
public T Config { get; set; } | ||
} | ||
``` |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.