Skip to content

Commit

Permalink
Use Newtonsoft.Json for JSONPath querys
Browse files Browse the repository at this point in the history
  • Loading branch information
r15ch13 committed Sep 10, 2017
1 parent 99820e8 commit 82ec0fe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 4 additions & 1 deletion bin/checkver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ while($in_progress -gt 0) {
}

if($jsonpath) {
$ver = json_path ($page | ConvertFrom-Json -ea stop) $jsonpath
$ver = json_path $page $jsonpath
if(!$ver) {
$ver = json_path_legacy $page $jsonpath
}
if(!$ver) {
write-host -f darkred "couldn't find '$jsonpath' in $url"
continue
Expand Down
6 changes: 4 additions & 2 deletions lib/autoupdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ function find_hash_in_json([String] $url, [String] $basename, [String] $jsonpath
try {
$wc = new-object net.webclient
$wc.headers.add('Referer', (strip_filename $url))
$json = $wc.downloadstring($url) | convertfrom-json -ea stop
$json = $wc.downloadstring($url)
} catch [system.net.webexception] {
write-host -f darkred $_
write-host -f darkred "URL $url is not valid"
return
}

$hash = json_path $json $jsonpath $basename
if(!$hash) {
$hash = json_path_legacy $json $jsonpath $basename
}
return format_hash $hash
}

Expand Down
19 changes: 17 additions & 2 deletions lib/json.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,23 @@ Function ConvertToPrettyJson {
}
}

function json_path([Object] $json, [String] $jsonpath, [String] $basename) {
$result = $json
Add-Type -Path "$psscriptroot\..\supporting\validator\Newtonsoft.Json.dll"

function json_path([String] $json, [String] $jsonpath, [String] $basename) {
$jsonpath = $jsonpath.Replace("`$basename", $basename)
$obj = [Newtonsoft.Json.Linq.JObject]::Parse($json)
$result = $null
try {
$result = $obj.SelectToken($jsonpath, $true)
} catch [System.Management.Automation.MethodInvocationException] {
write-host -f DarkRed $_
return $null
}
return $result.ToString()
}

function json_path_legacy([String] $json, [String] $jsonpath, [String] $basename) {
$result = $json | ConvertFrom-Json -ea stop
$isJsonPath = $jsonpath.StartsWith("`$")
$jsonpath.split(".") | ForEach-Object {
$el = $_
Expand Down

0 comments on commit 82ec0fe

Please sign in to comment.