Skip to content

Commit f5c7534

Browse files
committed
Issue #100: Make robust against invalid CustomCategories.json.
1 parent 8a3f604 commit f5c7534

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

DocumentTranslation.GUI/Categories.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public Categories()
2222
Read();
2323
}
2424

25-
private void Read(string filename = null)
25+
private void Read()
2626
{
2727
string categoriesJson;
2828
try
2929
{
30-
if (string.IsNullOrEmpty(filename)) filename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + AppName + Path.DirectorySeparatorChar + AppSettingsFileName;
30+
string filename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + AppName + Path.DirectorySeparatorChar + AppSettingsFileName;
3131
categoriesJson = File.ReadAllText(filename);
3232
}
3333
catch (Exception ex)
@@ -40,7 +40,15 @@ private void Read(string filename = null)
4040
}
4141
throw;
4242
}
43-
MyCategoryList = JsonSerializer.Deserialize<BindingList<MyCategory>>(categoriesJson, new JsonSerializerOptions { IncludeFields = true });
43+
try
44+
{
45+
MyCategoryList = JsonSerializer.Deserialize<BindingList<MyCategory>>(categoriesJson, new JsonSerializerOptions { IncludeFields = true });
46+
}
47+
catch
48+
{
49+
MyCategoryList.Add(new MyCategory("Category 1", ""));
50+
MyCategoryList.Add(new MyCategory("Category 2", ""));
51+
}
4452
}
4553

4654
public void Write(string filename = null)

0 commit comments

Comments
 (0)