1
1
# Convert objects to pretty json
2
2
# Only needed until PowerShell ConvertTo-Json will be improved https://github.com/PowerShell/PowerShell/issues/2736
3
- Function ConvertToPrettyJson {
4
- [cmdletbinding ()]
3
+ # https://github.com/PowerShell/PowerShell/issues/2736 was fixed in pwsh
4
+ # Still needed in normal powershell
5
+
6
+ function ConvertToPrettyJson {
7
+ [CmdletBinding ()]
5
8
6
9
Param (
7
- [parameter (Mandatory , ValueFromPipeline )]
10
+ [Parameter (Mandatory , ValueFromPipeline )]
8
11
$data
9
12
)
10
13
11
- Process {
14
+ Process {
12
15
$data = normalize_values $data
13
16
14
17
# convert to string
15
18
[String ]$json = $data | ConvertTo-Json - Depth 8 - Compress
16
- [String ]$output = " "
19
+ [String ]$output = ' '
17
20
18
21
# state
19
- [String ]$buffer = " "
22
+ [String ]$buffer = ' '
20
23
[Int ]$depth = 0
21
24
[Bool ]$inString = $false
22
25
23
26
# configuration
24
- [String ]$indent = " " * 4
27
+ [String ]$indent = ' ' * 4
25
28
[Bool ]$unescapeString = $true
26
29
[String ]$eol = " `r`n "
27
30
28
31
for ($i = 0 ; $i -lt $json.Length ; $i ++ ) {
29
32
# read current char
30
33
$buffer = $json.Substring ($i , 1 )
31
34
32
- #
33
- $objectStart = ! $inString -and $buffer.Equals (" {" )
34
- $objectEnd = ! $inString -and $buffer.Equals (" }" )
35
- $arrayStart = ! $inString -and $buffer.Equals (" [" )
36
- $arrayEnd = ! $inString -and $buffer.Equals (" ]" )
37
- $colon = ! $inString -and $buffer.Equals (" :" )
38
- $comma = ! $inString -and $buffer.Equals (" ," )
35
+ $objectStart = ! $inString -and $buffer.Equals (' {' )
36
+ $objectEnd = ! $inString -and $buffer.Equals (' }' )
37
+ $arrayStart = ! $inString -and $buffer.Equals (' [' )
38
+ $arrayEnd = ! $inString -and $buffer.Equals (' ]' )
39
+ $colon = ! $inString -and $buffer.Equals (' :' )
40
+ $comma = ! $inString -and $buffer.Equals (' ,' )
39
41
$quote = $buffer.Equals (' "' )
40
42
$escape = $buffer.Equals (' \' )
41
43
@@ -79,20 +81,20 @@ Function ConvertToPrettyJson {
79
81
80
82
# add whitespace and newlines after the content
81
83
if ($colon ) {
82
- $output += " "
84
+ $output += ' '
83
85
} elseif ($comma -or $arrayStart -or $objectStart ) {
84
86
$output += $eol
85
87
$output += $indent * $depth
86
88
}
87
89
}
88
90
89
- $output
91
+ return $output
90
92
}
91
93
}
92
94
93
95
function json_path ([String ] $json , [String ] $jsonpath , [String ] $basename ) {
94
96
Add-Type - Path " $psscriptroot \..\supporting\validator\bin\Newtonsoft.Json.dll"
95
- $jsonpath = $jsonpath.Replace (" ` $ basename" , $basename )
97
+ $jsonpath = $jsonpath.Replace (' $basename' , $basename )
96
98
try {
97
99
$obj = [Newtonsoft.Json.Linq.JObject ]::Parse($json )
98
100
} catch [Newtonsoft.Json.JsonReaderException ] {
@@ -112,24 +114,24 @@ function json_path([String] $json, [String] $jsonpath, [String] $basename) {
112
114
113
115
function json_path_legacy ([String ] $json , [String ] $jsonpath , [String ] $basename ) {
114
116
$result = $json | ConvertFrom-Json - ea stop
115
- $isJsonPath = $jsonpath.StartsWith (" `$ " )
116
- $jsonpath.split (" . " ) | ForEach-Object {
117
+ $isJsonPath = $jsonpath.StartsWith (' $ ' )
118
+ $jsonpath.split (' . ' ) | ForEach-Object {
117
119
$el = $_
118
120
119
121
# substitute the base filename into the jsonpath
120
- if ($el.Contains (" ` $ basename" )) {
121
- $el = $el.Replace (" ` $ basename" , $basename )
122
+ if ($el.Contains (' $basename' )) {
123
+ $el = $el.Replace (' $basename' , $basename )
122
124
}
123
125
124
126
# skip $ if it's jsonpath format
125
- if ($el -eq " `$ " -and $isJsonPath ) {
127
+ if ($el -eq ' $ ' -and $isJsonPath ) {
126
128
return
127
129
}
128
130
129
131
# array detection
130
- if ($el -match " ^(?<property>\w+)?\[(?<index>\d+)\]$" ) {
132
+ if ($el -match ' ^(?<property>\w+)?\[(?<index>\d+)\]$' ) {
131
133
$property = $matches [' property' ]
132
- if ($property ) {
134
+ if ($property ) {
133
135
$result = $result .$property [$matches [' index' ]]
134
136
} else {
135
137
$result = $result [$matches [' index' ]]
@@ -146,19 +148,58 @@ function normalize_values([psobject] $json) {
146
148
# Iterate Through Manifest Properties
147
149
$json.PSObject.Properties | ForEach-Object {
148
150
151
+ $value = $_.Value
152
+
153
+ # Recursively edit psobjects
154
+ # If the values is psobjects, its not normalized
155
+ # For example if manifest have architecture and it's architecture have array with single value it's not formatted.
156
+ # @see https://github.com/lukesampson/scoop/pull/2642#issue-220506263
157
+ if ($value -is [System.Management.Automation.PSCustomObject ]) {
158
+ $value = normalize_values $value
159
+ }
160
+
149
161
# Process String Values
150
- if ($_ .Value -is [string ]) {
162
+ if ($value -is [String ]) {
151
163
152
164
# Split on new lines
153
- [array ] $parts = ($_ .Value -split ' \r?\n' ).Trim()
165
+ [Array ] $parts = ($value -split ' \r?\n' ).Trim()
154
166
155
167
# Replace with string array if result is multiple lines
156
168
if ($parts.Count -gt 1 ) {
157
- $_.Value = $parts
169
+ $value = $parts
170
+ }
171
+ }
172
+
173
+ # Convert single value array into string
174
+ if ($value -is [Array ]) {
175
+ # Array contains only 1 element String or Array
176
+ if ($value.Count -eq 1 ) {
177
+ # Array
178
+ if ($value [0 ] -is [Array ]) {
179
+ $value = $value
180
+ } else {
181
+ # String
182
+ $value = $value [0 ]
183
+ }
184
+ } else {
185
+ # Array of Arrays
186
+ $resulted_arrs = @ ()
187
+ foreach ($element in $value ) {
188
+ if ($element.Count -eq 1 ) {
189
+ $resulted_arrs += $element
190
+ } else {
191
+ $resulted_arrs += , $element
192
+ }
193
+ }
194
+
195
+ $value = $resulted_arrs
158
196
}
159
197
}
160
198
161
199
# Process other values as needed...
200
+
201
+ # Bind edited values into original
202
+ $_.Value = $value
162
203
}
163
204
164
205
return $json
0 commit comments