mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 00:39:00 +08:00
docs(setting): 添加设置系统文档并实现JSON序列化器
- 新增设置系统详细文档,包含核心概念、接口定义和基本用法 - 实现JSON序列化器支持对象序列化和反序列化功能 - 添加序列化器单元测试验证序列化功能正确性 - 提供自定义转换器支持和错误处理机制
This commit is contained in:
parent
a5d18e4eca
commit
3d86cdb093
@ -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,
|
||||
|
||||
@ -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}'.",
|
||||
|
||||
@ -175,7 +175,7 @@ public interface ISettingsMigration
|
||||
Type SettingsType { get; }
|
||||
int FromVersion { get; }
|
||||
int ToVersion { get; }
|
||||
ISettingsSection Migrate(ISettingsSection oldData);
|
||||
ISettingsData Migrate(ISettingsData oldData);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user