GFramework/GFramework.Game/Config/YamlConfigTextSerializer.cs
GeWuYou 12e54ce637 feat(config): 添加YAML配置序列化和校验功能
- 实现YamlConfigTextSerializer提供YAML文本序列化功能
- 实现YamlConfigTextValidator提供YAML文本校验功能
- 添加缓存机制优化schema文件加载性能
- 实现同步和异步校验接口支持
- 添加集成测试验证生成配置绑定功能
- 扩展SchemaConfigGenerator支持配置类型生成
- 实现GeneratedConfigConsumerIntegrationTests完整测试覆盖
2026-04-12 15:41:45 +08:00

44 lines
1.8 KiB
C#

using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace GFramework.Game.Config;
/// <summary>
/// 提供可复用的 YAML 文本序列化入口,供生成配置绑定与宿主写回流程共享。
/// </summary>
public static class YamlConfigTextSerializer
{
/// <summary>
/// 将配置对象序列化为 YAML 文本,并统一以 LF 作为尾随换行。
/// 该约定与底层 YamlDotNet 输出保持一致,避免不同操作系统的宿主行尾约定影响生成结果。
/// </summary>
/// <typeparam name="TValue">配置对象类型。</typeparam>
/// <param name="value">要序列化的配置对象。</param>
/// <returns>带尾随 LF 换行的 YAML 文本。</returns>
/// <exception cref="ArgumentNullException">当 <paramref name="value" /> 为 <see langword="null" /> 时抛出。</exception>
public static string Serialize<TValue>(TValue value)
{
ArgumentNullException.ThrowIfNull(value);
// Build one serializer per call so the helper does not rely on undocumented
// cross-thread safety guarantees from YamlDotNet's serializer implementation.
var yaml = CreateSerializer().Serialize(value);
return yaml.EndsWith('\n')
? yaml
: $"{yaml}\n";
}
/// <summary>
/// 创建与运行时配置绑定共享的 YAML 序列化器。
/// </summary>
/// <returns>复用统一命名与默认值策略的序列化器。</returns>
private static ISerializer CreateSerializer()
{
return new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.DisableAliases()
.ConfigureDefaultValuesHandling(DefaultValuesHandling.Preserve)
.Build();
}
}