从Serein.Library分离了WebSocket/Modbus;新增了Json门户类,用于未来的Http、WebSocket、Mqtt、gRPC、QUIC扩展。

This commit is contained in:
fengjiayi
2025-07-27 23:34:01 +08:00
parent ab2adfde80
commit d3c3210ccc
70 changed files with 2306 additions and 554 deletions

View File

@@ -1,12 +1,11 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.Library.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Serein.Library.Web;
namespace Serein.Library.Network
{
@@ -15,6 +14,7 @@ namespace Serein.Library.Network
/// </summary>
public class ApiHandleConfig
{
private readonly IJsonPortal jsonPortal;
private readonly DelegateDetails delegateDetails;
/// <summary>
@@ -39,8 +39,9 @@ namespace Serein.Library.Network
/// <summary>
/// 添加处理配置
/// </summary>
/// <param name="jsonPortal"></param>
/// <param name="methodInfo"></param>
public ApiHandleConfig(MethodInfo methodInfo)
public ApiHandleConfig(IJsonPortal jsonPortal, MethodInfo methodInfo)
{
delegateDetails = new DelegateDetails(methodInfo);
var parameterInfos = methodInfo.GetParameters();
@@ -92,7 +93,7 @@ namespace Serein.Library.Network
}
else // if (type.IsValueType)
{
args[i] = JsonConvert.DeserializeObject(argValue, type);
args[i] = jsonPortal.Deserialize(argValue, type); // JsonConvert.DeserializeObject(argValue, type);
}
}
else
@@ -103,7 +104,7 @@ namespace Serein.Library.Network
return args;
}
public object[] GetArgsOfPost(Dictionary<string, string> routeData, JObject jsonObject)
public object[] GetArgsOfPost(Dictionary<string, string> routeData, IJsonToken jsonObject)
{
object[] args = new object[ParameterType.Length];
for (int i = 0; i < ParameterType.Length; i++)
@@ -120,7 +121,7 @@ namespace Serein.Library.Network
}
else // if (type.IsValueType)
{
args[i] = JsonConvert.DeserializeObject(argValue, type);
args[i] = jsonPortal.Deserialize(argValue, type);
}
}
else
@@ -134,16 +135,15 @@ namespace Serein.Library.Network
}
else if (jsonObject != null)
{
var jsonValue = jsonObject.GetValue(argName);
if (jsonValue is null)
if(jsonObject.TryGetProperty(argName, out var jsonToken))
{
// 值类型返回默认值引用类型返回null
args[i] = type.IsValueType ? Activator.CreateInstance(type) : null;
args[i] = jsonToken.ToObject(type);
}
else
{
args[i] = jsonValue.ToObject(type);
args[i] = type.IsValueType ? Activator.CreateInstance(type) : null;
}
}
}
return args;

View File

@@ -1,8 +1,5 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serein.Library.Api;
using Serein.Library.Api;
using Serein.Library.Network;
using Serein.Library.Utils;
using System;
using System.Collections;
using System.Collections.Concurrent;
@@ -47,7 +44,7 @@ namespace Serein.Library.Web
public class Router : IRouter
{
private readonly ISereinIOC SereinIOC; // 用于存储路由信息
private readonly IJsonPortal jsonPortal; // JSON门户
/// <summary>
/// 控制器实例对象的类型,每次调用都会重新实例化,[Url - ControllerType]
@@ -109,7 +106,7 @@ namespace Serein.Library.Web
SereinEnv.WriteLine(InfoType.INFO, url);
var apiType = routeAttribute.ApiType.ToString();
var config = new ApiHandleConfig(method);
var config = new ApiHandleConfig(jsonPortal, method);
if(!HandleModels.TryGetValue(apiType, out var configs))
{
configs = new ConcurrentDictionary<string, ApiHandleConfig>();

View File

@@ -1,13 +1,6 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.Library.Network.Http
namespace Serein.Library.Network.Http
{
internal static partial class SereinExtension
/*internal static partial class SereinExtension
{
#region JSON相关
@@ -60,5 +53,5 @@ namespace Serein.Library.Network.Http
}
}
#endregion
}
}*/
}