Skip to content

Commit 506dc95

Browse files
committed
Revert "去掉 Json 库,重新构建进行测试"
This reverts commit 178366b.
1 parent 178366b commit 506dc95

File tree

7 files changed

+174
-2
lines changed

7 files changed

+174
-2
lines changed

src/dotnetCampus.Ipc/Context/IpcConfiguration.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,18 @@ internal IEnumerable<IIpcRequestHandler> GetIpcRequestHandlers()
113113
/// </summary>
114114
public IIpcObjectSerializer IpcObjectSerializer
115115
{
116-
get => _ipcObjectSerializer ?? throw new Exception("xxxx");
116+
get => _ipcObjectSerializer ??= DefaultNewtonsoftJsonSerializer;
117117
set => _ipcObjectSerializer = value;
118118
}
119119

120120
private IIpcObjectSerializer? _ipcObjectSerializer;
121+
122+
/// <summary>
123+
/// 默认的 Newtonsoft Json 序列化器
124+
/// </summary>
125+
public static IpcObjectJsonSerializer DefaultNewtonsoftJsonSerializer
126+
// 不加上锁了,这里不管线程安全,最多就是多创建几个对象而已,不会影响业务逻辑
127+
=> _defaultNewtonsoftJsonSerializer ??= new IpcObjectJsonSerializer();
128+
private static IpcObjectJsonSerializer? _defaultNewtonsoftJsonSerializer;
121129
}
122130
}

src/dotnetCampus.Ipc/Context/IpcConfigurationExtensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using dotnetCampus.Ipc.Serialization;
2+
using Newtonsoft.Json;
23

34
namespace dotnetCampus.Ipc.Context;
45

