Skip to content

Commit

Permalink
Move JSON parsing under try in order to ensure invalid JSON is handled
Browse files Browse the repository at this point in the history
  • Loading branch information
terrajobst committed Nov 30, 2022
1 parent cb35f8c commit c35328a
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/git-istage/Services/KeyBindingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ public string GetUserKeyBindingsPath()
ReadCommentHandling = JsonCommentHandling.Skip
};

// The built-in dictionary deserialization doesn't like it when it finds a $schema key.
// Since the schema is only useful for editors, we just remove it before attempting to
// deserialize.

if (JsonNode.Parse(content) is JsonObject jsonObject)
{
if (jsonObject.Remove("$schema"))
content = jsonObject.ToJsonString();
}

try
{
// The built-in dictionary deserialization doesn't like it when it finds a $schema key.
// Since the schema is only useful for editors, we just remove it before attempting to
// deserialize.

if (JsonNode.Parse(content) is JsonObject jsonObject)
{
if (jsonObject.Remove("$schema"))
content = jsonObject.ToJsonString();
}

return JsonSerializer.Deserialize<Dictionary<string, CustomKeyBinding?>>(content, options);
}
catch (JsonException ex)
Expand Down

0 comments on commit c35328a

Please sign in to comment.