-
-
Notifications
You must be signed in to change notification settings - Fork 492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can we get all the values of available keys within Yaml file which has deep hierarchy. Also, If I just need to get all the keys available. #686
Comments
When you deserialize the yaml without specifying the object type it looks like it comes back as a Here's some code that will dump out all of the keys in a yaml file of an arbitrary format: using YamlDotNet.Benchmark;
using YamlDotNet.Serialization;
var deserializer = new DeserializerBuilder().Build();
var o = deserializer.Deserialize(new StringReader(@"
a:
b:
hello: world
c:
i: am
a: martian
"))!;
var allKeys = GetKeys((Dictionary<object, object>)o, string.Empty);
foreach (var key in allKeys)
{
Console.WriteLine(key);
}
string[] GetKeys(Dictionary<object, object> values, string path)
{
var keys = new List<string>();
foreach (var kvp in values)
{
if (kvp.Value is Dictionary<object, object> x)
{
keys.AddRange(GetKeys((Dictionary<object, object>)kvp.Value, $"{path}:{kvp.Key}"));
}
else
{
keys.Add($"{path}:{kvp.Key}={kvp.Value}");
}
}
return keys.ToArray();
} Result:
Hopefully that helps. |
Closing. Reopen this if you still need help. |
Is it possible to make it |
The key doesn’t have to be a string. It can be an actual object as well. The yaml spec doesn’t require it to be a scalar. |
Oh, I understand. And is there a way to manually configure a particular deserializer to convert the Edit: it seems that a custom INodeDeserializer registered instead of DictionaryNodeDeserializer is the solution. Thank you guys for this great library! |
Reference (and slight correction) for this: #332 (comment) |
If I do not have idea about how many keys or with what hierarchy keys could be present in YAML , is it possible to write a generic method which fetches all the keys and values available with in yaml file given of any structure.
The text was updated successfully, but these errors were encountered: