Json扩展支持JsonSerializerSettings

This commit is contained in:
Eternity 2020-03-21 02:56:27 +08:00
parent 2f32e0e71c
commit d8f49808a8
4 changed files with 55 additions and 16 deletions

View File

@ -18,6 +18,8 @@ namespace base_entity
{ {
public int clicks { get; set; } public int clicks { get; set; }
public string title { get; set; } public string title { get; set; }
public string nullvalue { get; set; }
} }
[Table(Name = "sysconfig")] [Table(Name = "sysconfig")]
public class S_SysConfig<T> : BaseEntity<S_SysConfig<T>> public class S_SysConfig<T> : BaseEntity<S_SysConfig<T>>
@ -43,7 +45,7 @@ namespace base_entity
.UseNoneCommandParameter(true) .UseNoneCommandParameter(true)
.UseConnectionString(FreeSql.DataType.Sqlite, "data source=test.db;max pool size=5") .UseConnectionString(FreeSql.DataType.Sqlite, "data source=test.db;max pool size=5")
//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=2") //.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=2")
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3") //.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3")
.UseLazyLoading(true) .UseLazyLoading(true)
.Build(); .Build();
BaseEntity.Initialization(fsql); BaseEntity.Initialization(fsql);
@ -59,10 +61,12 @@ namespace base_entity
var items1 = Products.Select.Limit(10).OrderByDescending(a => a.CreateTime).ToList(); var items1 = Products.Select.Limit(10).OrderByDescending(a => a.CreateTime).ToList();
var items2 = fsql.Select<Products>().Limit(10).OrderByDescending(a => a.CreateTime).ToList(); var items2 = fsql.Select<Products>().Limit(10).OrderByDescending(a => a.CreateTime).ToList();
#if NETCORE30
BaseEntity.Orm.UseJsonMap(); BaseEntity.Orm.UseJsonMap(new System.Text.Json.JsonSerializerOptions { IgnoreNullValues = true });
#else
new S_SysConfig<TestConfig> { Name = "testkey11", Config = new TestConfig { clicks = 11, title = "testtitle11" } }.Save(); BaseEntity.Orm.UseJsonMap(new JsonSerializerSettings { NullValueHandling=NullValueHandling.Ignore });
#endif
new S_SysConfig<TestConfig> { Name = "testkey11", Config = new TestConfig { clicks = 11, title = "testtitle11", nullvalue = null } }.Save();
new S_SysConfig<TestConfig> { Name = "testkey22", Config = new TestConfig { clicks = 22, title = "testtitle22" } }.Save(); new S_SysConfig<TestConfig> { Name = "testkey22", Config = new TestConfig { clicks = 22, title = "testtitle22" } }.Save();
new S_SysConfig<TestConfig> { Name = "testkey33", Config = new TestConfig { clicks = 33, title = "testtitle33" } }.Save(); new S_SysConfig<TestConfig> { Name = "testkey33", Config = new TestConfig { clicks = 33, title = "testtitle33" } }.Save();
var testconfigs11 = S_SysConfig<TestConfig>.Select.ToList(); var testconfigs11 = S_SysConfig<TestConfig>.Select.ToList();

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFrameworks>netcoreapp2.1;netcoreapp3.0</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@ -13,7 +13,9 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup> </ItemGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.0' ">
<DefineConstants>NETCORE30</DefineConstants>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.BaseEntity\FreeSql.Extensions.BaseEntity.csproj" /> <ProjectReference Include="..\..\Extensions\FreeSql.Extensions.BaseEntity\FreeSql.Extensions.BaseEntity.csproj" />
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.JsonMap\FreeSql.Extensions.JsonMap.csproj" /> <ProjectReference Include="..\..\Extensions\FreeSql.Extensions.JsonMap\FreeSql.Extensions.JsonMap.csproj" />

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks> <TargetFrameworks>netstandard2.0;net45;net40;netcoreapp3.0</TargetFrameworks>
<Version>1.3.0-preview10</Version> <Version>1.3.0-preview10</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors> <Authors>YeXiangQin</Authors>
@ -29,8 +29,11 @@
<DocumentationFile>FreeSql.Extensions.JsonMap.xml</DocumentationFile> <DocumentationFile>FreeSql.Extensions.JsonMap.xml</DocumentationFile>
<WarningLevel>3</WarningLevel> <WarningLevel>3</WarningLevel>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.0' ">
<DefineConstants>NETCORE30</DefineConstants>
</PropertyGroup>
<ItemGroup> <ItemGroup Condition=" '$(TargetFramework)' != 'netcoreapp3.0' ">
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup> </ItemGroup>

