-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Escape non-ascii characters in json payloads.
Even if the content type indicates a valid unicode charset, Zendesk does like non-ascii characters that aren't escaped. PS 6.2 added the `-EscapeHanding` parameter to `ConvertTo-Json` which can be used.
- Loading branch information
Showing
4 changed files
with
2,029 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
function ConvertTo-UnicodeEscape { | ||
<# | ||
.SYNOPSIS | ||
Escapes non-ascii characters in a given string | ||
.DESCRIPTION | ||
Escapes non-ascii characters in a given string | ||
.EXAMPLE | ||
PS C:\> 'A Náme' | ConvertTo-UnicodeEscape | ||
Escapes the `á` to json compliant `\u00E1` | ||
.EXAMPLE | ||
PS C:\> $data | ConvertTo-Json -Compress | ConvertTo-UnicodeEscape | ||
Converts `$data` to json and escapes any non ascii characters | ||
#> | ||
[CmdletBinding()] | ||
Param ( | ||
# String to escape | ||
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | ||
[String] | ||
$InputObject | ||
) | ||
|
||
$output = '' | ||
|
||
foreach ($char in $InputObject.GetEnumerator()) { | ||
$i = [int]$char | ||
|
||
if ($i -lt 128) { | ||
Write-Debug -Message "$char ($i) does not need escaping." | ||
$output += $char | ||
} else { | ||
Write-Debug -Message "$char ($i) needs escaping." | ||
|
||
$hex = '{0:X}' -f $i | ||
Write-Debug -Message "Character as hex: $hex" | ||
|
||
$escape = '\u' + $hex.PadLeft(4, '0') | ||
Write-Debug -Message "Full escape sequence: $escape" | ||
|
||
$output += $escape | ||
} | ||
} | ||
|
||
$output | ||
} |
Oops, something went wrong.