Im reading a json file to a dictionary
{
"auths": {
"https://www.google.es": {
"uri": "https://www.google.es"
}
}
}
having the POCO class
public class MyClass
{
[ConfigurationProperty("auths", IsRequired = false)]
public Dictionary<string, OtherObject> Auths { get; set; }
}
When i load it using ConfigurationRoot like
public MyClass Read()
{
IConfigurationRoot configuration =
new ConfigurationBuilder()
.AddJsonFile("path_to_json_file", optional: true, reloadOnChange: true)
.Build();
// Try Parse and return (if will exception in case of binding error)
var _settings= new MyClass();
configuration.Bind(_settings);
return _settings;
}
The issue is that the Dictionary item contains https only on the Key property. Looks like it's ignoring or having issues with https://.....
Not sure if it's an intended feature or a bug.
Steps to reproduce the behavior:
Dictionary Key Item should contains https://www.google.es

That seems deliberate to me. The configuration system flattens the key hierarchy to a string with colons as delimiters so it becomes "auths:https://www.google.es:uri", and binding then cannot know where the boundaries were originally.
It is documented in Hierarchical configuration data.
That makes sense, even if i don't agree on using : as hierarchy... but, i will try to look into the code to see if the separator char can be changed / configured somewhere.
Thanks for the reply i completely missed that point !
The separator char does not look easy to change at run time: https://github.com/dotnet/extensions/blob/1774c15ac24c65513fa9fc1f4fbb69be9a2a4e25/src/Configuration/Config.Abstractions/src/ConfigurationPath.cs#L14-L17
By the way, I don't think the Microsoft.Extensions.Configuration.* packages will care about [ConfigurationProperty("auths", IsRequired = false)] in your POCO class.
Im using the ConfigProperty due other reasons.
About the limiter... i will need to use JsonSerializer :
Can you use some other strings as keys? For example:
{
"auths": {
"es": {
"uri": "https://www.google.es"
}
}
}
Then register an IPostConfigureOptions<MyClass> implementation that reads the Dictionary<string, OtherObject> Auths property that was populated from configuration binding, and copies the objects to a separate dictionary with the keys that you really want. The "es" key would be used only in configuration files or environment variables, not for looking up OtherObject instances later.
Well, not in this case, i need to use the Uri as key. But anyway, i can do exactly the same using the JsonSerializer with a custom serializer, and works well.
Without the need of : in the key, the classical approach works pretty well.
Thank you for your help, i think we can close the issue ?
Tagging subscribers to this area: @maryamariyan
See info in area-owners.md if you want to be subscribed.
Maybe we want to have an escape sequence for json config so this is allowed? Or a flag to disable the ':' behavior just for Json?
If any change is made, we should port it to the Newtonsoft.Json config provider in Extensions.
Looks like this issue has been encountered previously, e.g. https://github.com/aspnet/Configuration/issues/792 https://github.com/dotnet/extensions/issues/782. Sounds like something we should address instead of our usual answer that : is a reserved character and this issue is by design.
Most helpful comment
Looks like this issue has been encountered previously, e.g. https://github.com/aspnet/Configuration/issues/792 https://github.com/dotnet/extensions/issues/782. Sounds like something we should address instead of our usual answer that
:is a reserved character and this issue is by design.