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

XFEExtension

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

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

XFEstudio/XFEExtension

fix-修复Json转换对布尔值和枚举类型的转换问题

add-添加分析对象对枚举类型的进一步支持,现在可以显示其转为int后的序号

720ea01
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

6 个文件 +46 -25
Modified XFEExtension.NetCore.Analyzer.Test/Program.cs +18 -2
@@ -1,4 +1,8 @@
1 namespace XFEExtension.NetCore.Analyzer.Test;
1 using System.Text.Json;
2 using XFEExtension.NetCore.StringExtension;
3 using XFEExtension.NetCore.StringExtension.Json;
4
5 namespace XFEExtension.NetCore.Analyzer.Test;
2 6
3 7 internal class Program
4 8 {
@@ -6,7 +10,19 @@ internal class Program
6 10 {
7 11 var testClass = new TestClass("测试名称", "测试描述哈哈", 59)
8 12 {
9 Tags = [["123", "321"], ["1234567", "7654321"]]
13 Tags = [["123", "321"], ["1234567", "7654321"]],
14 Enum = MyEnum.Test1
10 15 };
16 var json = testClass.ToJson();
17 //var json = JsonSerializer.Serialize(testClass);
18 //testClass.Enum.ToString().CW();
19 Console.WriteLine(json);
20 JsonSerializer.Deserialize<TestClass>(json).X();
11 21 }
22 }
23 enum MyEnum
24 {
25 Test1 = 3,
26 Test2 = 12,
27 Test3 =1
12 28 }
Modified XFEExtension.NetCore.Analyzer.Test/TestClass.cs +2 -0
@@ -5,6 +5,8 @@ internal class TestClass(string name, string description, int age)
5 5 public int id;
6 6 private int data;
7 7 public string Name { get; set; } = name;
8 public bool MyProperty { get; set; }
9 public MyEnum Enum { get; set; }
8 10 public string Description { get; set; } = description;
9 11 public int Age { get; set; } = age;
10 12 public List<List<string>> Tags { get; set; }
Modified XFEExtension.NetCore/XFEExtension.NetCore.csproj +4 -3
@@ -19,11 +19,12 @@
19 19 <PackageTags>XFE;拓展;GPT;Server;服务器;测试;XFEExtension</PackageTags>
20 20 <PackageReleaseNotes>## 调整
21 21
22 修复部分已知问题
22 修复Json转换对布尔值和枚举类型的转换问题
23 添加分析对象对枚举类型的进一步支持,现在可以显示其转为int后的序号
23 24
24 25 ## 新增
25 26
26 添加对象转为Json字符串拓展
27 无
27 28
28 29 ## 严重
29 30
@@ -32,7 +33,7 @@
32 33 <PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
33 34 <PackageLicenseFile>LICENSE</PackageLicenseFile>
34 35 <AssemblyOriginatorKeyFile>..\XFEstudio.pfx</AssemblyOriginatorKeyFile>
35 <Version>2.7.0</Version>
36 <Version>2.7.1</Version>
36 37 <Authors>XFEstudio</Authors>
37 38 </PropertyGroup>
38 39
Modified XFEExtension.NetCore/XFETransform/StringConverter/JsonTransformer.cs +10 -16
@@ -21,37 +21,33 @@ public class JsonTransformer : StringConverter
21 21 {
22 22 tabString += " ";
23 23 }
24 if (objectInfo.IsBasicType)
24 if (objectInfo.IsBasicType || objectInfo.ObjectPlace == ObjectPlace.Enum)
25 25 {
26 26 if (IsEnumerableMember(objectInfo))
27 27 {
28 28 if (objectInfo.Value is null)
29 {
30 29 outPutString += $"{tabString}null";
31 }
32 30 else if (objectInfo.Value is string)
33 {
34 31 outPutString += $"{tabString}\"{objectInfo.Value}\"";
35 }
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 36 else
37 {
38 37 outPutString += $"{tabString}{objectInfo.Value}";
39 }
40 38 }
41 39 else
42 40 {
43 41 if (objectInfo.Value is null)
44 {
45 42 outPutString += $"{tabString}\"{objectInfo.Name}\": null";
46 }
47 43 else if (objectInfo.Value is string)
48 {
49 44 outPutString += $"{tabString}\"{objectInfo.Name}\": \"{objectInfo.Value}\"";
50 }
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()}";
51 49 else
52 {
53 50 outPutString += $"{tabString}\"{objectInfo.Name}\": {objectInfo.Value}";
54 }
55 51 }
56 52 }
57 53 else
@@ -97,7 +93,7 @@ public class JsonTransformer : StringConverter
97 93 {
98 94 currentConnectString = ",";
99 95 }
100 if (obj.IsBasicType)
96 if (obj.IsBasicType || obj.ObjectPlace == ObjectPlace.Enum)
101 97 {
102 98 outString += obj.OutPutObject();
103 99 outString += currentConnectString;
@@ -110,7 +106,6 @@ public class JsonTransformer : StringConverter
110 106 {{tabString}}]{{currentConnectString}}
111 107 """ : "null";
112 108 else if (IsEnumerableMember(obj))
113 {
114 109 if (obj.Value is not null && (obj.Type!.IsAssignableTo(typeof(IEnumerable)) || obj.Type.IsAssignableTo(typeof(Array))))
115 110 outString += obj.Value is not null ? $$"""
116 111 {{tabString}}[{{OutPutObject(obj)}}
@@ -121,7 +116,6 @@ public class JsonTransformer : StringConverter
121 116 {{tabString}}{{{OutPutObject(obj)}}
122 117 {{tabString}}}{{currentConnectString}}
123 118 """ : "null";
124 }
125 119 else
126 120 outString += obj.Value is not null ? $$"""
127 121 {{tabString}}"{{obj.Name}}": {{{tabString}}{{OutPutObject(obj)}}
Modified XFEExtension.NetCore/XFETransform/StringConverter/ObjectAnalyzer.cs +6 -2
@@ -15,12 +15,16 @@ public class ObjectAnalyzer : StringConverter
15 15 public override string OutPutObject(IObjectInfo objectInfo)
16 16 {
17 17 var outPutString = string.Empty;
18 if (objectInfo.IsBasicType)
18 if (objectInfo.IsBasicType || objectInfo.ObjectPlace == ObjectPlace.Enum)
19 19 {
20 20 if (objectInfo.Value is null)
21 21 {
22 22 outPutString += $"{AddObjectPlace(objectInfo)} 空对象 {objectInfo.Name}:null\n";
23 23 }
24 else if(objectInfo.ObjectPlace == ObjectPlace.Enum)
25 {
26 outPutString += $"{AddObjectPlace(objectInfo)} {XFEConverter.OutPutTypeName(objectInfo.Type!)} {objectInfo.Name}:{objectInfo.Value}[{(int)objectInfo.Value}]\n";
27 }
24 28 else
25 29 {
26 30 outPutString += $"{AddObjectPlace(objectInfo)} {XFEConverter.OutPutTypeName(objectInfo.Type!)} {objectInfo.Name}:{objectInfo.Value}\n";
@@ -73,7 +77,7 @@ public class ObjectAnalyzer : StringConverter
73 77 {
74 78 currentConnectString = "├─";
75 79 }
76 if (obj.IsBasicType)
80 if (obj.IsBasicType || obj.ObjectPlace == ObjectPlace.Enum)
77 81 {
78 82 outString += currentConnectString;
79 83 outString += obj.OutPutObject();
Modified XFEExtension.NetCore/XFETransform/XFEConverter.cs +6 -2
@@ -86,7 +86,7 @@ public class XFEConverter
86 86 }
87 87 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, false, true, value, arrayObjects);
88 88 }
89 if (type.IsAssignableTo(typeof(IEnumerable)))
89 else if (type.IsAssignableTo(typeof(IEnumerable)))
90 90 {
91 91 var enumerableObjects = new List<IObjectInfo>();
92 92 foreach (var item in (IEnumerable)value)
@@ -102,7 +102,11 @@ public class XFEConverter
102 102 }
103 103 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, false, true, value, enumerableObjects);
104 104 }
105 if (type.IsValueType)
105 else if (type.IsAssignableTo(typeof(Enum)))
106 {
107 return new ObjectInfoImpl(stringConverter, name, ObjectPlace.Enum, layer, type, false, value);
108 }
109 else if (type.IsValueType)
106 110 {
107 111 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, false, value);
108 112 }