ps/Modules/Cole.PowerShell.Developer/Public/Write-JsonArrayNameValuePair.ps1
2023-05-30 22:51:22 -07:00

45 lines
1.7 KiB
PowerShell

function Write-JsonArrayNameValuePair {
param (
$JsonName,
$JsonValue,
$Depth = 0,
[switch]$OrderedKeys
)
$spacesString, $shortSpacesString = (Get-JsonStringLeadsByDepth -Depth $Depth)
$quoteString = '"'
$commaString = ","
if ($OrderedKeys) {
$JsonValue = $JsonValue | Sort-Object -Property Name,Key,Id
}
$stringBuilder = New-Object System.Text.StringBuilder
$stringBuilder.Append($shortSpacesString) | Out-Null
if (![string]::IsNullOrWhiteSpace($JsonName)) {
$stringBuilder.Append($quoteString).Append($JsonName).Append($quoteString).Append(" : ") | Out-Null
}
$stringBuilder.AppendLine("[") | Out-Null
foreach ($iter in $JsonValue) {
if ($iter.GetType().Name -match 'byte|short|int32|long|sbyte|ushort|uint32|ulong|float|double|decimal|boolean') {
# Write it without quotes
$stringBuilder.Append($spacesString).Append($iter).AppendLine($commaString) | Out-Null
} elseif ($iter -is [string]) {
# Write it with quotes
$stringBuilder.Append($spacesString).Append($quoteString).Append($iter).Append($quoteString).AppendLine($commaString) | Out-Null
} else {
# Must be a complex object, but without a name, so write the value
$evaluatedValue = (Write-JsonObject -JsonName $null -JsonValue $iter -Depth ($Depth + 1) -OrderedKeys:$OrderedKeys)
$stringBuilder.AppendLine($evaluatedValue) | Out-Null
}
}
$stringBuilder.Append($shortSpacesString) | Out-Null
$stringBuilder.Append("]") | Out-Null
$stringBuilder.Append($commaString) | Out-Null
$result = ($stringBuilder.ToString())
return $result
}