using EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue.Models;
using System.Text;
namespace EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue;
///
/// Provides functionality to serialize key–value groups into a text-based format.
///
public class KeyValueSerializer
{
///
/// Serializes the given key–value group into a text format.
///
/// The key–value group to serialize. Cannot be null.
/// A string representing the serialized form of the key–value group.
public static string Serialize(KeyValueGroup group)
{
var sb = new StringBuilder();
SerializeGroup(group, sb, 0);
return sb.ToString();
}
///
/// Serializes the specified key-value group and its child elements into a formatted string representation, appending the result to the provided StringBuilder.
///
/// The key-value group to serialize. Cannot be null.
/// The StringBuilder to which the serialized output is appended. Cannot be null.
/// The indentation level to apply to the serialized output.
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");
}
}