@@ -3,25 +3,25 @@ function Format-Json
3
3
<#
4
4
. SYNOPSIS
5
5
Applies proper formatting to a JSON string with the specified indentation.
6
-
6
+
7
7
. DESCRIPTION
8
- The `Format-Json` function takes a JSON string as input and formats it with the specified level of indentation.
8
+ The `Format-Json` function takes a JSON string as input and formats it with the specified level of indentation.
9
9
The function processes each line of the JSON string, adjusting the indentation level based on the structure of the JSON.
10
-
10
+
11
11
. PARAMETER Json
12
12
The JSON string to be formatted.
13
13
This parameter is mandatory and accepts input from the pipeline.
14
-
14
+
15
15
. PARAMETER Indentation
16
16
Specifies the number of spaces to use for each indentation level.
17
- The value must be between 1 and 1024.
17
+ The value must be between 1 and 1024.
18
18
The default value is 2.
19
-
19
+
20
20
. EXAMPLE
21
21
$formattedJson = Get-Content -Path 'config.json' | Format-Json -Indentation 4
22
- This example reads the JSON content from a file named 'config.json', formats it with an
22
+ This example reads the JSON content from a file named 'config.json', formats it with an
23
23
indentation level of 4 spaces, and stores the result in the `$formattedJson` variable.
24
-
24
+
25
25
. EXAMPLE
26
26
@'
27
27
{
@@ -33,40 +33,40 @@ function Format-Json
33
33
}
34
34
'@ | Format-Json
35
35
This example formats an inline JSON string with the default indentation level of 2 spaces.
36
-
36
+
37
37
. NOTES
38
38
This function assumes that the input string is valid JSON.
39
39
#>
40
40
param
41
41
(
42
42
[Parameter (Mandatory = $true , ValueFromPipeline = $true )]
43
43
[String ]$Json ,
44
-
44
+
45
45
[ValidateRange (1 , 1024 )]
46
46
[Int ]$Indentation = 2
47
47
)
48
-
48
+
49
49
$lines = $Json -split ' \n'
50
-
50
+
51
51
$indentLevel = 0
52
-
52
+
53
53
$result = $lines | ForEach-Object `
54
54
{
55
55
if ($_ -match " [\}\]]" )
56
56
{
57
57
$indentLevel --
58
58
}
59
-
59
+
60
60
$line = (' ' * $indentLevel * $Indentation ) + $_.TrimStart ().Replace(" : " , " : " )
61
-
61
+
62
62
if ($_ -match " [\{\[]" )
63
63
{
64
64
$indentLevel ++
65
65
}
66
-
66
+
67
67
return $line
68
68
}
69
-
69
+
70
70
return $result -join " `n "
71
71
}
72
72
79
79
$jsonData = Get-Content $jsonFilePath - Raw | ConvertFrom-Json
80
80
$jsonData.Version = " $Env: APPVEYOR_BUILD_VERSION "
81
81
82
- $jsonData | ConvertTo-Json | Format-Json | Out-File $jsonFilePath
82
+ # $jsonData | ConvertTo-Json | Format-Json | Out-File $jsonFilePath -Encoding UTF8
83
+
84
+ $utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $false
85
+ $jsonContent = $jsonData | ConvertTo-Json | Format-Json
86
+ $jsonFilePath2 = [System.Environment ]::GetEnvironmentVariable(' APPVEYOR_BUILD_FOLDER' ) + " \" + [System.Environment ]::GetEnvironmentVariable(' APPVEYOR_PROJECT_NAME' ) + " \Meta.json"
87
+ [System.IO.File ]::WriteAllText($jsonFilePath2 , $jsonContent , $utf8NoBomEncoding )
83
88
84
89
Write-Host " OK" - ForegroundColor Green
85
90
0 commit comments