docs(setting): 添加设置系统文档并实现JSON序列化器

- 新增设置系统详细文档,包含核心概念、接口定义和基本用法
- 实现JSON序列化器支持对象序列化和反序列化功能
- 添加序列化器单元测试验证序列化功能正确性
- 提供自定义转换器支持和错误处理机制
This commit is contained in:
GeWuYou 2026-04-05 22:26:07 +08:00
parent a5d18e4eca
commit 3d86cdb093
3 changed files with 23 additions and 5 deletions

View File

@ -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<ArgumentException>(() => serializer.Deserialize<PlayerStateStub>(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,

View File

@ -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
/// <returns>序列化后的JSON字符串</returns>
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}'.",

View File

@ -175,7 +175,7 @@ public interface ISettingsMigration
Type SettingsType { get; }
int FromVersion { get; }
int ToVersion { get; }
ISettingsSection Migrate(ISettingsSection oldData);
ISettingsData Migrate(ISettingsData oldData);
}
```