53 lines
1.9 KiB
C#
53 lines
1.9 KiB
C#
using EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue.Models;
|
||
using System.Text;
|
||
|
||
namespace EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue;
|
||
|
||
/// <summary>
|
||
/// Provides functionality to serialize key–value groups into a text-based format.
|
||
/// </summary>
|
||
public class KeyValueSerializer
|
||
{
|
||
/// <summary>
|
||
/// Serializes the given key–value group into a text format.
|
||
/// </summary>
|
||
/// <param name="group">The key–value group to serialize. Cannot be null.</param>
|
||
/// <returns>A string representing the serialized form of the key–value 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");
|
||
}
|
||
} |