From 82ec0fe7a11c1fc0db39cb51b671a5650c1252c8 Mon Sep 17 00:00:00 2001 From: Richard Kuhnt Date: Sun, 10 Sep 2017 17:44:44 +0200 Subject: [PATCH] Use Newtonsoft.Json for JSONPath querys --- bin/checkver.ps1 | 5 ++++- lib/autoupdate.ps1 | 6 ++++-- lib/json.ps1 | 19 +++++++++++++++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/bin/checkver.ps1 b/bin/checkver.ps1 index 1f86ba873c..5b4d20faed 100644 --- a/bin/checkver.ps1 +++ b/bin/checkver.ps1 @@ -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 diff --git a/lib/autoupdate.ps1 b/lib/autoupdate.ps1 index 5e69a994da..cfdbf6ba61 100644 --- a/lib/autoupdate.ps1 +++ b/lib/autoupdate.ps1 @@ -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 } diff --git a/lib/json.ps1 b/lib/json.ps1 index fa697dcc2e..fb7e6ecc7d 100644 --- a/lib/json.ps1 +++ b/lib/json.ps1 @@ -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 = $_