mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-04-29 02:53:23 +08:00
从Serein.Library分离了WebSocket/Modbus;新增了Json门户类,用于未来的Http、WebSocket、Mqtt、gRPC、QUIC扩展。
This commit is contained in:
90
Serein.Extend.NewtonsoftJson/NewtonsoftJsonProvider.cs
Normal file
90
Serein.Extend.NewtonsoftJson/NewtonsoftJsonProvider.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Serein.Library.Api;
|
||||
|
||||
namespace Serein.Extend.NewtonsoftJson
|
||||
{
|
||||
|
||||
public enum JsonType
|
||||
{
|
||||
Default = 0,
|
||||
Web = 1,
|
||||
}
|
||||
/// <summary>
|
||||
/// 基于Newtonsoft.Json的IJsonProvider实现
|
||||
/// </summary>
|
||||
public sealed class NewtonsoftJsonProvider : IJsonProvider
|
||||
{
|
||||
private JsonSerializerSettings settings;
|
||||
|
||||
public NewtonsoftJsonProvider()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public NewtonsoftJsonProvider(JsonType jsonType)
|
||||
{
|
||||
settings = jsonType switch
|
||||
{
|
||||
JsonType.Web => new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver(), // 控制首字母小写
|
||||
NullValueHandling = NullValueHandling.Ignore // 可选:忽略 null
|
||||
},
|
||||
_ => new JsonSerializerSettings
|
||||
{
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public NewtonsoftJsonProvider(JsonSerializerSettings settings)
|
||||
{
|
||||
settings = new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver(), // 控制首字母小写
|
||||
NullValueHandling = NullValueHandling.Ignore // 可选:忽略 null
|
||||
};
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
public T? Deserialize<T>(string jsonText)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(jsonText, settings);
|
||||
}
|
||||
|
||||
public object? Deserialize(string jsonText, Type type)
|
||||
{
|
||||
return JsonConvert.DeserializeObject(jsonText, type, settings);
|
||||
}
|
||||
|
||||
public string Serialize(object obj)
|
||||
{
|
||||
return JsonConvert.SerializeObject(obj, settings);
|
||||
}
|
||||
|
||||
public IJsonToken Parse(string json)
|
||||
{
|
||||
var token = JToken.Parse(json);
|
||||
return new NewtonsoftJsonToken(token);
|
||||
}
|
||||
|
||||
public IJsonToken CreateObject(IDictionary<string, object> values = null)
|
||||
{
|
||||
var jobj = values != null ? JObject.FromObject(values) : new JObject();
|
||||
return new NewtonsoftJsonToken(jobj);
|
||||
}
|
||||
|
||||
public IJsonToken CreateArray(IEnumerable<object> values = null)
|
||||
{
|
||||
var jarr = values != null ? JArray.FromObject(values) : new JArray();
|
||||
return new NewtonsoftJsonToken(jarr);
|
||||
}
|
||||
|
||||
public IJsonToken FromObject(object obj)
|
||||
{
|
||||
var token = JToken.FromObject(obj);
|
||||
return new NewtonsoftJsonToken(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
59
Serein.Extend.NewtonsoftJson/NewtonsoftJsonToken.cs
Normal file
59
Serein.Extend.NewtonsoftJson/NewtonsoftJsonToken.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Serein.Library.Api;
|
||||
|
||||
namespace Serein.Extend.NewtonsoftJson
|
||||
{
|
||||
/// <summary>
|
||||
/// 基于Newtonsoft.Json的IJsonToken实现
|
||||
/// </summary>
|
||||
public sealed class NewtonsoftJsonToken : IJsonToken
|
||||
{
|
||||
private readonly JToken _token;
|
||||
|
||||
public NewtonsoftJsonToken(JToken token)
|
||||
{
|
||||
_token = token ?? throw new ArgumentNullException(nameof(token));
|
||||
}
|
||||
|
||||
public bool TryGetValue(string name, out IJsonToken token)
|
||||
{
|
||||
if (_token is JObject obj && obj.TryGetValue(name, out JToken value))
|
||||
{
|
||||
token = new NewtonsoftJsonToken(value);
|
||||
return true;
|
||||
}
|
||||
token = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public IJsonToken? GetValue(string name)
|
||||
{
|
||||
if (_token is JObject obj && obj.TryGetValue(name, out JToken value))
|
||||
{
|
||||
return new NewtonsoftJsonToken(value);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public string GetString() => _token.Type == JTokenType.Null ? null : _token.ToString();
|
||||
|
||||
public int GetInt32() => _token.Value<int>();
|
||||
|
||||
public bool GetBoolean() => _token.Value<bool>();
|
||||
|
||||
public bool IsNull => _token.Type == JTokenType.Null || _token.Type == JTokenType.Undefined;
|
||||
|
||||
public IEnumerable<IJsonToken> EnumerateArray()
|
||||
{
|
||||
if (_token is JArray arr)
|
||||
return arr.Select(x => new NewtonsoftJsonToken(x));
|
||||
throw new InvalidOperationException("当前Token不是数组类型。");
|
||||
}
|
||||
|
||||
public T ToObject<T>() => _token.ToObject<T>();
|
||||
|
||||
public object ToObject(Type type) => _token.ToObject(type);
|
||||
|
||||
public override string ToString() => _token.ToString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net8.0;net462</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Library\Serein.Library.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user