Browse Source

增加FallbackJsonPropertyResolver和CompositeContractResolver解释器

懒得勤快 3 years ago
parent
commit
9a31538ce0

+ 1 - 1
Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj

@@ -4,7 +4,7 @@
     <LangVersion>latest</LangVersion>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <CodeAnalysisRuleSet />
-    <Version>2.4.7.6</Version>
+    <Version>2.4.7.7</Version>
     <Authors>懒得勤快</Authors>
     <Description>Masuit.Tools基础公共库,包含一些常用的操作类,大都是静态类,加密解密,反射操作,Excel简单导出,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。</Description>
     <Copyright>懒得勤快,长空X</Copyright>

+ 26 - 0
Masuit.Tools.Abstractions/Systems/CompositeContractResolver.cs

@@ -0,0 +1,26 @@
+using System.Reflection;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Serialization;
+
+namespace Masuit.Tools.Systems;
+
+/// <summary>
+/// 支持只允许反序列化属性和多别名属性的解释器
+/// </summary>
+public class CompositeContractResolver : FallbackJsonPropertyResolver
+{
+    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
+    {
+        var property = base.CreateProperty(member, memberSerialization);
+        if (property is { Writable: true })
+        {
+            var attributes = property.AttributeProvider.GetAttributes(typeof(DeserializeOnlyJsonPropertyAttribute), true);
+            if (attributes is { Count: > 0 })
+            {
+                property.ShouldSerialize = _ => false;
+            }
+        }
+
+        return property;
+    }
+}

+ 0 - 9
Masuit.Tools.Abstractions/Systems/DeserializeOnlyContractResolver.cs

@@ -1,18 +1,9 @@
 using Newtonsoft.Json.Serialization;
 using Newtonsoft.Json;
 using System.Reflection;
-using System;
 
 namespace Masuit.Tools.Systems;
 
-/// <summary>
-/// 只允许反序列化
-/// </summary>
-[AttributeUsage(AttributeTargets.Property)]
-public class DeserializeOnlyJsonPropertyAttribute : Attribute
-{
-}
-
 /// <summary>
 /// 只允许反序列化的契约解释器
 /// </summary>

+ 11 - 0
Masuit.Tools.Abstractions/Systems/DeserializeOnlyJsonPropertyAttribute.cs

@@ -0,0 +1,11 @@
+using System;
+
+namespace Masuit.Tools.Systems;
+
+/// <summary>
+/// 只允许反序列化
+/// </summary>
+[AttributeUsage(AttributeTargets.Property)]
+public class DeserializeOnlyJsonPropertyAttribute : Attribute
+{
+}

+ 20 - 0
Masuit.Tools.Abstractions/Systems/FallbackJsonProperty.cs

@@ -0,0 +1,20 @@
+using System;
+
+namespace Masuit.Tools.Systems;
+
+/// <summary>
+/// 多别名属性
+/// </summary>
+[AttributeUsage(AttributeTargets.Property)]
+public class FallbackJsonProperty : Attribute
+{
+    public string PreferredName { get; }
+
+    public string[] FallbackReadNames { get; }
+
+    public FallbackJsonProperty(string preferredName, params string[] fallbackReadNames)
+    {
+        PreferredName = preferredName;
+        FallbackReadNames = fallbackReadNames;
+    }
+}

+ 44 - 0
Masuit.Tools.Abstractions/Systems/FallbackJsonPropertyResolver.cs

@@ -0,0 +1,44 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Serialization;
+
+namespace Masuit.Tools.Systems;
+
+/// <summary>
+/// 多别名属性的解释器
+/// </summary>
+public class FallbackJsonPropertyResolver : CamelCasePropertyNamesContractResolver
+{
+    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
+    {
+        var typeMembers = GetSerializableMembers(type);
+        var properties = new List<JsonProperty>();
+
+        foreach (var member in typeMembers)
+        {
+            var property = CreateProperty(member, memberSerialization);
+            properties.Add(property);
+
+            var fallbackAttribute = member.GetCustomAttribute<FallbackJsonProperty>();
+
+            if (fallbackAttribute == null)
+            {
+                continue;
+            }
+
+            property.PropertyName = fallbackAttribute.PreferredName;
+
+            foreach (var alternateName in fallbackAttribute.FallbackReadNames)
+            {
+                var fallbackProperty = CreateProperty(member, memberSerialization);
+                fallbackProperty.PropertyName = alternateName;
+                fallbackProperty.ShouldSerialize = (x) => false;
+                properties.Add(fallbackProperty);
+            }
+        }
+
+        return properties;
+    }
+}

+ 1 - 1
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -18,7 +18,7 @@ github:https://github.com/ldqk/Masuit.Tools
         <UserSecretsId>830c282f-f7c1-42be-8651-4cd06ac8e73f</UserSecretsId>
         <RepositoryType>Github</RepositoryType>
         <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
-        <Version>2.4.7.6</Version>
+        <Version>2.4.7.7</Version>
         <FileVersion>2.4.5.6</FileVersion>
         <Company>masuit.com</Company>
         <AssemblyVersion>2.4.5.6</AssemblyVersion>

+ 1 - 1
Masuit.Tools.Excel/Masuit.Tools.Excel.csproj

@@ -28,7 +28,7 @@
         <DocumentationFile>.\Masuit.Tools.Excel.xml</DocumentationFile>
     </PropertyGroup>
     <ItemGroup>
