mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-02 15:50:47 +08:00
从Serein.Library分离了WebSocket/Modbus;新增了Json门户类,用于未来的Http、WebSocket、Mqtt、gRPC、QUIC扩展。
This commit is contained in:
@@ -1,11 +1,5 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Utils
|
||||
{
|
||||
@@ -118,37 +112,7 @@ namespace Serein.Library.Utils
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 对象转JSON文本
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToJsonText(this object obj)
|
||||
{
|
||||
var jsonText = JsonConvert.SerializeObject(obj, Formatting.Indented);
|
||||
return jsonText;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// JSON文本转对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T">转换类型</typeparam>
|
||||
/// <param name="json">JSON文本</param>
|
||||
/// <returns></returns>
|
||||
public static T ToJsonObject<T>(this string json)
|
||||
{
|
||||
try
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 对象转换为对应类型
|
||||
/// </summary>
|
||||
@@ -162,18 +126,18 @@ namespace Serein.Library.Utils
|
||||
{
|
||||
return default;
|
||||
}
|
||||
return (TResult)data.ToConvert(type);
|
||||
return (TResult)data.ToConvertValueType(type);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 对象转换(好像没啥用)
|
||||
/// 对象转换
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public static object ToConvert(this object data, Type type)
|
||||
public static object ToConvertValueType(this object data, Type type)
|
||||
{
|
||||
if (type.IsValueType)
|
||||
{
|
||||
|
||||
@@ -1,16 +1,9 @@
|
||||
using Microsoft.Extensions.ObjectPool;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Serein.Library.Api;
|
||||
using Serein.Library.Utils;
|
||||
using Serein.Library.Api;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reactive.Subjects;
|
||||
using System.Threading;
|
||||
using System.Threading.Channels;
|
||||
using System.Threading.Tasks;
|
||||
using System.Transactions;
|
||||
|
||||
namespace Serein.Library.Utils
|
||||
{
|
||||
|
||||
70
Library/Utils/JsonHelper.cs
Normal file
70
Library/Utils/JsonHelper.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using Serein.Library.Api;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Serein.Library.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Json门户类,需要你提供实现
|
||||
/// </summary>
|
||||
public static class JsonHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Json门户类,需要你提供实现
|
||||
/// </summary>
|
||||
private static IJsonProvider provider;
|
||||
|
||||
/// <summary>
|
||||
/// 使用第三方包进行解析
|
||||
/// </summary>
|
||||
/// <param name="jsonPortal"></param>
|
||||
public static void UseJsonLibrary(IJsonProvider jsonPortal)
|
||||
{
|
||||
JsonHelper.provider = jsonPortal;
|
||||
}
|
||||
|
||||
|
||||
public static T Deserialize<T>(string jsonText)
|
||||
{
|
||||
return provider.Deserialize<T>(jsonText);
|
||||
}
|
||||
|
||||
public static object Deserialize(string jsonText, Type type)
|
||||
{
|
||||
return provider.Deserialize(jsonText, type);
|
||||
|
||||
}
|
||||
|
||||
public static IJsonToken Parse(string json)
|
||||
{
|
||||
return provider.Parse(json);
|
||||
|
||||
}
|
||||
|
||||
public static string Serialize(object obj)
|
||||
{
|
||||
return provider.Serialize(obj);
|
||||
}
|
||||
public static IJsonToken Object(Action<Dictionary<string, object>> init)
|
||||
{
|
||||
var dict = new Dictionary<string, object>();
|
||||
init(dict);
|
||||
return provider.CreateObject(dict);
|
||||
}
|
||||
|
||||
public static IJsonToken Array(IEnumerable<object> values)
|
||||
{
|
||||
return provider.CreateArray(values);
|
||||
}
|
||||
|
||||
public static IJsonToken FromObject(object obj)
|
||||
{
|
||||
if (obj is System.Collections.IEnumerable && !(obj is string))
|
||||
return provider.CreateObject(obj as IDictionary<string, object>);
|
||||
return provider.CreateArray(obj as IEnumerable<object>);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,12 +1,7 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Utils
|
||||
{
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Serein.Library.Network.WebSocketCommunication;
|
||||
using Serein.Library.Network.WebSocketCommunication;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Utils
|
||||
|
||||
@@ -12,6 +12,9 @@ using System.Xml.Linq;
|
||||
|
||||
namespace Serein.Library
|
||||
{
|
||||
/// <summary>
|
||||
/// 全局运行环境
|
||||
/// </summary>
|
||||
public static class SereinEnv
|
||||
{
|
||||
private static IFlowEnvironment environment;
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Serein.Library.Utils;
|
||||
using Serein.Library.Utils.SereinExpression.Resolver;
|
||||
using Serein.Library.Utils.SereinExpression.Resolver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.Design;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
@@ -276,7 +276,7 @@ namespace Serein.Library.Utils.SereinExpression
|
||||
|
||||
if (hasType)
|
||||
{
|
||||
target = target.ToConvert(type);
|
||||
target = target.ToConvertValueType(type);
|
||||
}
|
||||
|
||||
|
||||
@@ -437,7 +437,7 @@ namespace Serein.Library.Utils.SereinExpression
|
||||
int endIndex = expression.IndexOf('>');
|
||||
if(endIndex == expression.Length -1)
|
||||
{
|
||||
return value.ToConvert(type);
|
||||
return value.ToConvertValueType(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user