Skip to content

Commit 6d087eb

Browse files
authored
Merge pull request #166 from dotnet-campus/t/lvyi/system-text-json
在 net8.0 和 net9.0 中,完全移除 Newtonsoft.Json,使用 System.Text.Json
2 parents e525c57 + 6602429 commit 6d087eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1201
-856
lines changed

.editorconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ csharp_preferred_modifier_order = public,private,protected,internal,static,exter
5252
csharp_prefer_braces = true:silent
5353
csharp_prefer_simple_default_expression = true:suggestion
5454
csharp_preserve_single_line_blocks = true
55-
csharp_preserve_single_line_statements = true
5655
csharp_space_after_cast = true
5756
csharp_space_after_colon_in_inheritance_clause = true
5857
csharp_space_after_keywords_in_control_flow_statements = true

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<ItemGroup>
33
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
44
<PackageVersion Include="dotnetCampus.AsyncWorkerCollection.Source" Version="1.7.2" />
5-
<PackageVersion Include="dotnetCampus.CommandLine" Version="3.3.1" />
6-
<PackageVersion Include="dotnetCampus.LatestCSharpFeatures" Version="12.0.1" />
5+
<PackageVersion Include="DotNetCampus.CommandLine" Version="4.0.1-alpha.1" />
6+
<PackageVersion Include="DotNetCampus.LatestCSharpFeatures" Version="13.0.0" />
77
<PackageVersion Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
88
<PackageVersion Include="Microsoft.CodeAnalysis" Version="4.10.0" />
99
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />

