mirror of
https://github.com/nsnail/ns-ext.git
synced 2025-04-20 01:22:51 +08:00
迁移newtonsoft json 至 system.text.json
This commit is contained in:
parent
392762f834
commit
97dcf0e49b
@ -1,4 +1,4 @@
|
|||||||
using Newtonsoft.Json.Serialization;
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace NSExt.Extensions;
|
namespace NSExt.Extensions;
|
||||||
|
|
||||||
@ -12,25 +12,10 @@ public static class ObjectExtensions
|
|||||||
/// <returns>json文本</returns>
|
/// <returns>json文本</returns>
|
||||||
public static string Json(this object me, bool format = false)
|
public static string Json(this object me, bool format = false)
|
||||||
{
|
{
|
||||||
return JsonConvert.SerializeObject(me, format ? Formatting.Indented : Formatting.None);
|
return JsonSerializer.Serialize(me,
|
||||||
}
|
new JsonSerializerOptions {
|
||||||
|
WriteIndented = format,
|
||||||
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||||
/// <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
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,24 +2,57 @@
|
|||||||
|
|
||||||
|
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace NSExt.Extensions;
|
namespace NSExt.Extensions;
|
||||||
|
|
||||||
public static class StringExtensions
|
public static class StringExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// MD5 hmac编码
|
/// aes加密
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="me">字符串</param>
|
/// <param name="me">要加密的串</param>
|
||||||
/// <param name="key">密钥</param>
|
/// <param name="key">密钥</param>
|
||||||
/// <param name="e">字符串使用的编码</param>
|
/// <param name="cipherMode">指定要用于加密的块密码模式。</param>
|
||||||
/// <returns>hash摘要的16进制文本形式(无连字符小写)</returns>
|
/// <param name="paddingMode">指定在消息数据块短于加密操作所需的完整字节数时要应用的填充类型。</param>
|
||||||
private static string Md5Hmac(this string me, string key, Encoding e)
|
/// <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));
|
using var aes = System.Security.Cryptography.Aes.Create();
|
||||||
return BitConverter.ToString(md5Hmac.ComputeHash(e.GetBytes(me)))
|
aes.Padding = PaddingMode.PKCS7;
|
||||||
.Replace("-", string.Empty)
|
aes.Mode = CipherMode.ECB;
|
||||||
.ToLower(CultureInfo.CurrentCulture);
|
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>
|
/// <summary>
|
||||||
@ -143,6 +176,17 @@ public static class StringExtensions
|
|||||||
return !decimal.TryParse(me, out var ret) ? def : ret;
|
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>
|
||||||
/// 将字符串转换成枚举对象
|
/// 将字符串转换成枚举对象
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -167,6 +211,16 @@ public static class StringExtensions
|
|||||||
return !System.Enum.TryParse(typeof(T), name, out var ret) ? def : (T)ret;
|
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>
|
/// <summary>
|
||||||
/// 将字符串转为guid
|
/// 将字符串转为guid
|
||||||
@ -248,27 +302,6 @@ public static class StringExtensions
|
|||||||
return int.Parse(me, CultureInfo.CurrentCulture);
|
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>
|
/// <summary>
|
||||||
/// 尝试将字符串转为int32
|
/// 尝试将字符串转为int32
|
||||||
/// </summary>
|
/// </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>
|
||||||
/// 中文姓名打马赛克
|
/// 中文姓名打马赛克
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -408,7 +431,7 @@ public static class StringExtensions
|
|||||||
/// <returns>反序列化后生成的对象</returns>
|
/// <returns>反序列化后生成的对象</returns>
|
||||||
public static T Object<T>(this string me)
|
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>
|
/// <returns>反序列化后生成的对象</returns>
|
||||||
public static object Object(this string me, Type type)
|
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>
|
/// <summary>
|
||||||
/// 截取指定长度的字符串,代替substring
|
/// 截取指定长度的字符串,代替substring
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -516,62 +550,18 @@ public static class StringExtensions
|
|||||||
return Uri.UnescapeDataString(me);
|
return Uri.UnescapeDataString(me);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 蛇形命名
|
/// MD5 hmac编码
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="me"></param>
|
/// <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="key">密钥</param>
|
/// <param name="key">密钥</param>
|
||||||
/// <param name="cipherMode">指定要用于加密的块密码模式。</param>
|
/// <param name="e">字符串使用的编码</param>
|
||||||
/// <param name="paddingMode">指定在消息数据块短于加密操作所需的完整字节数时要应用的填充类型。</param>
|
/// <returns>hash摘要的16进制文本形式(无连字符小写)</returns>
|
||||||
/// <returns></returns>
|
private static string Md5Hmac(this string me, string key, Encoding e)
|
||||||
public static string Aes(this string me,
|
|
||||||
string key,
|
|
||||||
CipherMode cipherMode = CipherMode.ECB,
|
|
||||||
PaddingMode paddingMode = PaddingMode.PKCS7)
|
|
||||||
{
|
{
|
||||||
using var aes = System.Security.Cryptography.Aes.Create();
|
using var md5Hmac = new HMACMD5(e.GetBytes(key));
|
||||||
aes.Padding = PaddingMode.PKCS7;
|
return BitConverter.ToString(md5Hmac.ComputeHash(e.GetBytes(me)))
|
||||||
aes.Mode = CipherMode.ECB;
|
.Replace("-", string.Empty)
|
||||||
aes.Key = key.Hex();
|
.ToLower(CultureInfo.CurrentCulture);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
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;
|
||||||
global using System.Text.RegularExpressions;
|
global using System.Text.RegularExpressions;
|
||||||
global using System.Web;
|
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>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0"/>
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0"/>
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user