XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension

## 调整

修复:JsonTransformer的现在可以正常处理结构类 调整:ToJson方法新增一个布尔值参数,指定是否使用格式化的Json转换器 ## 新增 添加:新增标准化的JsonTransformer,旧的JsonTransformer现在迁移至:FormattedJsonTransformer ## 严重 无

635a52a
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

7 个文件 +225 -40
Modified XFEExtension.NetCore.Test/Program.cs +23 -1
@@ -1,4 +1,6 @@
1 using XFEExtension.NetCore.XFEChatGPT;
1 using PDDShopManagementSystem.AdminTool.Model;
2 using XFEExtension.NetCore.StringExtension.Json;
3 using XFEExtension.NetCore.XFEChatGPT;
2 4
3 5 internal class Program
4 6 {
@@ -11,6 +13,26 @@ internal class Program
11 13 //client.DefaultRequestHeaders.Add("Origin", "https://www.piyao.org.cn");
12 14 //client.DefaultRequestHeaders.Add("Referer", "https://www.piyao.org.cn/");
13 15 //QueryableJsonNode jsonNode = @"C:\Users\XFEstudio\Desktop\新建 文本文档.txt".ReadOut()!;
16 var list = new List<UserInfo>();
17 list.Add(new()
18 {
19 ShopID = "cece",
20 RecentShopName = "cececececec",
21 EndDateTime = DateTime.Now,
22 SessionID = Guid.NewGuid().ToString(),
23 CurrentIpAddress = "127.0.0.1",
24 Banned = false
25 });
26 list.Add(new()
27 {
28 ShopID = "cecead11",
29 RecentShopName = "adadwdawd",
30 EndDateTime = DateTime.Now,
31 SessionID = Guid.NewGuid().ToString(),
32 CurrentIpAddress = "127.0.0.1",
33 Banned = false
34 });
35 Console.WriteLine(list.ToJson(true));
14 36 }
15 37
16 38 private static void XFEChatGPT_XFEChatGPTMessageReceived(object? sender, XFEExtension.NetCore.XFEChatGPT.ChatGPTInnerClass.HelperClass.MemorableGPTMessageReceivedEventArgs e)
Added XFEExtension.NetCore.Test/UserInfo.cs +29 -0
@@ -0,0 +1,29 @@
1 namespace PDDShopManagementSystem.AdminTool.Model;
2
3 public class UserInfo
4 {
5 /// <summary>
6 /// 店铺最近登录使用的店铺名称
7 /// </summary>
8 public string? RecentShopName { get; set; }
9 /// <summary>
10 /// 店铺ID
11 /// </summary>
12 public required string ShopID { get; set; }
13 /// <summary>
14 /// 当前活动期ID
15 /// </summary>
16 public required string SessionID { get; set; }
17 /// <summary>
18 /// 最近一次登录的IP地址
19 /// </summary>
20 public required string CurrentIpAddress { get; set; }
21 /// <summary>
22 /// 剩余天数
23 /// </summary>
24 public DateTime EndDateTime { get; set; }
25 /// <summary>
26 /// 该商家店铺是否被封禁
27 /// </summary>
28 public bool Banned { get; set; }
29 }
Modified XFEExtension.NetCore/StringExtension/JsonStringExtension.cs +3 -2
@@ -50,7 +50,8 @@ public static class JsonStringExtension
50 50 /// <summary>
51 51 /// 转化为Json字符串文本
52 52 /// </summary>
53 /// <param name="obj"></param>
53 /// <param name="obj">待转换对象</param>
54 /// <param name="formatted">格式化Json(自动换行和空格对齐)</param>
54 55 /// <returns></returns>
55 public static string ToJson(this object obj) => XFEConverter.GetObjectInfo(StringConverter.JsonTransformer, string.Empty, ObjectPlace.Main, 0, [obj], obj?.GetType(), obj).OutPutObject();
56 public static string ToJson(this object obj, bool formatted = false) => XFEConverter.GetObjectInfo(formatted ? StringConverter.FormattedJsonTransformer : StringConverter.JsonTransformer, string.Empty, ObjectPlace.Main, 0, [obj], obj?.GetType(), obj).OutPutObject();
56 57 }
Modified XFEExtension.NetCore/XFEExtension.NetCore.csproj +4 -4
@@ -5,7 +5,7 @@
5 5 <ImplicitUsings>enable</ImplicitUsings>
6 6 <Nullable>enable</Nullable>
7 7 <GenerateDocumentationFile>True</GenerateDocumentationFile>
8 <Version>3.2.3</Version>
8 <Version>3.2.4</Version>
9 9 <Title>XFEExtension</Title>
10 10 <RepositoryUrl>https://github.com/XFEstudio/XFEExtension</RepositoryUrl>
11 11 <AnalysisLevel>latest</AnalysisLevel>
@@ -21,12 +21,12 @@
21 21 <PackageReleaseNotes>
22 22 ## 调整
23 23
24 修复:现在string扩展方法中的IsNullOrEmpty和IsNullOrWhiteSpace可以正常判断null是否为空
25 修复:JsonTransformer的现在可以正常处理第一级为数组的情况
24 修复:JsonTransformer的现在可以正常处理结构类
25 调整:ToJson方法新增一个布尔值参数,指定是否使用格式化的Json转换器
26 26
27 27 ## 新增
28 28
29
29 添加:新增标准化的JsonTransformer,旧的JsonTransformer现在迁移至:FormattedJsonTransformer
30 30
31 31 ## 严重
32 32
Added XFEExtension.NetCore/XFETransform/StringConverter/FormattedJsonTransformer.cs +146 -0
@@ -0,0 +1,146 @@
1 using System.Collections;
2 using XFEExtension.NetCore.XFETransform.ObjectInfoAnalyzer;
3
4 namespace XFEExtension.NetCore.XFETransform.StringConverter;
5
6 /// <summary>
7 /// 格式化Json转换器
8 /// </summary>
9 public class FormattedJsonTransformer : StringConverter
10 {
11 /// <summary>
12 /// 输出Json对象信息
13 /// </summary>
14 /// <param name="objectInfo"></param>
15 /// <returns></returns>
16 public override string OutPutObject(IObjectInfo objectInfo)
17 {
18 var outPutString = string.Empty;
19 var tabString = string.Empty;
20 for (int k = 0; k < objectInfo.Layer; k++)
21 {
22 tabString += " ";
23 }
24 if (objectInfo.IsBasicType || objectInfo.ObjectPlace == ObjectPlace.Enum)
25 {
26 if (IsEnumerableMember(objectInfo))
27 {
28 if (objectInfo.Value is null)
29 outPutString += $"{tabString}null";
30 else if (objectInfo.Value is string)
31 outPutString += $"{tabString}\"{objectInfo.Value}\"";
32 else if (objectInfo.ObjectPlace == ObjectPlace.Enum)
33 outPutString += $"{tabString}{(int)objectInfo.Value}";
34 else if (objectInfo.Value is bool)
35 outPutString += $"{tabString}{objectInfo.Value.ToString()?.ToLower()}";
36 else
37 outPutString += $"{tabString}\"{objectInfo.Value}\"";
38 }
39 else
40 {
41 if (objectInfo.Value is null)
42 outPutString += $"{tabString}\"{objectInfo.Name}\": null";
43 else if (objectInfo.Value is string)
44 outPutString += $"{tabString}\"{objectInfo.Name}\": \"{objectInfo.Value}\"";
45 else if (objectInfo.ObjectPlace == ObjectPlace.Enum)
46 outPutString += $"{tabString}\"{objectInfo.Name}\": {(int)objectInfo.Value}";
47 else if (objectInfo.Value is bool)
48 outPutString += $"{tabString}\"{objectInfo.Name}\": {objectInfo.Value.ToString()?.ToLower()}";
49 else
50 outPutString += $"{tabString}\"{objectInfo.Name}\": \"{objectInfo.Value}\"";
51 }
52 }
53 else
54 {
55 if (objectInfo.Layer == 0)
56 {
57 if (objectInfo.IsArray)
58 {
59 outPutString += objectInfo.SubObjects is not null ? $$"""
60 [{{OutPutSubObjects(objectInfo.SubObjects)}}
61 ]
62 """ : "null";
63 }
64 else
65 {
66 outPutString += objectInfo.SubObjects is not null ? $$"""
67 {{{OutPutSubObjects(objectInfo.SubObjects)}}
68 }
69 """ : "null";
70 }
71 }
72 else
73 {
74 if (objectInfo.SubObjects is not null)
75 outPutString += OutPutSubObjects(objectInfo.SubObjects);
76 }
77 }
78 return outPutString;
79 }
80 /// <summary>
81 /// 输出子对象的Json信息
82 /// </summary>
83 /// <param name="subObjects"></param>
84 /// <returns></returns>
85 public override string OutPutSubObjects(ISubObjects subObjects)
86 {
87 var outString = string.Empty;
88 for (int i = 0; i < subObjects.Count; i++)
89 {
90 var obj = subObjects[i];
91 var tabString = string.Empty;
92 for (int k = 0; k < obj.Layer; k++)
93 {
94 tabString += " ";
95 }
96 outString += "\n" + tabString;
97 string? currentConnectString;
98 if (i == subObjects.Count - 1)
99 {
100 currentConnectString = "";
101 }
102 else
103 {
104 currentConnectString = ",";
105 }
106 if (obj.IsBasicType || obj.ObjectPlace == ObjectPlace.Enum)
107 {
108 outString += obj.OutPutObject();
109 outString += currentConnectString;
110 }
111 else
112 {
113 if (IsEnumerableClassMember(obj))
114 outString += obj.Value is not null ? $$"""
115 {{tabString}}"{{obj.Name}}": [{{OutPutObject(obj)}}
116 {{tabString}}]{{currentConnectString}}
117 """ : "null";
118 else if (IsEnumerableMember(obj))
119 if (obj.Value is not null && (obj.Type!.IsAssignableTo(typeof(IEnumerable)) || obj.Type.IsAssignableTo(typeof(Array))))
120 outString += obj.Value is not null ? $$"""
121 {{tabString}}[{{OutPutObject(obj)}}
122 {{tabString}}]{{currentConnectString}}
123 """ : "null";
124 else
125 outString += obj.Value is not null ? $$"""
126 {{tabString}}{{{OutPutObject(obj)}}
127 {{tabString}}}{{currentConnectString}}
128 """ : "null";
129 else
130 outString += obj.Value is not null ? $$"""
131 {{tabString}}"{{obj.Name}}": {{{tabString}}{{OutPutObject(obj)}}
132 {{tabString}}}{{currentConnectString}}
133 """ : "null";
134 }
135 }
136 if (outString.Length > 0 && outString[^1] == '\n')
137 {
138 outString = outString[..^1];
139 }
140 return outString;
141 }
142
143 internal static bool IsEnumerable(IObjectInfo objectInfo) => objectInfo.ObjectPlace == ObjectPlace.Array || objectInfo.ObjectPlace == ObjectPlace.List;
144 internal static bool IsEnumerableMember(IObjectInfo objectInfo) => objectInfo.ObjectPlace == ObjectPlace.ArrayMember || objectInfo.ObjectPlace == ObjectPlace.ListMember;
145 internal static bool IsEnumerableClassMember(IObjectInfo objectInfo) => objectInfo.Value is not null && objectInfo.ObjectPlace == ObjectPlace.Property && (objectInfo.Type!.IsAssignableTo(typeof(IEnumerable)) || objectInfo.Type.IsAssignableTo(typeof(Array)));
146 }
Modified XFEExtension.NetCore/XFETransform/StringConverter/JsonTransformer.cs +16 -33
@@ -16,38 +16,33 @@ public class JsonTransformer : StringConverter
16 16 public override string OutPutObject(IObjectInfo objectInfo)
17 17 {
18 18 var outPutString = string.Empty;
19 var tabString = string.Empty;
20 for (int k = 0; k < objectInfo.Layer; k++)
21 {
22 tabString += " ";
23 }
24 19 if (objectInfo.IsBasicType || objectInfo.ObjectPlace == ObjectPlace.Enum)
25 20 {
26 21 if (IsEnumerableMember(objectInfo))
27 22 {
28 23 if (objectInfo.Value is null)
29 outPutString += $"{tabString}null";
24 outPutString += "null";
30 25 else if (objectInfo.Value is string)
31 outPutString += $"{tabString}\"{objectInfo.Value}\"";
26 outPutString += $"\"{objectInfo.Value}\"";
32 27 else if (objectInfo.ObjectPlace == ObjectPlace.Enum)
33 outPutString += $"{tabString}{(int)objectInfo.Value}";
28 outPutString += $"{(int)objectInfo.Value}";
34 29 else if (objectInfo.Value is bool)
35 outPutString += $"{tabString}{objectInfo.Value.ToString()?.ToLower()}";
30 outPutString += $"{objectInfo.Value.ToString()?.ToLower()}";
36 31 else
37 outPutString += $"{tabString}{objectInfo.Value}";
32 outPutString += $"\"{objectInfo.Value}\"";
38 33 }
39 34 else
40 35 {
41 36 if (objectInfo.Value is null)
42 outPutString += $"{tabString}\"{objectInfo.Name}\": null";
37 outPutString += $"\"{objectInfo.Name}\":null";
43 38 else if (objectInfo.Value is string)
44 outPutString += $"{tabString}\"{objectInfo.Name}\": \"{objectInfo.Value}\"";
39 outPutString += $"\"{objectInfo.Name}\":\"{objectInfo.Value}\"";
45 40 else if (objectInfo.ObjectPlace == ObjectPlace.Enum)
46 outPutString += $"{tabString}\"{objectInfo.Name}\": {(int)objectInfo.Value}";
41 outPutString += $"\"{objectInfo.Name}\":{(int)objectInfo.Value}";
47 42 else if (objectInfo.Value is bool)
48 outPutString += $"{tabString}\"{objectInfo.Name}\": {objectInfo.Value.ToString()?.ToLower()}";
43 outPutString += $"\"{objectInfo.Name}\":{objectInfo.Value.ToString()?.ToLower()}";
49 44 else
50 outPutString += $"{tabString}\"{objectInfo.Name}\": {objectInfo.Value}";
45 outPutString += $"\"{objectInfo.Name}\":\"{objectInfo.Value}\"";
51 46 }
52 47 }
53 48 else
@@ -57,15 +52,13 @@ public class JsonTransformer : StringConverter
57 52 if (objectInfo.IsArray)
58 53 {
59 54 outPutString += objectInfo.SubObjects is not null ? $$"""
60 [{{OutPutSubObjects(objectInfo.SubObjects)}}
61 ]
55 [{{OutPutSubObjects(objectInfo.SubObjects)}}]
62 56 """ : "null";
63 57 }
64 58 else
65 59 {
66 60 outPutString += objectInfo.SubObjects is not null ? $$"""
67 {{{OutPutSubObjects(objectInfo.SubObjects)}}
68 }
61 {{{OutPutSubObjects(objectInfo.SubObjects)}}}
69 62 """ : "null";
70 63 }
71 64 }
@@ -88,12 +81,6 @@ public class JsonTransformer : StringConverter
88 81 for (int i = 0; i < subObjects.Count; i++)
89 82 {
90 83 var obj = subObjects[i];
91 var tabString = string.Empty;
92 for (int k = 0; k < obj.Layer; k++)
93 {
94 tabString += " ";
95 }
96 outString += "\n" + tabString;
97 84 string? currentConnectString;
98 85 if (i == subObjects.Count - 1)
99 86 {
@@ -112,24 +99,20 @@ public class JsonTransformer : StringConverter
112 99 {
113 100 if (IsEnumerableClassMember(obj))
114 101 outString += obj.Value is not null ? $$"""
115 {{tabString}}"{{obj.Name}}": [{{OutPutObject(obj)}}
116 {{tabString}}]{{currentConnectString}}
102 "{{obj.Name}}": [{{OutPutObject(obj)}}]{{currentConnectString}}
117 103 """ : "null";
118 104 else if (IsEnumerableMember(obj))
119 105 if (obj.Value is not null && (obj.Type!.IsAssignableTo(typeof(IEnumerable)) || obj.Type.IsAssignableTo(typeof(Array))))
120 106 outString += obj.Value is not null ? $$"""
121 {{tabString}}[{{OutPutObject(obj)}}
122 {{tabString}}]{{currentConnectString}}
107 [{{OutPutObject(obj)}}]{{currentConnectString}}
123 108 """ : "null";
124 109 else
125 110 outString += obj.Value is not null ? $$"""
126 {{tabString}}{{{OutPutObject(obj)}}
127 {{tabString}}}{{currentConnectString}}
111 {{{OutPutObject(obj)}}}{{currentConnectString}}
128 112 """ : "null";
129 113 else
130 114 outString += obj.Value is not null ? $$"""
131 {{tabString}}"{{obj.Name}}": {{{tabString}}{{OutPutObject(obj)}}
132 {{tabString}}}{{currentConnectString}}
115 "{{obj.Name}}": {{{OutPutObject(obj)}}}{{currentConnectString}}
133 116 """ : "null";
134 117 }
135 118 }
Modified XFEExtension.NetCore/XFETransform/StringConverter/StringConverter.cs +4 -0
@@ -20,6 +20,10 @@ public abstract class StringConverter : IStringConverter
20 20 /// </summary>
21 21 public static JsonTransformer JsonTransformer { get; } = new();
22 22 /// <summary>
23 /// 格式化Json转换注入
24 /// </summary>
25 public static FormattedJsonTransformer FormattedJsonTransformer { get; } = new();
26 /// <summary>
23 27 /// 输出对象信息
24 28 /// </summary>
25 29 /// <param name="objectInfo"></param>