demo/IpcDirectRoutedAotDemo/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
var ipcProvider = new IpcProvider(pipeName, new IpcConfiguration()
2020
{
2121

22-
}.UseSystemJsonIpcObjectSerializer(SourceGenerationContext.Default));
22+
}.UseSystemTextJsonIpcObjectSerializer(SourceGenerationContext.Default));
2323
var ipcDirectRoutedProvider = new JsonIpcDirectRoutedProvider(ipcProvider);
2424
ipcDirectRoutedProvider.AddNotifyHandler(notifyPath, () =>
2525
{
@@ -50,7 +50,7 @@
5050
var peerName = args[0];
5151
Console.WriteLine($"[{Environment.ProcessId}] 客户端进程启动");
5252
var jsonIpcDirectRoutedProvider = new JsonIpcDirectRoutedProvider(ipcConfiguration: new IpcConfiguration()
53-
.UseSystemJsonIpcObjectSerializer(SourceGenerationContext.Default));
53+
.UseSystemTextJsonIpcObjectSerializer(SourceGenerationContext.Default));
5454
JsonIpcDirectRoutedClientProxy jsonIpcDirectRoutedClientProxy = await jsonIpcDirectRoutedProvider.GetAndConnectClientAsync(peerName);
5555

5656
Console.WriteLine($"[{Environment.ProcessId}] 客户端发送通知");

demo/dotnetCampus.Ipc.WpfDemo/MainWindow.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
using System.Windows.Media.Imaging;
1616
using System.Windows.Navigation;
1717
using System.Windows.Shapes;
18-
18+
using DotNetCampus.Cli;
1919
using dotnetCampus.Ipc.Context;
2020
using dotnetCampus.Ipc.Pipes;
2121
using dotnetCampus.Ipc.WpfDemo.View;
@@ -34,7 +34,7 @@ public MainWindow()
3434
InitializeComponent();
3535
DataContext = this;
3636

37-
var options = dotnetCampus.Cli.CommandLine.Parse(Environment.GetCommandLineArgs()).As<Options>();
37+
var options = CommandLine.Parse(Environment.GetCommandLineArgs().Skip(1).ToArray()).As<Options>();
3838
if (!string.IsNullOrEmpty(options.ServerName))
3939
{
4040
Debugger.Launch();
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
using dotnetCampus.Cli;
1+
using DotNetCampus.Cli.Compiler;
22

3-
namespace dotnetCampus.Ipc.WpfDemo
3+
namespace dotnetCampus.Ipc.WpfDemo;
4+
5+
public class Options
46
{
5-
public class Options
6-
{
7-
/// <summary>
8-
/// 本机的服务名
9-
/// </summary>
10-
[Option(longName: nameof(ServerName))]
11-
public string? ServerName { get; set; }
7+
/// <summary>
8+
/// 本机的服务名
9+
/// </summary>
10+
[Option]
11+
public string? ServerName { get; set; }
1212

13-
/// <summary>
14-
/// 对方的服务名
15-
/// </summary>
16-
[Option(longName: nameof(PeerName))]
17-
public string? PeerName { get; set; }
18-
}
13+
/// <summary>
14+
/// 对方的服务名
15+
/// </summary>
16+
[Option]
17+
public string? PeerName { get; set; }
1918
}
Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
using System.Threading.Tasks;
2-
1+
using dotnetCampus.Ipc.CompilerServices.GeneratedProxies.Models;
32
using dotnetCampus.Ipc.Messages;
3+
using dotnetCampus.Ipc.Serialization;
4+
5+
namespace dotnetCampus.Ipc.CompilerServices.GeneratedProxies.Contexts;
46

5-
namespace dotnetCampus.Ipc.CompilerServices.GeneratedProxies.Contexts
7+
internal class GeneratedIpcJointResponse : IIpcResponseMessage
68
{
7-
internal class GeneratedIpcJointResponse : IIpcResponseMessage
8-
{
9-
internal static GeneratedIpcJointResponse Empty { get; } = new GeneratedIpcJointResponse();
9+
internal static GeneratedIpcJointResponse Empty { get; } = new GeneratedIpcJointResponse();
1010

11-
private GeneratedIpcJointResponse()
12-
{
13-
}
11+
private GeneratedIpcJointResponse()
12+
{
13+
}
1414

15-
private GeneratedIpcJointResponse(IpcMessage message)
16-
{
17-
ResponseMessage = message;
18-
}
15+
private GeneratedIpcJointResponse(IpcMessage message)
16+
{
17+
ResponseMessage = message;
18+
}
1919

20-
public IpcMessage ResponseMessage { get; }
20+
public IpcMessage ResponseMessage { get; }
2121

22-
internal static async Task<GeneratedIpcJointResponse> FromAsyncReturnModel(Task<GeneratedProxyMemberReturnModel?> asyncReturnModel)
23-
{
24-
var returnModel = await asyncReturnModel.ConfigureAwait(false);
25-
var message = returnModel is null
26-
? new IpcMessage()
27-
: GeneratedProxyMemberReturnModel.Serialize(returnModel);
28-
return new GeneratedIpcJointResponse(message);
29-
}
22+
internal static async Task<GeneratedIpcJointResponse> FromAsyncReturnModel(
23+
Task<GeneratedProxyMemberReturnModel?> asyncReturnModel,
24+
IIpcObjectSerializer serializer)
25+
{
26+
var returnModel = await asyncReturnModel.ConfigureAwait(false);
27+
var message = returnModel is null
28+
? new IpcMessage()
29+
: serializer.SerializeToIpcMessage(returnModel, "Return");
30+
return new GeneratedIpcJointResponse(message);
3031
}
3132
}

src/dotnetCampus.Ipc/CompilerServices/GeneratedProxies/Garms/Garm.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
using System;
2-
3-
using dotnetCampus.Ipc.CompilerServices.GeneratedProxies.Models;
4-
5-
#if UseNewtonsoftJson
6-
using Newtonsoft.Json.Linq;
7-
#endif
1+
using dotnetCampus.Ipc.CompilerServices.GeneratedProxies.Models;
2+
using dotnetCampus.Ipc.Serialization;
83

94
namespace dotnetCampus.Ipc.CompilerServices.GeneratedProxies;
105

@@ -15,19 +10,17 @@ namespace dotnetCampus.Ipc.CompilerServices.GeneratedProxies;
1510
{
1611
private readonly object? _value;
1712

18-
#if UseNewtonsoftJson
1913
/// <summary>
20-
/// 创建一个存储 IPC 中 <see cref="JToken"/> 的临时 GARM 模型。
14+
/// 创建一个存储 IPC 中 <see cref="IpcJsonElement"/> 的临时 GARM 模型。
2115
/// </summary>
2216
/// <param name="value">模型的值。</param>
2317
/// <param name="valueType">模型中值的类型。</param>
24-
public Garm(JToken? value, Type valueType)
18+
public Garm(IpcJsonElement value, Type valueType)
2519
{
2620
_value = value;
2721
ValueType = valueType ?? throw new ArgumentNullException(nameof(valueType));
2822
IpcType = null;
2923
}
30-
#endif
3124

3225
/// <summary>
3326
/// 创建一个存储 IPC 中 IPC 代理对象的临时 GARM 模型。
@@ -49,7 +42,7 @@ public Garm(object? value, Type ipcType)
4942
/// proxy -> IPC model in proxy side -> IPC model in joint side -> joint
5043
/// 其中,proxy 和 joint 均由编译器在用户端生成了泛型代码,因此这些上下文中都可以获取到泛型类型。
5144
/// 但中间的传输过程中没有泛型上下文,所以会用到此临时类型中转值。
52-
/// 由于中转的值可能是 <see cref="JToken"/> 这种未确定类型的值,为避免代码中意外写出错误的代码,因此禁止在此类型中获取值。
45+
/// 由于中转的值可能是 JsonElement 这种未确定类型的值,为避免代码中意外写出错误的代码,因此禁止在此类型中获取值。
5346
/// </remarks>
5447
object? IGarmObject.Value => throw new InvalidOperationException("当 GARM 未获得泛型类型时,不可获取其值。请等待泛型上下文中调用 ToGeneric<T> 转换为泛型类型后再获取值。");
5548

@@ -68,10 +61,13 @@ public Garm(object? value, Type ipcType)
6861
/// </summary>
6962
/// <typeparam name="T">泛型类型。</typeparam>
7063
/// <returns>泛型 GARM 模型。</returns>
71-
public Garm<T> ToGeneric<T>()
64+
public Garm<T> ToGeneric<T>(IIpcObjectSerializer serializer) => _value switch
7265
{
73-
return new Garm<T>(
74-
KnownTypeConverter.ConvertBackFromJTokenOrObject<T>(_value),
75-
IpcType);
76-
}
66+
// 是 null。
67+
null => new Garm<T>(default, IpcType),
68+
// 是一个普通的 IPC 对象。
69+
IpcJsonElement jsonElement => new Garm<T>(IpcJsonElement.Deserialize<T>(jsonElement, serializer), IpcType),
70+
// 是一个 IPC 代理对象。
71+
_ => new Garm<T>((T?) _value, IpcType),
72+
};
7773
}

src/dotnetCampus.Ipc/CompilerServices/GeneratedProxies/Garms/Garm.generic.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System;
2-
3-
using dotnetCampus.Ipc.CompilerServices.Attributes;
1+
using dotnetCampus.Ipc.CompilerServices.Attributes;
42

53
namespace dotnetCampus.Ipc.CompilerServices.GeneratedProxies;
4+
65
/// <summary>
76
/// <para>IPC GARM 模型。</para>
87
/// <para>GARM = Generated Argument and Return Model 生成类的参数与返回值模型。</para>

src/dotnetCampus.Ipc/CompilerServices/GeneratedProxies/Garms/IGarmObject.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System;
1+
using dotnetCampus.Ipc.CompilerServices.GeneratedProxies.Models;
22

33
namespace dotnetCampus.Ipc.CompilerServices.GeneratedProxies;
4+
45
/// <summary>
56
/// 为 <see cref="Garm{T}"/> 提供非泛型访问的方法。
67
/// </summary>
@@ -9,6 +10,10 @@ public interface IGarmObject
910
/// <summary>
1011
/// 获取 <see cref="Garm{T}"/> 的值。
1112
/// </summary>
13+
/// <remarks>
14+
/// 对于发送方,值可能是:<see langword="null"/>、可 JSON 序列化的任意对象(即将被序列化)、IPC 代理对象。<br/>
15+
/// 对于接收方,值可能是:<see langword="null"/>、<see cref="IpcJsonElement"/>(用于延迟反序列化)、IPC 代理对象。
16+
/// </remarks>
1217
object? Value { get; }
1318

1419
/// <summary>

src/dotnetCampus.Ipc/CompilerServices/GeneratedProxies/GeneratedIpcFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ public static TContract CreateIpcJoint<TContract>(this IIpcProvider ipcProvider,
122122
var realType = realInstance.GetType();
123123
if (IpcTypeToProxyJointCache[typeof(TContract)].jointType is { } jointType)
124124
{
125+
var context = GetContext(ipcProvider);
125126
var joint = (GeneratedIpcJoint<TContract>) Activator.CreateInstance(jointType)!;
127+
joint.Context = context;
126128
joint.SetInstance(realInstance);
127-
GetContext(ipcProvider).JointManager.AddPublicIpcObject(joint, ipcObjectId);
129+
context.JointManager.AddPublicIpcObject(joint, ipcObjectId);
128130
return realInstance;
129131
}
130132
else

0 commit comments

Comments
 (0)