懒得勤快 3 年之前
父节点
当前提交
74ecbc11d0

+ 4 - 3
Masuit.Tools.Abstractions/Systems/CompositeContractResolver.cs

@@ -1,4 +1,5 @@
-using System.Reflection;
+using System.Linq;
+using System.Reflection;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Serialization;
 
@@ -14,8 +15,8 @@ public class CompositeContractResolver : FallbackJsonPropertyResolver
         var property = base.CreateProperty(member, memberSerialization);
         if (property is { Writable: true })
         {
-            var attributes = property.AttributeProvider.GetAttributes(typeof(DeserializeOnlyJsonPropertyAttribute), true);
-            if (attributes is { Count: > 0 })
+            var attributes = property.AttributeProvider.GetAttributes(typeof(DeserializeOnlyJsonPropertyAttribute), true).Union(property.AttributeProvider.GetAttributes(typeof(SerializeIgnoreAttribute), true));
+            if (attributes.Any())
             {
                 property.ShouldSerialize = _ => false;
             }

+ 3 - 2
Masuit.Tools.Abstractions/Systems/DeserializeOnlyContractResolver.cs

@@ -1,6 +1,7 @@
 using Newtonsoft.Json.Serialization;
 using Newtonsoft.Json;
 using System.Reflection;
+using System.Linq;
 
 namespace Masuit.Tools.Systems;
 
@@ -14,8 +15,8 @@ public class DeserializeOnlyContractResolver : DefaultContractResolver
         var property = base.CreateProperty(member, memberSerialization);
         if (property is { Writable: true })
         {
-            var attributes = property.AttributeProvider.GetAttributes(typeof(DeserializeOnlyJsonPropertyAttribute), true);
-            if (attributes is { Count: > 0 })
+            var attributes = property.AttributeProvider.GetAttributes(typeof(DeserializeOnlyJsonPropertyAttribute), true).Union(property.AttributeProvider.GetAttributes(typeof(SerializeIgnoreAttribute), true));
+            if (attributes.Any())
             {
                 property.ShouldSerialize = _ => false;
             }

+ 1 - 1
Masuit.Tools.Abstractions/Systems/DeserializeOnlyJsonPropertyAttribute.cs

@@ -8,4 +8,4 @@ namespace Masuit.Tools.Systems;
 [AttributeUsage(AttributeTargets.Property)]
 public class DeserializeOnlyJsonPropertyAttribute : Attribute
 {
-}
+}

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

@@ -0,0 +1,11 @@
+using System;
+
+namespace Masuit.Tools.Systems;
+
+/// <summary>
+/// 序列化时忽略
+/// </summary>
+[AttributeUsage(AttributeTargets.Property)]
+public class SerializeIgnoreAttribute : Attribute
+{
+}

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

@@ -301,6 +301,9 @@
     <Compile Include="..\Masuit.Tools.Abstractions\Systems\NullObject.cs">
       <Link>Systems\NullObject.cs</Link>
     </Compile>
+    <Compile Include="..\Masuit.Tools.Abstractions\Systems\SerializeIgnoreAttribute.cs">
+      <Link>Systems\SerializeIgnoreAttribute.cs</Link>
+    </Compile>
     <Compile Include="..\Masuit.Tools.Abstractions\Systems\SnowFlake.cs">
       <Link>Systems\SnowFlake.cs</Link>
     </Compile>

+ 3 - 0
Masuit.Tools/Masuit.Tools.csproj

@@ -133,6 +133,9 @@
     <Compile Include="..\Masuit.Tools.Abstractions\Systems\NullObject.cs">
       <Link>Systems\NullObject.cs</Link>
     </Compile>
+    <Compile Include="..\Masuit.Tools.Abstractions\Systems\SerializeIgnoreAttribute.cs">
+      <Link>Systems\SerializeIgnoreAttribute.cs</Link>
+    </Compile>
     <Compile Include="..\Masuit.Tools.Abstractions\Validator\*.*">
       <Link>Validator\%(RecursiveDir)%(FileName)%(Extension)</Link>
     </Compile>

+ 13 - 122
NetCoreTest/Program.cs

@@ -1,136 +1,27 @@
-using Masuit.Tools;
-using Masuit.Tools.Database;
-using Masuit.Tools.Excel;
-using Masuit.Tools.Files;
-using Masuit.Tools.Models;
-using Masuit.Tools.Reflection;
-using Masuit.Tools.Security;
+using System;
+using Masuit.Tools;
+using Masuit.Tools.Systems;
 using Microsoft.AspNetCore;
 using Microsoft.AspNetCore.Hosting;
-using Newtonsoft.Json;
-using OfficeOpenXml;
-using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.IO;
-using System.Linq;
-using Masuit.Tools.Systems;
 using NetCoreTest;
-using LicenseContext = OfficeOpenXml.LicenseContext;
-
-ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
-var pkg = new ExcelPackage();
-pkg.Workbook.Worksheets.Add("Sheet1");
-var sheet = pkg.Workbook.Worksheets["Sheet1"];
-sheet.FillWorksheet(Enumerable.Range(1, 2).Select(i => new
-{
-    序号 = i,
-    图片 = new Dictionary<string, Stream>()
-    {
-        ["https://ldqk.org/1383"] = File.OpenRead(@"D:\images\emotion\16.jpg"),
-        ["https://ldqk.org/1384"] = File.OpenRead(@"D:\images\emotion\16.jpg"),
-        ["https://ldqk.org/1385"] = File.OpenRead(@"D:\images\emotion\16.jpg"),
-    }
-}).ToDataTable("aa"), null, 4, 3);
-sheet.Cells["A1:F1"].Merge = true;
-sheet.Cells["A1"].Value = "title";
-pkg.SaveAs("Y:\\1.xlsx");
-Console.WriteLine("ok");
-
-Console.ReadKey();
-Enumerable.Range(1, 2).Select(i => new
-{
-    序号 = i,
-    图片 = new Dictionary<string, FileStream>()
-    {
-        ["https://ldqk.org/1383"] = File.OpenRead(@"D:\images\emotion\16.jpg")
-    }
-}).ToDataTable("aa").ToExcel().SaveFile(@"Y:\2.xlsx");
-var myClass = new MyClass()
-{
-    MyProperty1 = 1,
-    Id = "mcc",
-    Pid = "mc",
-    Parent = new MyClass()
-    {
-        Id = "mc",
-        Pid = "ccc",
-        Parent = new MyClass()
-        {
-            Id = "ccc"
-        }
-    }
-};
-var allParent = myClass.AllParent().Append(myClass);
-var tree = allParent.ToTreeGeneral(c => c.Id, c => c.Pid);
-Console.WriteLine(tree.ToJsonString(new JsonSerializerSettings()
-{
-    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
-    Formatting = Formatting.Indented
-}));
-var path = myClass.Path(c => c.Id);
-Console.WriteLine(path);
+using Newtonsoft.Json;
 
-myClass.SetProperty(nameof(MyClass.MyProperty1), 1);
-Console.ReadKey();
-var rsaKey = RsaCrypt.GenerateRsaKeys(RsaKeyType.PKCS8, 2048);
-Console.WriteLine(rsaKey.PrivateKey);
-Console.WriteLine(rsaKey.PublicKey);
-var enc = "123456".RSAEncrypt();
-Console.WriteLine(enc);
-Console.Beep();
-var dec = enc.RSADecrypt();
-Console.WriteLine(dec);
+string json1 = "{\"a\":\"aa\"}";
+string json2 = "{\"b\":\"bb\"}";
+string json3 = "{\"MyProperty\":\"mm\"}";
+JsonConvert.DefaultSettings = () => new JsonSerializerSettings() { ContractResolver = new CompositeContractResolver() };
+var m1 = JsonConvert.DeserializeObject<MyClass>(json1);
+var m2 = JsonConvert.DeserializeObject<MyClass>(json2);
+var m3 = JsonConvert.DeserializeObject<MyClass>(json3);
 Console.ReadKey();
 
 //CreateWebHostBuilder(args).Build().Run();
-
 static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
     WebHost.CreateDefaultBuilder(args)
         .UseStartup<Startup>();
 
-public class ClassCmd
+public class MyClass
 {
+    [SerializeIgnore, FallbackJsonProperty(nameof(MyProperty), "a", "b")]
     public string MyProperty { get; set; }
-
-    public int Num { get; set; }
-}
-
-public class ClassDto
-{
-    [DeserializeOnlyJsonProperty]
-    public string MyProperty { get; set; }
-
-    public int Num { get; set; }
-}
-
-public class MyClass : ITree<MyClass>
-{
-    [Description("test")]
-    public string Pid { get; set; }
-
-    public int? MyProperty1 { get; set; }
-
-    /// <summary>
-    /// 名字
-    /// </summary>
-    public string Id { get; set; }
-
-    /// <summary>
-    /// 父节点
-    /// </summary>
-    [JsonIgnore]
-    public virtual MyClass Parent { get; set; }
-
-    /// <summary>
-    /// 子级
-    /// </summary>
-    [JsonIgnore]
-    public ICollection<MyClass> Children { get; set; } = new List<MyClass>();
-
-    /// <summary>
-    /// 名字
-    /// </summary>
-    [DeserializeOnlyJsonProperty]
-    public string Name { get; set; }
 }