Skip to content

Commit

Permalink
json: catch JsonReaderException
Browse files Browse the repository at this point in the history
  • Loading branch information
r15ch13 committed Sep 14, 2018
1 parent 091ddf5 commit fb58e92
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/json.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,21 @@ Function ConvertToPrettyJson {
function json_path([String] $json, [String] $jsonpath, [String] $basename) {
Add-Type -Path "$psscriptroot\..\supporting\validator\bin\Newtonsoft.Json.dll"
$jsonpath = $jsonpath.Replace("`$basename", $basename)
$obj = [Newtonsoft.Json.Linq.JObject]::Parse($json)
$result = $null
try {
$obj = [Newtonsoft.Json.Linq.JObject]::Parse($json)
} catch [Newtonsoft.Json.JsonReaderException] {
return $null
}

try {
$result = $obj.SelectToken($jsonpath, $true)
return $result.ToString()
} catch [System.Management.Automation.MethodInvocationException] {
write-host -f DarkRed $_
return $null
}
return $result.ToString()

return $null
}

function json_path_legacy([String] $json, [String] $jsonpath, [String] $basename) {
Expand Down

0 comments on commit fb58e92

Please sign in to comment.