@@ -7,6 +8,26 @@ namespace dotnetCampus.Ipc.Context;
78
/// </summary>
89
public static class IpcConfigurationExtensions
910
{
11+
/// <summary>
12+
/// 使用 Newtonsoft.Json 作为 IPC 对象序列化器
13+
/// </summary>
14+
/// <param name="configuration"></param>
15+
/// <param name="jsonSerializer"></param>
16+
/// <returns></returns>
17+
public static IpcConfiguration UseNewtonsoftJsonIpcObjectSerializer(this IpcConfiguration configuration, JsonSerializer? jsonSerializer)
18+
{
19+
if (jsonSerializer is null)
20+
{
21+
configuration.IpcObjectSerializer = IpcConfiguration.DefaultNewtonsoftJsonSerializer;
22+
}
23+
else
24+
{
25+
configuration.IpcObjectSerializer = new IpcObjectJsonSerializer(jsonSerializer);
26+
}
27+
28+
return configuration;
29+
}
30+
1031
#if NET6_0_OR_GREATER
1132
/// <summary>
1233
/// 使用 System.Text.Json 作为 IPC 对象序列化器

src/dotnetCampus.Ipc/IpcRouteds/DirectRouteds/Json_/JsonIpcDirectRoutedHandleRequestExceptionInfo.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
namespace dotnetCampus.Ipc.IpcRouteds.DirectRouteds;
1+
using Newtonsoft.Json;
2+
3+
namespace dotnetCampus.Ipc.IpcRouteds.DirectRouteds;
24

35
/// <summary>
46
/// 直接路由的 IPC 通讯的异常信息,即 IPC 服务端的业务层通讯异常。如参数不匹配、执行的业务端逻辑抛出异常等
57
/// </summary>
68
internal class JsonIpcDirectRoutedHandleRequestExceptionResponse
79
{
10+
[JsonProperty("__$Exception")]
811
#if NET6_0_OR_GREATER
912
[System.Text.Json.Serialization.JsonPropertyName("__$Exception")]
1013
#endif

src/dotnetCampus.Ipc/Pipes/PeerProxy.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using dotnetCampus.Ipc.Internals;
99
using dotnetCampus.Ipc.Messages;
1010
using dotnetCampus.Ipc.Utils.Extensions;
11+
using Newtonsoft.Json.Schema;
1112

1213
namespace dotnetCampus.Ipc.Pipes
1314
{
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.IO;
2+
using System.Text;
3+
using Newtonsoft.Json;
4+
5+
namespace dotnetCampus.Ipc.Serialization
6+
{
7+
public class IpcObjectJsonSerializer : IIpcObjectSerializer
8+
{
9+
public IpcObjectJsonSerializer()
10+
{
11+
JsonSerializer = JsonSerializer.CreateDefault();
12+
}
13+
14+
public IpcObjectJsonSerializer(JsonSerializer jsonSerializer)
15+
{
16+
JsonSerializer = jsonSerializer;
17+
}
18+
19+
private JsonSerializer JsonSerializer { get; }
20+
21+
public byte[] Serialize(object value)
22+
{
23+
var json = JsonConvert.SerializeObject(value);
24+
return Encoding.UTF8.GetBytes(json);
25+
}
26+
27+
public void Serialize(Stream stream, object? value)
28+
{
29+
using var textWriter = new StreamWriter(stream, Encoding.UTF8, leaveOpen: true, bufferSize: 2048);
30+
JsonSerializer.Serialize(textWriter, value);
31+
}
32+
33+
public T? Deserialize<T>(byte[] byteList)
34+
{
35+
var json = Encoding.UTF8.GetString(byteList);
36+
return JsonConvert.DeserializeObject<T>(json);
37+
}
38+
39+
public T? Deserialize<T>(Stream stream)
40+
{
41+
using StreamReader reader = new StreamReader
42+
(
43+
stream,
44+
#if !NETCOREAPP
45+
Encoding.UTF8,
46+
detectEncodingFromByteOrderMarks: false,
47+
bufferSize: 2048,
48+
#endif
49+
leaveOpen: true
50+
);
51+
JsonReader jsonReader = new JsonTextReader(reader);
52+
return JsonSerializer.Deserialize<T>(jsonReader);
53+
}
54+
}
55+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Text;
4+
5+
using dotnetCampus.Ipc.Messages;
6+
7+
using Newtonsoft.Json;
8+
9+
namespace dotnetCampus.Ipc.Serialization
10+
{
11+
/// <summary>
12+
/// 如果某 IPC 消息计划以 JSON 格式传输,那么可使用此类型来序列化和反序列化。
13+
/// </summary>
14+
public static class JsonIpcMessageSerializer
15+
{
16+
/// <summary>
17+
/// 将 IPC 模块自动生成的内部模型序列化成可供跨进程传输的 IPC 消息。
18+
/// </summary>
19+
/// <param name="tag"></param>
20+
/// <param name="model"></param>
21+
/// <returns></returns>
22+
public static IpcMessage Serialize(string tag, object model)
23+
{
24+
if (model is null)
25+
{
26+
throw new ArgumentNullException(nameof(model));
27+
}
28+
29+
var json = JsonConvert.SerializeObject(model);
30+
var data = Encoding.UTF8.GetBytes(json);
31+
var message = new IpcMessage(tag, new IpcMessageBody(data));
32+
return message;
33+
}
34+
35+
/// <summary>
36+
/// 尝试将跨进程传输过来的 IPC 消息反序列化成 IPC 模块自动生成的内部模型。
37+
/// </summary>
38+
/// <param name="message">IPC 消息。</param>
39+
/// <param name="model"></param>
40+
/// <returns></returns>
41+
public static bool TryDeserialize<T>(IpcMessage message, [NotNullWhen(true)] out T? model) where T : class, new()
42+
{
43+
var body = message.Body;
44+
var stringBody = Encoding.UTF8.GetString(body.Buffer, body.Start, body.Length);
45+
if (string.IsNullOrWhiteSpace(stringBody))
46+
{
47+
model = new T();
48+
return true;
49+
}
50+
try
51+
{
52+
model = JsonConvert.DeserializeObject<T>(stringBody);
53+
return true;
54+
}
55+
catch (JsonSerializationException)
56+
{
57+
model = null;
58+
return false;
59+
}
60+
catch (JsonReaderException)
61+
{
62+
// Newtonsoft.Json.JsonReaderException
63+
// Unexpected character encountered while parsing value: {0}.
64+
// JSON 字符串中包含不符合格式的字符。典型情况如下:
65+
// * IPC 消息头被意外合入了消息体
66+
// * 待发现……
67+
model = null;
68+
return false;
69+
}
70+
catch
71+
{
72+
// 此反序列化过程抛出异常是合理行为(毕竟 IPC 谁都能发,但应该主要是 JsonSerializationException)。
73+
// 目前来看,还不知道有没有一些合理的正常的情况下会抛出其他异常,因此暂时在 !DEBUG 下不处理消息。
74+
#if DEBUG
75+
throw;
76+
#else
77+
model = null;
78+
return false;
79+
#endif
80+
}
81+
}
82+
}
83+
}

src/dotnetCampus.Ipc/dotnetCampus.Ipc.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<ItemGroup>
4444
<PackageReference Include="dotnetCampus.LatestCSharpFeatures" PrivateAssets="all" />
4545
<PackageReference Include="dotnetCampus.AsyncWorkerCollection.Source" PrivateAssets="all" />
46+
<PackageReference Include="Newtonsoft.Json" />
4647
<PackageReference Include="System.ValueTuple" />
4748
</ItemGroup>
4849

0 commit comments

Comments
 (0)