mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 17:28:27 -04:00
37 lines
1.0 KiB
C#
37 lines
1.0 KiB
C#
using System.Text.RegularExpressions;
|
|
using NadekoBot.Common.Yml;
|
|
using NadekoBot.Common.Configs;
|
|
using YamlDotNet.Serialization;
|
|
|
|
namespace NadekoBot.Common;
|
|
|
|
public class YamlSeria : IConfigSeria
|
|
{
|
|
private readonly ISerializer _serializer;
|
|
private readonly IDeserializer _deserializer;
|
|
|
|
private static readonly Regex CodePointRegex
|
|
= new(@"(\\U(?<code>[a-zA-Z0-9]{8})|\\u(?<code>[a-zA-Z0-9]{4})|\\x(?<code>[a-zA-Z0-9]{2}))",
|
|
RegexOptions.Compiled);
|
|
|
|
public YamlSeria()
|
|
{
|
|
_serializer = Yaml.Serializer;
|
|
_deserializer = Yaml.Deserializer;
|
|
}
|
|
|
|
public string Serialize<T>(T obj)
|
|
{
|
|
var escapedOutput = _serializer.Serialize(obj);
|
|
var output = CodePointRegex.Replace(escapedOutput, me =>
|
|
{
|
|
var str = me.Groups["code"].Value;
|
|
var newString = YamlHelper.UnescapeUnicodeCodePoint(str);
|
|
return newString;
|
|
});
|
|
return output;
|
|
}
|
|
|
|
public T Deserialize<T>(string data)
|
|
=> _deserializer.Deserialize<T>(data);
|
|
} |