-        <PackageReference Include="EPPlus" Version="5.8.6" />
+        <PackageReference Include="EPPlus" Version="5.8.7" />
         <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
         <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
     </ItemGroup>

+ 15 - 0
Masuit.Tools.Net45/Masuit.Tools.Net45.csproj

@@ -262,15 +262,30 @@
     <Compile Include="..\Masuit.Tools.Abstractions\Strings\Template.cs">
       <Link>Strings\Template.cs</Link>
     </Compile>
+    <Compile Include="..\Masuit.Tools.Abstractions\Systems\CompositeContractResolver.cs">
+      <Link>Systems\CompositeContractResolver.cs</Link>
+    </Compile>
     <Compile Include="..\Masuit.Tools.Abstractions\Systems\ConcurrentLimitedQueue.cs">
       <Link>Systems\ConcurrentLimitedQueue.cs</Link>
     </Compile>
+    <Compile Include="..\Masuit.Tools.Abstractions\Systems\DeserializeOnlyContractResolver.cs">
+      <Link>Systems\DeserializeOnlyContractResolver.cs</Link>
+    </Compile>
+    <Compile Include="..\Masuit.Tools.Abstractions\Systems\DeserializeOnlyJsonPropertyAttribute.cs">
+      <Link>Systems\DeserializeOnlyJsonPropertyAttribute.cs</Link>
+    </Compile>
     <Compile Include="..\Masuit.Tools.Abstractions\Systems\Disposable.cs">
       <Link>Systems\Disposable.cs</Link>
     </Compile>
     <Compile Include="..\Masuit.Tools.Abstractions\Systems\EnumExt.cs">
       <Link>Systems\EnumExt.cs</Link>
     </Compile>
+    <Compile Include="..\Masuit.Tools.Abstractions\Systems\FallbackJsonProperty.cs">
+      <Link>Systems\FallbackJsonProperty.cs</Link>
+    </Compile>
+    <Compile Include="..\Masuit.Tools.Abstractions\Systems\FallbackJsonPropertyResolver.cs">
+      <Link>Systems\FallbackJsonPropertyResolver.cs</Link>
+    </Compile>
     <Compile Include="..\Masuit.Tools.Abstractions\Systems\HiPerfTimer.cs">
       <Link>Systems\HiPerfTimer.cs</Link>
     </Compile>

+ 1 - 1
Masuit.Tools.Net45/package.nuspec

@@ -4,7 +4,7 @@
     <!--*-->
     <id>Masuit.Tools.Net45</id>
     <!--*-->
-    <version>2.4.7.6</version>
+    <version>2.4.7.7</version>
     <title>Masuit.Tools</title>
     <!--*-->
     <authors>懒得勤快</authors>

+ 13 - 1
Masuit.Tools/Masuit.Tools.csproj

@@ -112,6 +112,18 @@
     <Compile Include="..\Masuit.Tools.Abstractions\Systems\*.*">
       <Link>Systems\%(RecursiveDir)%(FileName)%(Extension)</Link>
     </Compile>
+    <Compile Include="..\Masuit.Tools.Abstractions\Systems\CompositeContractResolver.cs">
+      <Link>Systems\CompositeContractResolver.cs</Link>
+    </Compile>
+    <Compile Include="..\Masuit.Tools.Abstractions\Systems\DeserializeOnlyJsonPropertyAttribute.cs">
+      <Link>Systems\DeserializeOnlyJsonPropertyAttribute.cs</Link>
+    </Compile>
+    <Compile Include="..\Masuit.Tools.Abstractions\Systems\FallbackJsonProperty.cs">
+      <Link>Systems\FallbackJsonProperty.cs</Link>
+    </Compile>
+    <Compile Include="..\Masuit.Tools.Abstractions\Systems\FallbackJsonPropertyResolver.cs">
+      <Link>Systems\FallbackJsonPropertyResolver.cs</Link>
+    </Compile>
     <Compile Include="..\Masuit.Tools.Abstractions\Systems\NullableConcurrentDictionary.cs">
       <Link>Systems\NullableConcurrentDictionary.cs</Link>
     </Compile>
@@ -187,7 +199,7 @@
       <Version>0.30.1</Version>
     </PackageReference>
     <PackageReference Include="StackExchange.Redis">
-      <Version>2.2.88</Version>
+      <Version>2.5.43</Version>
     </PackageReference>
     <PackageReference Include="System.ValueTuple">
       <Version>4.5.0</Version>

+ 1 - 1
Masuit.Tools/package.nuspec

@@ -4,7 +4,7 @@
     <!--*-->
     <id>Masuit.Tools.Net</id>
     <!--*-->
-    <version>2.4.7.6</version>
+    <version>2.4.7.7</version>
     <title>Masuit.Tools</title>
     <!--*-->
     <authors>懒得勤快</authors>

+ 1 - 1
Test/Masuit.Tools.Test/Masuit.Tools.Test.csproj

@@ -96,7 +96,7 @@
       <Version>1.0.0</Version>
     </PackageReference>
     <PackageReference Include="Moq">
-      <Version>4.17.1</Version>
+      <Version>4.17.2</Version>
     </PackageReference>
     <PackageReference Include="MSTest.TestAdapter">
       <Version>2.2.8</Version>