From d2533de3be3eeb56384a821f2900eeba7807aa0c Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sun, 5 Apr 2026 22:58:45 +0800 Subject: [PATCH] =?UTF-8?q?test(serializer):=20=E6=B7=BB=E5=8A=A0JSON?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=E5=99=A8=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=B9=B6=E4=BC=98=E5=8C=96=E5=8F=8D=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了完整的JsonSerializer单元测试覆盖序列化和反序列化场景 - 优化了反序列化方法中的异常处理逻辑,改进错误信息 - 修改了DeserializeCore方法中未使用的参数命名 - 添加了对各种边界情况的测试验证,包括无效JSON和空值处理 --- .../Serializer/JsonSerializerTests.cs | 17 ++++++++++++++ GFramework.Game/Serializer/JsonSerializer.cs | 23 +++++++++++-------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/GFramework.Game.Tests/Serializer/JsonSerializerTests.cs b/GFramework.Game.Tests/Serializer/JsonSerializerTests.cs index 1fe1a59e..b6eb1a49 100644 --- a/GFramework.Game.Tests/Serializer/JsonSerializerTests.cs +++ b/GFramework.Game.Tests/Serializer/JsonSerializerTests.cs @@ -82,6 +82,23 @@ public sealed class JsonSerializerTests }); } + [Test] + public void Deserialize_With_Runtime_Type_Should_Throw_With_Target_Type_Context_When_Json_Is_Invalid() + { + var serializer = new GameJsonSerializer(); + + var exception = + Assert.Throws(() => + serializer.Deserialize("{invalid json}", typeof(PlayerStateStub))); + + Assert.Multiple(() => + { + Assert.That(exception, Is.Not.Null); + Assert.That(exception!.Message, Does.Contain(typeof(PlayerStateStub).FullName)); + Assert.That(exception.InnerException, Is.Not.Null); + }); + } + [Test] public void Deserialize_With_Runtime_Type_Should_Return_Target_Runtime_Type() { diff --git a/GFramework.Game/Serializer/JsonSerializer.cs b/GFramework.Game/Serializer/JsonSerializer.cs index d246f95b..3e498840 100644 --- a/GFramework.Game/Serializer/JsonSerializer.cs +++ b/GFramework.Game/Serializer/JsonSerializer.cs @@ -67,7 +67,7 @@ public sealed class JsonSerializer return (T)DeserializeCore( data, typeof(T), - static (json, type, settings) => JsonConvert.DeserializeObject(json, settings)); + static (json, _, settings) => JsonConvert.DeserializeObject(json, settings)); } /// @@ -94,22 +94,25 @@ public sealed class JsonSerializer ArgumentNullException.ThrowIfNull(targetType); ArgumentNullException.ThrowIfNull(deserialize); + object? result; + try { - var result = deserialize(data, targetType, _settings); - if (result == null) - { - throw new InvalidOperationException( - $"Deserialization returned null for target type '{targetType.FullName}'."); - } - - return result; + result = deserialize(data, targetType, _settings); } - catch (Exception ex) when (ex is InvalidCastException) + catch (Exception ex) when (ex is not ArgumentException) { throw new InvalidOperationException( $"Failed to deserialize JSON to target type '{targetType.FullName}'.", ex); } + + if (result == null) + { + throw new InvalidOperationException( + $"Deserialization returned null for target type '{targetType.FullName}'."); + } + + return result; } } \ No newline at end of file