using EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue.Models;
namespace EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue;
///
/// Provides static methods for deserializing text representations of key-value groups into KvGroup objects.
///
public static class KeyValueDeserializer
{
///
/// Deserializes the specified text into a KvGroup object.
///
/// The input string containing the serialized representation of a KvGroup. Cannot be null.
/// A KvGroup object that represents the data contained in the input text.
public static KeyValueGroup Deserialize(string text)
{
var tokenizer = new KeyValueTokenizer(text);
return ParseGroup(tokenizer);
}
///
/// Parses a group from the provided tokenizer, including any nested groups or key-value pairs.
///
/// The tokenizer used to read group names, symbols, and key-value pairs from the input stream. Must not be null and must be positioned at the start of a group.
/// The optional name of the group to parse. If null, the group name is read from the tokenizer.
/// A KvGroup representing the parsed group, including all nested groups and key-value pairs.
private static KeyValueGroup ParseGroup(KeyValueTokenizer t, string? k = null)
{
// Expect group name
var groupName = k ?? t.ReadString();
var group = new KeyValueGroup(groupName);
t.ReadSymbol('{');
while (!t.PeekSymbol('}'))
{
var key = t.ReadString();
if (t.PeekSymbol('{'))
{
// Nested group
group.Nodes.Add(ParseGroup(t, key));
}
else
{
// Key-value pair
var value = t.ReadString();
group.Nodes.Add(new Models.KeyValuePair(key, value));
}
}
t.ReadSymbol('}');
return group;
}
}