Skip to content

Commit 1ba8c29

Browse files
committed
Fix Json builder when file is not found.
1 parent 1d2ef3b commit 1ba8c29

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

src/Json/SimpleJsonConfigBuilder.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public override void Initialize(string name, NameValueCollection config)
3131
{
3232
base.Initialize(name, config);
3333

34+
_allSettings = new Dictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
35+
3436
// IgnoreMissingFile
3537
bool ignoreMissing;
3638
IgnoreMissingFile = (Boolean.TryParse(config?[ignoreMissingFileTag], out ignoreMissing)) ? ignoreMissing : true;
@@ -42,8 +44,15 @@ public override void Initialize(string name, NameValueCollection config)
4244
throw new ArgumentException($"SimpleJsonConfigBuilder '{name}': Json file must be specified with the '{jsonFileTag}' attribute.");
4345
}
4446
JsonFile = Utils.MapPath(jsonFile);
45-
if (!IgnoreMissingFile && !File.Exists(JsonFile))
47+
if (!File.Exists(JsonFile))
4648
{
49+
if (IgnoreMissingFile)
50+
{
51+
// This empty dictionary allows us to effectively no-op any attempt to get values.
52+
_allSettings[""] = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
53+
return;
54+
}
55+
4756
throw new ArgumentException($"SimpleJsonConfigBuilder '{name}': Json file does not exist.");
4857
}
4958

@@ -56,7 +65,6 @@ public override void Initialize(string name, NameValueCollection config)
5665

5766

5867
// Now load up all the data for easy referencing later
59-
_allSettings = new Dictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
6068
JObject root;
6169
using (JsonTextReader jtr = new JsonTextReader(new StreamReader(JsonFile))) {
6270
root = JObject.Load(jtr);

0 commit comments

Comments
 (0)