View File

@ -6,7 +6,9 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
#if NETCORE30
using System.Text.Json;
#endif
namespace FreeSql.Extensions namespace FreeSql.Extensions
{ {
public static class JsonMapCore public static class JsonMapCore
@ -14,14 +16,35 @@ namespace FreeSql.Extensions
static bool _isAoped = false; static bool _isAoped = false;
static object _isAopedLock = new object(); static object _isAopedLock = new object();
static ConcurrentDictionary<Type, bool> _dicTypes = new ConcurrentDictionary<Type, bool>(); static ConcurrentDictionary<Type, bool> _dicTypes = new ConcurrentDictionary<Type, bool>();
static MethodInfo MethodJsonConvertDeserializeObject = typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string), typeof(Type) }); #if NETCORE30
static MethodInfo MethodJsonConvertSerializeObject = typeof(JsonConvert).GetMethod("SerializeObject", new[] { typeof(object) }); static MethodInfo MethodJsonConvertDeserializeObject = typeof(JsonSerializer).GetMethod(nameof(JsonSerializer.Deserialize), new[] { typeof(string), typeof(Type) });
static MethodInfo MethodJsonConvertSerializeObject = typeof(JsonSerializer).GetMethod(nameof(JsonSerializer.Serialize), new[] { typeof(object), typeof(Type), typeof(JsonSerializerOptions) });
#else
static MethodInfo MethodJsonConvertDeserializeObject = typeof(JsonConvert).GetMethod(nameof(JsonConvert.DeserializeObject), new[] { typeof(string), typeof(Type) });
static MethodInfo MethodJsonConvertSerializeObject = typeof(JsonConvert).GetMethod(nameof(JsonConvert.SerializeObject), new[] { typeof(object), typeof(JsonSerializerSettings) });
#endif
/// <summary> /// <summary>
/// 当实体类属性为【对象】时,并且标记特性 [JsonMap] 时该属性将以JSON形式映射存储 /// 当实体类属性为【对象】时,并且标记特性 [JsonMap] 时该属性将以JSON形式映射存储
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static void UseJsonMap(this IFreeSql that) public static void UseJsonMap(this IFreeSql that)
{
UseJsonMap(that,
#if NETCORE30
new JsonSerializerOptions()
#else
new JsonSerializerSettings()
#endif
);}
#if NETCORE30
public static void UseJsonMap(this IFreeSql that, JsonSerializerOptions settings)
#else
public static void UseJsonMap(this IFreeSql that, JsonSerializerSettings settings)
#endif
{ {
if (_isAoped == false) if (_isAoped == false)
lock (_isAopedLock) lock (_isAopedLock)
@ -46,7 +69,13 @@ namespace FreeSql.Extensions
{ {
return Expression.IfThenElse( return Expression.IfThenElse(
Expression.TypeEqual(valueExp, e.Property.PropertyType), Expression.TypeEqual(valueExp, e.Property.PropertyType),
Expression.Return(returnTarget, Expression.Call(MethodJsonConvertSerializeObject, Expression.Convert(valueExp, typeof(object))), typeof(object)), Expression.Return(returnTarget, Expression.Call(MethodJsonConvertSerializeObject,
#if NETCORE30
Expression.Convert(valueExp, typeof(object)), Expression.Constant(valueExp.Type), Expression.Constant(settings))
#else
Expression.Convert(valueExp, typeof(object)), Expression.Constant(settings))
#endif
, typeof(object)),
elseExp); elseExp);
}); });
} }
@ -54,5 +83,6 @@ namespace FreeSql.Extensions
}); });
} }
} }
} }
} }