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

XFEExtension

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

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

XFEstudio/XFEExtension

add-增加ValueNode的ToString重写方法 fix-现在Json解析不会被无关的空格换行等任何字符串干扰

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

代码差异

5 个文件 +195 -93
Modified XFEExtension.NetCore.Test/Program.cs +5 -13
@@ -7,22 +7,14 @@ internal class Program
7 7 public static string CurrentDialogID { get; set; } = Guid.NewGuid().ToString();
8 8 private static async Task Main(string[] args)
9 9 {
10 //XFEChatGPT.XFEChatGPTMessageReceived += XFEChatGPT_XFEChatGPTMessageReceived;
11 //XFEChatGPT.CreateDialog(CurrentDialogID, "你是一个专门负责整理谣言的人工智能,我会提供给你一些带有时间线的谣言,这其中可能会有很多不相干的信息和没有时间线的信息,你需要自己辨别判断有用信息并将其排序从最早的到最新的日期来排序,除此之外不要说任何多余的内容,格式是:[+-RumorEntry { TimeLine = 2017/2/4, Content = 你整理的第一条谣言内容 }-+][+-RumorEntry { TimeLine = 2019/8/12, Content = 你整理的第二条谣言内容 }-+][+-RumorEntry { TimeLine = 2022/10/4, Content = 你整理的第三条谣言内容 }-+][+-RumorEntry { TimeLine = 2023/1/23, Content = 你整理的第四条谣言内容 }-+]...以此类推", true, true, ChatGPTModel.gpt4o);
12 ////await XFEConsole.WriteObject(XFEChatGPT, false, false);
13 ////Console.WriteLine(XFEChatGPT);
14 //XFEChatGPT.AskChatGPT(CurrentDialogID, Guid.NewGuid().ToString(), "测试谣言");
15 //Console.ReadLine();
16 10 using var client = new HttpClient();
17 11 client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0");
18 QueryableJsonNode jsonNode = await client.GetStringAsync("https://zy.xywlapi.cc/qqapi?qq=64482927");
19 if (jsonNode["status"] == "500")
12 client.DefaultRequestHeaders.Add("Origin", "https://www.piyao.org.cn");
13 client.DefaultRequestHeaders.Add("Referer", "https://www.piyao.org.cn/");
14 QueryableJsonNode jsonNode = await client.GetStringAsync($"https://so.news.cn/xhtvapp/rumourSearch?title={Uri.EscapeDataString(Console.ReadLine())}&pageNum=1&timeInterval=&startTime=&endTime=&typeName=&pageSize=100&sort=-2&callback=?");
15 foreach (var nodes in jsonNode["content"]["resultList"]["package:list", "title", "thirdCheckTime", "url"].PackageInListObject())
20 16 {
21 Console.WriteLine(jsonNode["message"]);
22 }
23 else
24 {
25 Console.WriteLine("成功");
17 Console.WriteLine($"标题:{nodes["title"].Value}\t发布时间:{nodes["thirdCheckTime"].Value}\t网址:{nodes["url"].Value}");
26 18 }
27 19 }
28 20
Modified XFEExtension.NetCore/XFEExtension.NetCore.csproj +3 -2
@@ -5,7 +5,7 @@
5 5 <ImplicitUsings>enable</ImplicitUsings>
6 6 <Nullable>enable</Nullable>
7 7 <GenerateDocumentationFile>True</GenerateDocumentationFile>
8 <Version>3.1.2</Version>
8 <Version>3.1.3</Version>
9 9 <Title>XFEExtension</Title>
10 10 <RepositoryUrl>https://github.com/XFEstudio/XFEExtension</RepositoryUrl>
11 11 <AnalysisLevel>latest</AnalysisLevel>
@@ -20,7 +20,8 @@
20 20 <PackageTags>XFE;拓展;GPT;Server;服务器;测试;XFEExtension</PackageTags>
21 21 <PackageReleaseNotes>## 调整
22 22
23 重写可查询节点的ToString方法,简化内插
23 增加ValueNode的ToString重写方法
24 现在Json解析不会被无关的空格换行等任何字符串干扰
24 25
25 26 ## 新增
26 27
Modified XFEExtension.NetCore/XFETransform/JsonConverter/JsonNodeConverter.cs +177 -77
@@ -28,116 +28,176 @@ public class JsonNodeConverter(string jsonString)
28 28 var jsonNode = new JsonComplexPropertyNode();
29 29 CurrentState currentState = CurrentState.None;
30 30 JsonSymbol lastSymbol = JsonSymbol.Brace;
31 JsonSymbol currentSymbol = JsonSymbol.Brace;
31 32 for (int i = 0; i < jsonString.Length; i++)
32 33 {
33 34 var current = jsonString[i];
34 35 var currentJsonComplexJsonNode = GetJsonNodeByIndex(jsonNode, currentPosition);
35 JsonSymbol currentSymbol;
36 36 ValueType currentValueType;
37 37 switch (current)
38 38 {
39 39 case '{':
40 currentSymbol = JsonSymbol.Brace;
41 currentState = CurrentState.Property;
42 if (jsonNode.DescendingNodes.Count == 0)
43 break;
44 currentLayer++;
45 if (currentPosition.Count < currentLayer + 2)
46 currentPosition.Add(0);
40 if ((currentState != CurrentState.Property && currentState != CurrentState.StringPropertyValue) || currentState == CurrentState.AfterPropertyValue || currentState == CurrentState.AfterProperty)
41 {
42 currentSymbol = JsonSymbol.Brace;
43 currentState = CurrentState.BeforeProperty;
44 if (jsonNode.DescendingNodes.Count == 0)
45 break;
46 currentLayer++;
47 if (currentPosition.Count < currentLayer + 2)
48 currentPosition.Add(0);
49 else
50 currentPosition[currentLayer + 1] += 1;
51 var newObjectNode = new JsonComplexPropertyNode
52 {
53 DescendingNodes = [],
54 IsList = false,
55 Layer = currentLayer,
56 PropertyName = currentPropertyName.Replace("\"", "")
57 };
58 currentJsonComplexJsonNode.DescendingNodes.Add(newObjectNode);
59 currentPropertyName = "";
60 currentPropertyValue = "";
61 }
47 62 else
48 currentPosition[currentLayer + 1] += 1;
49 var newObjectNode = new JsonComplexPropertyNode
50 63 {
51 DescendingNodes = [],
52 IsList = false,
53 Layer = currentLayer,
54 PropertyName = currentPropertyName.Replace("\"", "")
55 };
56 currentJsonComplexJsonNode.DescendingNodes.Add(newObjectNode);
57 currentPropertyName = "";
58 currentPropertyValue = "";
64 currentPropertyValue += "{";
65 }
59 66 break;
60 67 case '}':
61 currentSymbol = JsonSymbol.InvertedBrace;
62 currentState = CurrentState.None;
63 if (lastSymbol == JsonSymbol.Text || lastSymbol == JsonSymbol.DoubleQuotationMark)
68 if ((currentState != CurrentState.Property && currentState != CurrentState.StringPropertyValue) || currentState == CurrentState.AfterPropertyValue || currentState == CurrentState.AfterProperty)
64 69 {
65 currentValueType = JudgeValueType(ref currentPropertyValue);
66 currentJsonComplexJsonNode = GetJsonNodeByIndex(jsonNode, currentPosition);
67 if (SummonJsonPropertyNode(currentValueType, currentLayer, currentPropertyName.Replace("\"", ""), currentPropertyValue) is JsonPropertyNode jsonPropertyNode)
68 currentJsonComplexJsonNode.DescendingNodes.Add(jsonPropertyNode);
69 else
70 Console.WriteLine($"设置复杂Json节点失败:{currentLayer}\t{currentPropertyName}\t{currentPropertyValue}");
70 currentSymbol = JsonSymbol.InvertedBrace;
71 currentState = CurrentState.None;
72 if (lastSymbol == JsonSymbol.Text || lastSymbol == JsonSymbol.DoubleQuotationMark)
73 {
74 currentValueType = JudgeValueType(ref currentPropertyValue);
75 currentJsonComplexJsonNode = GetJsonNodeByIndex(jsonNode, currentPosition);
76 if (SummonJsonPropertyNode(currentValueType, currentLayer, currentPropertyName.Replace("\"", ""), currentPropertyValue) is JsonPropertyNode jsonPropertyNode)
77 currentJsonComplexJsonNode.DescendingNodes.Add(jsonPropertyNode);
78 else
79 Console.WriteLine($"设置复杂Json节点失败:{currentLayer}\t{currentPropertyName}\t{currentPropertyValue}");
80 }
81 currentPosition.RemoveAt(currentPosition.Count - 1);
82 currentLayer--;
83 currentPropertyName = "";
84 currentPropertyValue = "";
85 }
86 else
87 {
88 currentPropertyValue += "}";
71 89 }
72 currentPosition.RemoveAt(currentPosition.Count - 1);
73 currentLayer--;
74 currentPropertyName = "";
75 currentPropertyValue = "";
76 90 break;
77 91 case '[':
78 currentLayer++;
79 currentSymbol = JsonSymbol.Bracket;
80 currentState = CurrentState.ListValue;
81 if (currentPosition.Count < currentLayer + 2)
82 currentPosition.Add(0);
92 if ((currentState != CurrentState.Property && currentState != CurrentState.StringPropertyValue) || currentState == CurrentState.AfterPropertyValue || currentState == CurrentState.AfterProperty)
93 {
94 currentLayer++;
95 currentSymbol = JsonSymbol.Bracket;
96 currentState = CurrentState.ListValue;
97 if (currentPosition.Count < currentLayer + 2)
98 currentPosition.Add(0);
99 else
100 currentPosition[currentLayer + 1] += 1;
101 var newListNode = new JsonComplexPropertyNode
102 {
103 DescendingNodes = [],
104 IsList = true,
105 Layer = currentLayer,
106 PropertyName = currentPropertyName.Replace("\"", "")
107 };
108 currentJsonComplexJsonNode.DescendingNodes.Add(newListNode);
109 currentPropertyName = "";
110 currentPropertyValue = "";
111 }
83 112 else
84 currentPosition[currentLayer + 1] += 1;
85 var newListNode = new JsonComplexPropertyNode
86 113 {
87 DescendingNodes = [],
88 IsList = true,
89 Layer = currentLayer,
90 PropertyName = currentPropertyName.Replace("\"", "")
91 };
92 currentJsonComplexJsonNode.DescendingNodes.Add(newListNode);
93 currentPropertyName = "";
94 currentPropertyValue = "";
114 currentPropertyValue += "[";
115 }
95 116 break;
96 117 case ']':
97 118 currentSymbol = JsonSymbol.InvertedBracket;
98 currentState = CurrentState.None;
99 if (lastSymbol == JsonSymbol.Text || lastSymbol == JsonSymbol.DoubleQuotationMark)
119 if ((currentState != CurrentState.Property && currentState != CurrentState.StringPropertyValue) || currentState == CurrentState.AfterPropertyValue || currentState == CurrentState.AfterProperty)
100 120 {
101 currentValueType = JudgeValueType(ref currentPropertyValue);
102 currentJsonComplexJsonNode = GetJsonNodeByIndex(jsonNode, currentPosition);
103 if (SummonJsonPropertyNode(currentValueType, currentLayer, currentPropertyName.Replace("\"", ""), currentPropertyValue) is JsonPropertyNode jsonPropertyNode)
104 currentJsonComplexJsonNode.DescendingNodes.Add(jsonPropertyNode);
105 else
106 Console.WriteLine($"设置复杂Json节点失败:{currentLayer}\t{currentPropertyName}\t{currentPropertyValue}");
121 currentState = CurrentState.None;
122 if (lastSymbol == JsonSymbol.Text || lastSymbol == JsonSymbol.DoubleQuotationMark)
123 {
124 currentValueType = JudgeValueType(ref currentPropertyValue);
125 currentJsonComplexJsonNode = GetJsonNodeByIndex(jsonNode, currentPosition);
126 if (SummonJsonPropertyNode(currentValueType, currentLayer, currentPropertyName.Replace("\"", ""), currentPropertyValue) is JsonPropertyNode jsonPropertyNode)
127 currentJsonComplexJsonNode.DescendingNodes.Add(jsonPropertyNode);
128 else
129 Console.WriteLine($"设置复杂Json节点失败:{currentLayer}\t{currentPropertyName}\t{currentPropertyValue}");
130 }
131 currentPosition.RemoveAt(currentPosition.Count - 1);
132 currentLayer--;
133 }
134 else
135 {
136 currentPropertyValue += "]";
107 137 }
108 currentPosition.RemoveAt(currentPosition.Count - 1);
109 currentLayer--;
110 138 break;
111 139 case ':':
112 currentSymbol = JsonSymbol.Colon;
113 currentState = CurrentState.PropertyValue;
140 if ((currentState != CurrentState.Property && currentState != CurrentState.StringPropertyValue) || currentState == CurrentState.AfterPropertyValue || currentState == CurrentState.AfterProperty)
141 {
142 currentSymbol = JsonSymbol.Colon;
143 currentState = CurrentState.BeforePropertyValue;
144 }
145 else
146 {
147 currentPropertyValue += ":";
148 }
114 149 break;
115 150 case ',':
116 currentSymbol = JsonSymbol.Comma;
117 currentPosition[currentLayer + 1] += 1;
118 if (currentState != CurrentState.ListValue)
119 currentState = CurrentState.Property;
120 if (lastSymbol == JsonSymbol.Text || lastSymbol == JsonSymbol.DoubleQuotationMark)
151 if ((currentState != CurrentState.Property && currentState != CurrentState.StringPropertyValue) || currentState == CurrentState.AfterPropertyValue || currentState == CurrentState.AfterProperty)
121 152 {
122 currentValueType = JudgeValueType(ref currentPropertyValue);
123 currentJsonComplexJsonNode = GetJsonNodeByIndex(jsonNode, currentPosition);
124 if (SummonJsonPropertyNode(currentValueType, currentLayer, currentPropertyName.Replace("\"", ""), currentPropertyValue) is JsonPropertyNode jsonPropertyNode)
125 currentJsonComplexJsonNode.DescendingNodes.Add(jsonPropertyNode);
126 else
127 Console.WriteLine($"设置复杂Json节点失败:{currentLayer}\t{currentPropertyName}\t{currentPropertyValue}");
153 currentSymbol = JsonSymbol.Comma;
154 currentPosition[currentLayer + 1] += 1;
155 if (currentState != CurrentState.ListValue)
156 currentState = CurrentState.BeforeProperty;
157 if (lastSymbol == JsonSymbol.Text || lastSymbol == JsonSymbol.DoubleQuotationMark)
158 {
159 currentValueType = JudgeValueType(ref currentPropertyValue);
160 currentJsonComplexJsonNode = GetJsonNodeByIndex(jsonNode, currentPosition);
161 if (SummonJsonPropertyNode(currentValueType, currentLayer, currentPropertyName.Replace("\"", ""), currentPropertyValue) is JsonPropertyNode jsonPropertyNode)
162 currentJsonComplexJsonNode.DescendingNodes.Add(jsonPropertyNode);
163 else
164 Console.WriteLine($"设置复杂Json节点失败:{currentLayer}\t{currentPropertyName}\t{currentPropertyValue}");
165 }
166 currentPropertyName = "";
167 currentPropertyValue = "";
168 }
169 else
170 {
171 currentPropertyValue += ",";
128 172 }
129 currentPropertyName = "";
130 currentPropertyValue = "";
131 173 break;
132 174 case '"':
133 175 currentSymbol = JsonSymbol.DoubleQuotationMark;
134 if (currentState == CurrentState.Property)
135 currentPropertyName += "\"";
136 else if (currentState == CurrentState.PropertyValue)
137 currentPropertyValue += "\"";
176 switch (currentState)
177 {
178 case CurrentState.Property:
179 currentPropertyName += "\"";
180 currentState = CurrentState.AfterPropertyValue;
181 break;
182 case CurrentState.BeforeProperty:
183 currentPropertyName += "\"";
184 currentState = CurrentState.Property;
185 break;
186 case CurrentState.StringPropertyValue:
187 currentPropertyValue += "\"";
188 currentState = CurrentState.AfterPropertyValue;
189 break;
190 case CurrentState.BeforePropertyValue:
191 currentPropertyValue += "\"";
192 currentState = CurrentState.StringPropertyValue;
193 break;
194 default:
195 break;
196 }
138 197 break;
139 198 case '\'':
140 199 currentSymbol = JsonSymbol.QuotationMark;
200 currentPropertyName += "'";
141 201 currentState = CurrentState.None;
142 202 break;
143 203 default:
@@ -147,12 +207,19 @@ public class JsonNodeConverter(string jsonString)
147 207 case CurrentState.Property:
148 208 currentPropertyName += current;
149 209 break;
150 case CurrentState.PropertyValue:
210 case CurrentState.PropertyValue or CurrentState.StringPropertyValue:
151 211 currentPropertyValue += current;
152 212 break;
153 213 case CurrentState.ListValue:
154 214 currentPropertyValue += current;
155 215 break;
216 case CurrentState.BeforePropertyValue:
217 if (IsNormalValueExceptString(current))
218 {
219 currentPropertyValue += current;
220 currentState = CurrentState.PropertyValue;
221 }
222 break;
156 223 case CurrentState.None:
157 224 break;
158 225 default:
@@ -164,6 +231,26 @@ public class JsonNodeConverter(string jsonString)
164 231 }
165 232 return jsonNode;
166 233 }
234 private static bool IsNormalValueExceptString(char current) => current switch
235 {
236 '0' => true,
237 '1' => true,
238 '2' => true,
239 '3' => true,
240 '4' => true,
241 '5' => true,
242 '6' => true,
243 '7' => true,
244 '8' => true,
245 '9' => true,
246 'f' => true,
247 'F' => true,
248 't' => true,
249 'T' => true,
250 'n' => true,
251 'N' => true,
252 _ => false
253 };
167 254 private static JsonComplexPropertyNode GetJsonNodeByIndex(JsonComplexPropertyNode jsonComplexPropertyNode, List<short> indexes)
168 255 {
169 256 var currentJsonComplexPropertyNode = jsonComplexPropertyNode;
@@ -184,6 +271,10 @@ public class JsonNodeConverter(string jsonString)
184 271 currentPropertyValue = currentPropertyValue.Replace("\"", "");
185 272 return ValueType.Text;
186 273 }
274 else if(currentPropertyValue.Equals("null", StringComparison.CurrentCultureIgnoreCase))
275 {
276 return ValueType.Null;
277 }
187 278 else if (currentPropertyValue.StartsWith('\''))
188 279 {
189 280 currentPropertyValue = currentPropertyValue.Replace("'", "");
@@ -201,10 +292,14 @@ public class JsonNodeConverter(string jsonString)
201 292 {
202 293 return ValueType.Int;
203 294 }
204 else
295 else if (long.TryParse(currentPropertyValue, out _))
205 296 {
206 297 return ValueType.Long;
207 298 }
299 else
300 {
301 return ValueType.None;
302 }
208 303 }
209 304 private static JsonPropertyNode? SummonJsonPropertyNode(ValueType currentValueType, int currentLayer, string currentPropertyName, string currentPropertyValue)
210 305 {
@@ -237,7 +332,12 @@ public class JsonNodeConverter(string jsonString)
237 332 enum CurrentState
238 333 {
239 334 Property,
335 BeforeProperty,
336 AfterProperty,
240 337 PropertyValue,
338 StringPropertyValue,
339 BeforePropertyValue,
340 AfterPropertyValue,
241 341 ListValue,
242 342 None
243 343 }
Modified XFEExtension.NetCore/XFETransform/JsonConverter/ValueNode.cs +5 -0
@@ -11,4 +11,9 @@ public class ValueNode(string value, ValueType valueType) : IValueNode
11 11 public string Value { get; set; } = value;
12 12 ///<inheritdoc/>
13 13 public ValueType ValueType { get; set; } = valueType;
14 /// <summary>
15 /// 字符串值
16 /// </summary>
17 /// <returns></returns>
18 public override string ToString() => Value;
14 19 }
Modified XFEExtension.NetCore/XFETransform/JsonConverter/ValueType.cs +5 -1
@@ -32,5 +32,9 @@ public enum ValueType
32 32 /// <summary>
33 33 /// 浮点类型
34 34 /// </summary>
35 Float
35 Float,
36 /// <summary>
37 /// 空类型
38 /// </summary>
39 Null
36 40 }