Skip to content

Commit

Permalink
Issue #100: Make robust against invalid CustomCategories.json.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswendt1 committed Jul 21, 2022
1 parent 8a3f604 commit f5c7534
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions DocumentTranslation.GUI/Categories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public Categories()
Read();
}

private void Read(string filename = null)
private void Read()
{
string categoriesJson;
try
{
if (string.IsNullOrEmpty(filename)) filename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + AppName + Path.DirectorySeparatorChar + AppSettingsFileName;
string filename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + AppName + Path.DirectorySeparatorChar + AppSettingsFileName;
categoriesJson = File.ReadAllText(filename);
}
catch (Exception ex)
Expand All @@ -40,7 +40,15 @@ private void Read(string filename = null)
}
throw;
}
MyCategoryList = JsonSerializer.Deserialize<BindingList<MyCategory>>(categoriesJson, new JsonSerializerOptions { IncludeFields = true });
try
{
MyCategoryList = JsonSerializer.Deserialize<BindingList<MyCategory>>(categoriesJson, new JsonSerializerOptions { IncludeFields = true });
}
catch
{
MyCategoryList.Add(new MyCategory("Category 1", ""));
MyCategoryList.Add(new MyCategory("Category 2", ""));
}
}

public void Write(string filename = null)
Expand Down

0 comments on commit f5c7534

Please sign in to comment.