mirror of
				https://github.com/nsnail/ns-ext.git
				synced 2025-10-31 23:15:28 +08:00 
			
		
		
		
	迁移newtonsoft json 至 system.text.json
This commit is contained in:
		| @@ -1,4 +1,4 @@ | ||||
| using Newtonsoft.Json.Serialization; | ||||
| using System.Text.Json; | ||||
|  | ||||
| namespace NSExt.Extensions; | ||||
|  | ||||
| @@ -12,25 +12,10 @@ public static class ObjectExtensions | ||||
|     /// <returns>json文本</returns> | ||||
|     public static string Json(this object me, bool format = false) | ||||
|     { | ||||
|         return JsonConvert.SerializeObject(me, format ? Formatting.Indented : Formatting.None); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将一个对象序列化成json文本(小驼峰属性名) | ||||
|     /// </summary> | ||||
|     /// <param name="me">指定对象</param> | ||||
|     /// <param name="format">是否格式化</param> | ||||
|     /// <returns>json文本</returns> | ||||
|     public static string JsonCamelCase(this object me, bool format = false) | ||||
|     { | ||||
|         return JsonConvert.SerializeObject(me, | ||||
|                                            new JsonSerializerSettings { | ||||
|                                                ContractResolver = new CamelCasePropertyNamesContractResolver(), | ||||
|                                                Formatting       = format ? Formatting.Indented : Formatting.None | ||||
|                                            }); | ||||
|         return JsonSerializer.Serialize(me, | ||||
|                                         new JsonSerializerOptions { | ||||
|                                             WriteIndented        = format, | ||||
|                                             PropertyNamingPolicy = JsonNamingPolicy.CamelCase | ||||
|                                         }); | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -2,24 +2,57 @@ | ||||
|  | ||||
|  | ||||
| using System.Security.Cryptography; | ||||
| using System.Text.Json; | ||||
|  | ||||
| namespace NSExt.Extensions; | ||||
|  | ||||
| public static class StringExtensions | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     MD5 hmac编码 | ||||
|     ///     aes加密 | ||||
|     /// </summary> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <param name="me">要加密的串</param> | ||||
|     /// <param name="key">密钥</param> | ||||
|     /// <param name="e">字符串使用的编码</param> | ||||
|     /// <returns>hash摘要的16进制文本形式(无连字符小写)</returns> | ||||
|     private static string Md5Hmac(this string me, string key, Encoding e) | ||||
|     /// <param name="cipherMode">指定要用于加密的块密码模式。</param> | ||||
|     /// <param name="paddingMode">指定在消息数据块短于加密操作所需的完整字节数时要应用的填充类型。</param> | ||||
|     /// <returns></returns> | ||||
|     public static string Aes(this string me, | ||||
|                              string      key, | ||||
|                              CipherMode  cipherMode  = CipherMode.ECB, | ||||
|                              PaddingMode paddingMode = PaddingMode.PKCS7) | ||||
|     { | ||||
|         using var md5Hmac = new HMACMD5(e.GetBytes(key)); | ||||
|         return BitConverter.ToString(md5Hmac.ComputeHash(e.GetBytes(me))) | ||||
|                            .Replace("-", string.Empty) | ||||
|                            .ToLower(CultureInfo.CurrentCulture); | ||||
|         using var aes = System.Security.Cryptography.Aes.Create(); | ||||
|         aes.Padding = PaddingMode.PKCS7; | ||||
|         aes.Mode    = CipherMode.ECB; | ||||
|         aes.Key     = key.Hex(); | ||||
|         using var encryptor = aes.CreateEncryptor(); | ||||
|         var       bytes     = me.Hex(); | ||||
|         var       decrypted = encryptor.TransformFinalBlock(bytes, 0, bytes.Length); | ||||
|         return decrypted.Base64(); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     aes解密 | ||||
|     /// </summary> | ||||
|     /// <param name="me">要加密的串</param> | ||||
|     /// <param name="key">密钥</param> | ||||
|     /// <param name="cipherMode">指定要用于加密的块密码模式。</param> | ||||
|     /// <param name="paddingMode">指定在消息数据块短于加密操作所需的完整字节数时要应用的填充类型。</param> | ||||
|     /// <returns></returns> | ||||
|     public static string AesDe(this string me, | ||||
|                                string      key, | ||||
|                                CipherMode  cipherMode  = CipherMode.ECB, | ||||
|                                PaddingMode paddingMode = PaddingMode.PKCS7) | ||||
|     { | ||||
|         using var aes = System.Security.Cryptography.Aes.Create(); | ||||
|         aes.Padding = PaddingMode.PKCS7; | ||||
|         aes.Mode    = CipherMode.ECB; | ||||
|         aes.Key     = key.Hex(); | ||||
|         using var encryptor = aes.CreateDecryptor(); | ||||
|         var       bytes     = me.Base64De(); | ||||
|         var       decrypted = encryptor.TransformFinalBlock(bytes, 0, bytes.Length); | ||||
|         return decrypted.HexDe(); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
| @@ -143,6 +176,17 @@ public static class StringExtensions | ||||
|         return !decimal.TryParse(me, out var ret) ? def : ret; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     string to double | ||||
|     /// </summary> | ||||
|     /// <param name="me">string</param> | ||||
|     /// <returns>Int32</returns> | ||||
|     public static double Double(this string me) | ||||
|     { | ||||
|         return double.Parse(me, CultureInfo.CurrentCulture); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将字符串转换成枚举对象 | ||||
|     /// </summary> | ||||
| @@ -167,6 +211,16 @@ public static class StringExtensions | ||||
|         return !System.Enum.TryParse(typeof(T), name, out var ret) ? def : (T)ret; | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     string to float | ||||
|     /// </summary> | ||||
|     /// <param name="me">string</param> | ||||
|     /// <returns>Int32</returns> | ||||
|     public static float Float(this string me) | ||||
|     { | ||||
|         return float.Parse(me, CultureInfo.CurrentCulture); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将字符串转为guid | ||||
| @@ -248,27 +302,6 @@ public static class StringExtensions | ||||
|         return int.Parse(me, CultureInfo.CurrentCulture); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     string to double | ||||
|     /// </summary> | ||||
|     /// <param name="me">string</param> | ||||
|     /// <returns>Int32</returns> | ||||
|     public static double Double(this string me) | ||||
|     { | ||||
|         return double.Parse(me, CultureInfo.CurrentCulture); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     string to float | ||||
|     /// </summary> | ||||
|     /// <param name="me">string</param> | ||||
|     /// <returns>Int32</returns> | ||||
|     public static float Float(this string me) | ||||
|     { | ||||
|         return float.Parse(me, CultureInfo.CurrentCulture); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     尝试将字符串转为int32 | ||||
|     /// </summary> | ||||
| @@ -332,16 +365,6 @@ public static class StringExtensions | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     将一个json字符串反序列化成为jObject对象 | ||||
|     /// </summary> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <returns></returns> | ||||
|     public static JObject JObject(this string me) | ||||
|     { | ||||
|         return Newtonsoft.Json.Linq.JObject.Parse(me); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     中文姓名打马赛克 | ||||
|     /// </summary> | ||||
| @@ -408,7 +431,7 @@ public static class StringExtensions | ||||
|     /// <returns>反序列化后生成的对象</returns> | ||||
|     public static T Object<T>(this string me) | ||||
|     { | ||||
|         return JsonConvert.DeserializeObject<T>(me); | ||||
|         return JsonSerializer.Deserialize<T>(me); | ||||
|     } | ||||
|  | ||||
|  | ||||
| @@ -420,7 +443,7 @@ public static class StringExtensions | ||||
|     /// <returns>反序列化后生成的对象</returns> | ||||
|     public static object Object(this string me, Type type) | ||||
|     { | ||||
|         return JsonConvert.DeserializeObject(me, type); | ||||
|         return JsonSerializer.Deserialize(me, type); | ||||
|     } | ||||
|  | ||||
|  | ||||
| @@ -470,6 +493,17 @@ public static class StringExtensions | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     蛇形命名 | ||||
|     /// </summary> | ||||
|     /// <param name="me"></param> | ||||
|     /// <returns></returns> | ||||
|     public static string Snakecase(this string me) | ||||
|     { | ||||
|         return Regex.Replace(me, "([A-Z])", "-$1").ToLower().TrimStart('-'); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     截取指定长度的字符串,代替substring | ||||
|     /// </summary> | ||||
| @@ -516,62 +550,18 @@ public static class StringExtensions | ||||
|         return Uri.UnescapeDataString(me); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     蛇形命名 | ||||
|     ///     MD5 hmac编码 | ||||
|     /// </summary> | ||||
|     /// <param name="me"></param> | ||||
|     /// <returns></returns> | ||||
|     public static string Snakecase(this string me) | ||||
|     { | ||||
|         return Regex.Replace(me, "([A-Z])", "-$1").ToLower().TrimStart('-'); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     aes加密 | ||||
|     /// </summary> | ||||
|     /// <param name="me">要加密的串</param> | ||||
|     /// <param name="me">字符串</param> | ||||
|     /// <param name="key">密钥</param> | ||||
|     /// <param name="cipherMode">指定要用于加密的块密码模式。</param> | ||||
|     /// <param name="paddingMode">指定在消息数据块短于加密操作所需的完整字节数时要应用的填充类型。</param> | ||||
|     /// <returns></returns> | ||||
|     public static string Aes(this string me, | ||||
|                              string      key, | ||||
|                              CipherMode  cipherMode  = CipherMode.ECB, | ||||
|                              PaddingMode paddingMode = PaddingMode.PKCS7) | ||||
|     /// <param name="e">字符串使用的编码</param> | ||||
|     /// <returns>hash摘要的16进制文本形式(无连字符小写)</returns> | ||||
|     private static string Md5Hmac(this string me, string key, Encoding e) | ||||
|     { | ||||
|         using var aes = System.Security.Cryptography.Aes.Create(); | ||||
|         aes.Padding = PaddingMode.PKCS7; | ||||
|         aes.Mode    = CipherMode.ECB; | ||||
|         aes.Key     = key.Hex(); | ||||
|         using var encryptor = aes.CreateEncryptor(); | ||||
|         var       bytes     = me.Hex(); | ||||
|         var       decrypted = encryptor.TransformFinalBlock(bytes, 0, bytes.Length); | ||||
|         return decrypted.Base64(); | ||||
|         using var md5Hmac = new HMACMD5(e.GetBytes(key)); | ||||
|         return BitConverter.ToString(md5Hmac.ComputeHash(e.GetBytes(me))) | ||||
|                            .Replace("-", string.Empty) | ||||
|                            .ToLower(CultureInfo.CurrentCulture); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /// <summary> | ||||
|     ///     aes解密 | ||||
|     /// </summary> | ||||
|     /// <param name="me">要加密的串</param> | ||||
|     /// <param name="key">密钥</param> | ||||
|     /// <param name="cipherMode">指定要用于加密的块密码模式。</param> | ||||
|     /// <param name="paddingMode">指定在消息数据块短于加密操作所需的完整字节数时要应用的填充类型。</param> | ||||
|     /// <returns></returns> | ||||
|     public static string AesDe(this string me, | ||||
|                                string      key, | ||||
|                                CipherMode  cipherMode  = CipherMode.ECB, | ||||
|                                PaddingMode paddingMode = PaddingMode.PKCS7) | ||||
|     { | ||||
|         using var aes = System.Security.Cryptography.Aes.Create(); | ||||
|         aes.Padding = PaddingMode.PKCS7; | ||||
|         aes.Mode    = CipherMode.ECB; | ||||
|         aes.Key     = key.Hex(); | ||||
|         using var encryptor = aes.CreateDecryptor(); | ||||
|         var       bytes     = me.Base64De(); | ||||
|         var       decrypted = encryptor.TransformFinalBlock(bytes, 0, bytes.Length); | ||||
|         return decrypted.HexDe(); | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
							
								
								
									
										19
									
								
								src/NSExt/Extensions/TypeExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/NSExt/Extensions/TypeExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| namespace NSExt.Extensions; | ||||
|  | ||||
| public static class TypeExtensions | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     搜索此成员的继承链以查找自定义属性,接口也会被搜索。 | ||||
|     /// </summary> | ||||
|     /// <param name="me"></param> | ||||
|     /// <typeparam name="T"></typeparam> | ||||
|     /// <returns></returns> | ||||
|     public static IEnumerable<T> GetCustomAttributesIncludingBaseInterfaces<T>(this Type me) | ||||
|     { | ||||
|         var attributeType = typeof(T); | ||||
|         return me.GetCustomAttributes(attributeType, true) | ||||
|                  .Union(me.GetInterfaces() | ||||
|                           .SelectMany(interfaceType => interfaceType.GetCustomAttributes(attributeType, true))) | ||||
|                  .Cast<T>(); | ||||
|     } | ||||
| } | ||||
| @@ -6,6 +6,4 @@ global using System.Globalization; | ||||
| global using System.Text; | ||||
| global using System.Text.RegularExpressions; | ||||
| global using System.Web; | ||||
| global using Newtonsoft.Json; | ||||
| global using Newtonsoft.Json.Linq; | ||||
| global using System.ComponentModel; | ||||
| global using System.ComponentModel; | ||||
| @@ -9,7 +9,6 @@ | ||||
|  | ||||
|     <ItemGroup> | ||||
|         <PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0"/> | ||||
|         <PackageReference Include="Newtonsoft.Json" Version="13.0.1"/> | ||||
|     </ItemGroup> | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user