Files
2026-05-31 11:06:50 +02:00

53 lines
1.9 KiB
C#
Raw Permalink 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 EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue.Models;
using System.Text;
namespace EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue;
/// <summary>
/// Provides functionality to serialize keyvalue groups into a text-based format.
/// </summary>
public class KeyValueSerializer
{
/// <summary>
/// Serializes the given keyvalue group into a text format.
/// </summary>
/// <param name="group">The keyvalue group to serialize. Cannot be null.</param>
/// <returns>A string representing the serialized form of the keyvalue group.</returns>
public static string Serialize(KeyValueGroup group)
{
var sb = new StringBuilder();
SerializeGroup(group, sb, 0);
return sb.ToString();
}
/// <summary>
/// Serializes the specified key-value group and its child elements into a formatted string representation, appending the result to the provided StringBuilder.
/// </summary>
/// <param name="group">The key-value group to serialize. Cannot be null.</param>
/// <param name="sb">The StringBuilder to which the serialized output is appended. Cannot be null.</param>
/// <param name="indent">The indentation level to apply to the serialized output.</param>
private static void SerializeGroup(KeyValueGroup group, StringBuilder sb, int indent)
{
const char padChar = '\t';
var pad = new string(padChar, indent);
sb.Append($"{pad}\"{group.Key}\"\n");
sb.Append($"{pad}{{\n");
foreach (var node in group.Nodes)
{
switch (node)
{
case Models.KeyValuePair pair:
sb.Append($"{pad}{padChar}\"{pair.Key}\"{padChar}{padChar}\"{pair.Value}\"\n");
break;
case KeyValueGroup childGroup:
SerializeGroup(childGroup, sb, indent + 1);
break;
}
}
sb.Append($"{pad}}}\n");
}
}