forked from ScoopInstaller/Scoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.ps1
210 lines (178 loc) · 6.39 KB
/
json.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Convert objects to pretty json
# Only needed until PowerShell ConvertTo-Json will be improved https://github.com/PowerShell/PowerShell/issues/2736
# https://github.com/PowerShell/PowerShell/issues/2736 was fixed in pwsh
# Still needed in normal powershell
function ConvertToPrettyJson {
[CmdletBinding()]
Param (
[Parameter(Mandatory, ValueFromPipeline)]
$data
)
Process {
$data = normalize_values $data
# convert to string
[String]$json = $data | ConvertTo-Json -Depth 8 -Compress
[String]$output = ''
# state
[String]$buffer = ''
[Int]$depth = 0
[Bool]$inString = $false
# configuration
[String]$indent = ' ' * 4
[Bool]$unescapeString = $true
[String]$eol = "`r`n"
for ($i = 0; $i -lt $json.Length; $i++) {
# read current char
$buffer = $json.Substring($i, 1)
$objectStart = !$inString -and $buffer.Equals('{')
$objectEnd = !$inString -and $buffer.Equals('}')
$arrayStart = !$inString -and $buffer.Equals('[')
$arrayEnd = !$inString -and $buffer.Equals(']')
$colon = !$inString -and $buffer.Equals(':')
$comma = !$inString -and $buffer.Equals(',')
$quote = $buffer.Equals('"')
$escape = $buffer.Equals('\')
if ($quote) {
$inString = !$inString
}
# skip escape sequences
if ($escape) {
$buffer = $json.Substring($i, 2)
++$i
# Unescape unicode
if ($inString -and $unescapeString) {
if ($buffer.Equals('\n')) {
$buffer = "`n"
} elseif ($buffer.Equals('\r')) {
$buffer = "`r"
} elseif ($buffer.Equals('\t')) {
$buffer = "`t"
} elseif ($buffer.Equals('\u')) {
$buffer = [regex]::Unescape($json.Substring($i - 1, 6))
$i += 4
}
}
$output += $buffer
continue
}
# indent / outdent
if ($objectStart -or $arrayStart) {
++$depth
} elseif ($objectEnd -or $arrayEnd) {
--$depth
$output += $eol + ($indent * $depth)
}
# add content
$output += $buffer
# add whitespace and newlines after the content
if ($colon) {
$output += ' '
} elseif ($comma -or $arrayStart -or $objectStart) {
$output += $eol
$output += $indent * $depth
}
}
return $output
}
}
function json_path([String] $json, [String] $jsonpath, [Hashtable] $substitutions) {
Add-Type -Path "$psscriptroot\..\supporting\validator\bin\Newtonsoft.Json.dll"
if ($null -ne $substitutions) {
$jsonpath = substitute $jsonpath $substitutions ($jsonpath -like "*=~*")
}
try {
$obj = [Newtonsoft.Json.Linq.JObject]::Parse($json)
} catch [Newtonsoft.Json.JsonReaderException] {
try {
$obj = [Newtonsoft.Json.Linq.JArray]::Parse($json)
} catch [Newtonsoft.Json.JsonReaderException] {
return $null
}
}
try {
try {
$result = $obj.SelectToken($jsonpath, $true)
} catch [Newtonsoft.Json.JsonException] {
return $null
}
return $result.ToString()
} catch [System.Management.Automation.MethodInvocationException] {
write-host -f DarkRed $_
return $null
}
return $null
}
function json_path_legacy([String] $json, [String] $jsonpath, [Hashtable] $substitutions) {
$result = $json | ConvertFrom-Json -ea stop
$isJsonPath = $jsonpath.StartsWith('$')
$jsonpath.split('.') | ForEach-Object {
$el = $_
# substitute the basename and version varibales into the jsonpath
if ($null -ne $substitutions) {
$el = substitute $el $substitutions
}
# skip $ if it's jsonpath format
if ($el -eq '$' -and $isJsonPath) {
return
}
# array detection
if ($el -match '^(?<property>\w+)?\[(?<index>\d+)\]$') {
$property = $matches['property']
if ($property) {
$result = $result.$property[$matches['index']]
} else {
$result = $result[$matches['index']]
}
return
}
$result = $result.$el
}
return $result
}
function normalize_values([psobject] $json) {
# Iterate Through Manifest Properties
$json.PSObject.Properties | ForEach-Object {
# Recursively edit psobjects
# If the values is psobjects, its not normalized
# For example if manifest have architecture and it's architecture have array with single value it's not formatted.
# @see https://github.com/lukesampson/scoop/pull/2642#issue-220506263
if ($_.Value -is [System.Management.Automation.PSCustomObject]) {
$_.Value = normalize_values $_.Value
}
# Process String Values
if ($_.Value -is [String]) {
# Split on new lines
[Array] $parts = ($_.Value -split '\r?\n').Trim()
# Replace with string array if result is multiple lines
if ($parts.Count -gt 1) {
$_.Value = $parts
}
}
# Convert single value array into string
if ($_.Value -is [Array]) {
# Array contains only 1 element String or Array
if ($_.Value.Count -eq 1) {
# Array
if ($_.Value[0] -is [Array]) {
$_.Value = $_.Value
} else {
# String
$_.Value = $_.Value[0]
}
} else {
# Array of Arrays
$resulted_arrs = @()
foreach ($element in $_.Value) {
if ($element.Count -eq 1) {
$resulted_arrs += $element
} else {
$resulted_arrs += , $element
}
}
$_.Value = $resulted_arrs
}
}
# Process other values as needed...
}
return $json
}