diff --git a/GFramework.Game.Tests/Serializer/JsonSerializerTests.cs b/GFramework.Game.Tests/Serializer/JsonSerializerTests.cs index d79f48b4..1fe1a59e 100644 --- a/GFramework.Game.Tests/Serializer/JsonSerializerTests.cs +++ b/GFramework.Game.Tests/Serializer/JsonSerializerTests.cs @@ -93,6 +93,26 @@ public sealed class JsonSerializerTests Assert.That(((PlayerStateStub)restored).Level, Is.EqualTo(11)); } + [Test] + public void Serialize_With_Runtime_Type_Should_Allow_Null_Object() + { + var serializer = new GameJsonSerializer(); + + var json = serializer.Serialize(null!, typeof(PlayerStateStub)); + + Assert.That(json, Is.EqualTo("null")); + } + + [Test] + public void Deserialize_Should_Preserve_ArgumentException_For_Invalid_Input_Arguments() + { + var serializer = new GameJsonSerializer(); + + var exception = Assert.Throws(() => serializer.Deserialize(string.Empty)); + + Assert.That(exception, Is.Not.Null); + } + private sealed class PlayerStateStub { public string Name { get; set; } = string.Empty; @@ -121,7 +141,7 @@ public sealed class JsonSerializerTests writer.WriteValue($"{value?.X}:{value?.Y}"); } - public override CoordinateStub? ReadJson( + public override CoordinateStub ReadJson( JsonReader reader, Type objectType, CoordinateStub? existingValue, diff --git a/GFramework.Game/Serializer/JsonSerializer.cs b/GFramework.Game/Serializer/JsonSerializer.cs index 1157b2fd..d246f95b 100644 --- a/GFramework.Game/Serializer/JsonSerializer.cs +++ b/GFramework.Game/Serializer/JsonSerializer.cs @@ -1,6 +1,5 @@ using GFramework.Core.Abstractions.Serializer; using Newtonsoft.Json; -using NewtonsoftJsonException = Newtonsoft.Json.JsonException; namespace GFramework.Game.Serializer; @@ -51,7 +50,6 @@ public sealed class JsonSerializer /// 序列化后的JSON字符串 public string Serialize(object obj, Type type) { - ArgumentNullException.ThrowIfNull(obj); ArgumentNullException.ThrowIfNull(type); return JsonConvert.SerializeObject(obj, type, _settings); @@ -107,7 +105,7 @@ public sealed class JsonSerializer return result; } - catch (Exception ex) when (ex is NewtonsoftJsonException or InvalidCastException or ArgumentException) + catch (Exception ex) when (ex is InvalidCastException) { throw new InvalidOperationException( $"Failed to deserialize JSON to target type '{targetType.FullName}'.", diff --git a/docs/zh-CN/game/setting.md b/docs/zh-CN/game/setting.md index 93d2232b..bbf16f2d 100644 --- a/docs/zh-CN/game/setting.md +++ b/docs/zh-CN/game/setting.md @@ -175,7 +175,7 @@ public interface ISettingsMigration Type SettingsType { get; } int FromVersion { get; } int ToVersion { get; } - ISettingsSection Migrate(ISettingsSection oldData); + ISettingsData Migrate(ISettingsData oldData); } ```