Amend reading config

This commit is contained in:
TheXamlGuy
2024-04-21 19:53:47 +01:00
parent e0f49832f8
commit 21481a82d8
+18 -3
View File
@@ -135,11 +135,26 @@ public class ConfigurationSource<TConfiguration>(IConfigurationFile<TConfigurati
using StreamReader? reader = new(stream); using StreamReader? reader = new(stream);
string? content = reader.ReadToEnd(); string? content = reader.ReadToEnd();
JsonNode? rootNode = JsonNode.Parse(content);
using JsonDocument jsonDocument = JsonDocument.Parse(content); string[] segments = section.Split(':');
if (jsonDocument.RootElement.TryGetProperty(section, out JsonElement sectionValue)) JsonNode? currentNode = rootNode;
int lastIndex = segments.Length - 1;
for (int i = 0; i < lastIndex; i++)
{ {
value = JsonSerializer.Deserialize<TConfiguration>(sectionValue.ToString(), serializerOptions ?? defaultSerializerOptions()); if (currentNode is null)
{
value = default;
return false;
}
currentNode = currentNode[segments[i]];
}
if (currentNode is not null)
{
value = JsonSerializer.Deserialize<TConfiguration>(currentNode[segments[lastIndex]], serializerOptions ?? defaultSerializerOptions());
return true; return true;
} }
} }