diff --git a/Library/Api/IJsonProvider.cs b/Library/Api/IJsonProvider.cs
index 744a701..146a020 100644
--- a/Library/Api/IJsonProvider.cs
+++ b/Library/Api/IJsonProvider.cs
@@ -148,6 +148,14 @@ namespace Serein.Library.Api
///
IJsonToken Parse(string json);
+ ///
+ /// 尝试解析JSON文本为IJsonToken对象,如果成功则返回true,并通过out参数返回解析后的对象。
+ ///
+ ///
+ ///
+ ///
+ bool TryParse(string json, out IJsonToken jsonToken);
+
///
/// 创建对象
///
diff --git a/Library/Utils/JsonHelper.cs b/Library/Utils/JsonHelper.cs
index 41b12b8..c02bf1f 100644
--- a/Library/Utils/JsonHelper.cs
+++ b/Library/Utils/JsonHelper.cs
@@ -58,6 +58,19 @@ namespace Serein.Library.Utils
return provider.Parse(json);
}
+ ///
+ /// 尝试解析Json文本为IJsonToken对象
+ ///
+ ///
+ ///
+ ///
+ public static bool TryParse(string json, out IJsonToken jsonToken)
+ {
+ return provider.TryParse(json, out jsonToken);
+ }
+
+
+
///
/// 将对象序列化为Json文本
///
diff --git a/Serein.Extend.NewtonsoftJson/NewtonsoftJsonProvider.cs b/Serein.Extend.NewtonsoftJson/NewtonsoftJsonProvider.cs
index 0b1d248..6273c71 100644
--- a/Serein.Extend.NewtonsoftJson/NewtonsoftJsonProvider.cs
+++ b/Serein.Extend.NewtonsoftJson/NewtonsoftJsonProvider.cs
@@ -2,11 +2,12 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using Serein.Library.Api;
+using System.Diagnostics.CodeAnalysis;
namespace Serein.Extend.NewtonsoftJson
{
- public enum JsonType
+ public enum ProviderType
{
Default = 0,
Web = 1,
@@ -30,11 +31,11 @@ namespace Serein.Extend.NewtonsoftJson
/// 基于Newtonsoft.Json的JSON门户实现
///
///
- public NewtonsoftJsonProvider(JsonType jsonType)
+ public NewtonsoftJsonProvider(ProviderType jsonType)
{
settings = jsonType switch
{
- JsonType.Web => new JsonSerializerSettings
+ ProviderType.Web => new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(), // 控制首字母小写
NullValueHandling = NullValueHandling.Ignore // 可选:忽略 null
@@ -98,6 +99,26 @@ namespace Serein.Extend.NewtonsoftJson
return NewtonsoftJsonTokenFactory.Parse(json);
}
+ ///
+ /// 尝试解析JSON文本为IJsonToken对象,如果成功则返回true,并通过out参数返回解析后的对象。
+ ///
+ ///
+ ///
+ ///
+ public bool TryParse(string json, [NotNullWhen(true)] out IJsonToken? jsonToken)
+ {
+ try
+ {
+ jsonToken = NewtonsoftJsonTokenFactory.Parse(json);
+ return true;
+ }
+ catch (Exception)
+ {
+ jsonToken = null!;
+ return false;
+ }
+ }
+
///
/// 创建一个新的JSON数组对象。
///
diff --git a/Serein.Proto.WebSocket/Handle/WebSocketHandleModule.cs b/Serein.Proto.WebSocket/Handle/WebSocketHandleModule.cs
index c86c303..cd5cbb4 100644
--- a/Serein.Proto.WebSocket/Handle/WebSocketHandleModule.cs
+++ b/Serein.Proto.WebSocket/Handle/WebSocketHandleModule.cs
@@ -91,80 +91,82 @@ namespace Serein.Proto.WebSocket.Handle
public async Task HandleAsync(WebSocketHandleContext context)
{
var jsonObject = context.MsgRequest; // 获取到消息
+ context.Model = new ModuleConfig(); // 设置当前模块配置
+ context.Model.IsResponseUseReturn = _moduleConfig.IsResponseUseReturn;
if (jsonObject is null)
{
context.TriggerExceptionTracking($"请求没有获取到消息");
return; // 没有获取到消息
}
+
+ if (!jsonObject.TryGetValue(_moduleConfig.MsgIdJsonKey, out var msgIdToken) || msgIdToken.IsNull)
+ {
+ context.TriggerExceptionTracking($"消息Id从JSON键[{_moduleConfig.MsgIdJsonKey}]提取失败");
+ return; // 没有获取到消息
+ }
+ if (msgIdToken.Type != IJsonToken.TokenType.Value)
+ {
+ context.TriggerExceptionTracking($"请求消息Id[{_moduleConfig.ThemeJsonKey}]需要值类型,当前类型为[{msgIdToken.Type}]");
+ return; // 没有获取到消息
+ }
+ var msgId = msgIdToken.ToString(); // 获取Id
+ context.Model.MsgId = msgId;
+ // 验证消息ID是否重复
+ if (!_myMsgIdHash.Add(msgId))
+ {
+ context.TriggerExceptionTracking($"消息Id[{msgId}]重复发送");
+ return; // 消息重复
+ }
+
+
+
if(!jsonObject.TryGetValue(_moduleConfig.ThemeJsonKey, out var themeToken) || themeToken.IsNull)
{
- context.TriggerExceptionTracking($"请求没有获取到主题\"{_moduleConfig.ThemeJsonKey}\"");
+ context.TriggerExceptionTracking($"主题从JSON键[{_moduleConfig.ThemeJsonKey}]提取失败");
return; // 没有获取到消息
}
if(themeToken.Type != IJsonToken.TokenType.Value)
{
- context.TriggerExceptionTracking($"请求主题需要值类型 \"{_moduleConfig.ThemeJsonKey}\"");
+ context.TriggerExceptionTracking($"请求主题[{_moduleConfig.ThemeJsonKey}]需要值类型,当前类型为[{themeToken.Type}]");
return; // 没有获取到消息
}
var theme = themeToken.ToString(); // 获取主题
+ context.Model.Theme = theme;
// 验证主题
if (!_methodInvokeConfigs.TryGetValue(theme, out var handldConfig))
{
- context.TriggerExceptionTracking($"{_moduleConfig.ThemeJsonKey} 主题不存在");
+ context.TriggerExceptionTracking($"不存在这样的主题");
return;
}
- if (!jsonObject.TryGetValue(_moduleConfig.MsgIdJsonKey, out var msgIdToken) || themeToken.IsNull)
- {
- context.TriggerExceptionTracking($"主题 {theme} 没有消息Id");
- return; // 没有获取到消息
- }
- if (themeToken.Type != IJsonToken.TokenType.Value)
- {
- context.TriggerExceptionTracking($"请求消息Id需要值类型 \"{_moduleConfig.ThemeJsonKey}\"");
- return; // 没有获取到消息
- }
-
- var msgId = msgIdToken.ToString(); // 获取主题
- // 验证消息ID是否重复
- if (!_myMsgIdHash.Add(msgId))
- {
- context.TriggerExceptionTracking($"主题 {theme} 消息Id {msgId} 消息重复");
- return; // 消息重复
- }
// 验证数据
if (!jsonObject.TryGetValue(_moduleConfig.DataJsonKey, out var dataToken))
{
- context.TriggerExceptionTracking($"主题 {theme} 消息Id {msgId} 数据提取失败,当前指定键\"{_moduleConfig.DataJsonKey}\"");
+ context.TriggerExceptionTracking($"数据从JSON键[{_moduleConfig.DataJsonKey}]提取失败");
return; // 没有主题
}
if(dataToken.Type != IJsonToken.TokenType.Object)
{
- context.TriggerExceptionTracking($"主题 {theme} 消息Id {msgId} 数据需要 JSON Object");
+ context.TriggerExceptionTracking($"数据需要 JSON Object,当前类型为[{dataToken.Type}]");
+ return;
}
- context.MsgTheme = theme; // 添加主题
- context.MsgId = msgId; // 添加 ID
context.MsgData = dataToken; // 添加消息
context.MsgRequest = jsonObject; // 添加原始消息
try
{
if (TryGetParameters(handldConfig, context, out var args))
{
- var result = await HandleAsync(handldConfig, args);
+ var result = await InvokeAsync(handldConfig, args);
if (handldConfig.IsReturnValue)
{
- await RepliedAsync(_moduleConfig, context, result);
+ await RepliedAsync(_moduleConfig, context, result);
}
}
- else
- {
- context.TriggerExceptionTracking($"主题 {theme} 消息Id {msgId} 参数获取失败");
- }
}
catch (Exception ex)
{
@@ -184,7 +186,7 @@ namespace Serein.Proto.WebSocket.Handle
///
///
///
- public static async Task