GwWuYou c3376bf4d5 feat(storage): 添加存储系统接口和文件存储实现
- 定义了IStorage接口提供同步和异步的数据存储操作功能
- 实现了基于文件系统的FileStorage类支持读写删除操作
- 添加了ScopedStorage包装器为存储键提供作用域前缀功能
- 创建了ISerializer接口并实现JsonSerializer使用Newtonsoft.Json
- 在项目中引入Newtonsoft.Json包依赖
2026-01-11 19:56:31 +08:00

29 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using GFramework.Game.Abstractions.serializer;
using Newtonsoft.Json;
namespace GFramework.Game.serializer;
/// <summary>
/// JSON序列化器实现类用于将对象序列化为JSON字符串或将JSON字符串反序列化为对象
/// </summary>
public sealed class JsonSerializer : ISerializer
{
/// <summary>
/// 将指定的对象序列化为JSON字符串
/// </summary>
/// <typeparam name="T">要序列化的对象类型</typeparam>
/// <param name="value">要序列化的对象实例</param>
/// <returns>序列化后的JSON字符串</returns>
public string Serialize<T>(T value)
=> JsonConvert.SerializeObject(value);
/// <summary>
/// 将JSON字符串反序列化为指定类型的对象
/// </summary>
/// <typeparam name="T">要反序列化的对象类型</typeparam>
/// <param name="data">包含JSON数据的字符串</param>
/// <returns>反序列化后的对象实例</returns>
/// <exception cref="ArgumentException">当无法反序列化数据时抛出</exception>
public T Deserialize<T>(string data)
=> JsonConvert.DeserializeObject<T>(data) ?? throw new ArgumentException("Cannot deserialize data");
}