mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 20:56:02 +00:00
ConvertTo-Json doesn't allow $null to be passed in even though it can be valid resulting json. Fix is to remove the check that the input is not null. NewtonSoft.Json handles it just fine.
73 lines
3.5 KiB
PowerShell
73 lines
3.5 KiB
PowerShell
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
# Licensed under the MIT License.
|
|
Describe 'ConvertTo-Json' -tags "CI" {
|
|
BeforeAll {
|
|
$newline = [System.Environment]::NewLine
|
|
}
|
|
|
|
It 'Newtonsoft.Json.Linq.Jproperty should be converted to Json properly' {
|
|
$EgJObject = New-Object -TypeName Newtonsoft.Json.Linq.JObject
|
|
$EgJObject.Add("TestValue1", "123456")
|
|
$EgJObject.Add("TestValue2", "78910")
|
|
$EgJObject.Add("TestValue3", "99999")
|
|
$dict = @{}
|
|
$dict.Add('JObject', $EgJObject)
|
|
$dict.Add('StrObject', 'This is a string Object')
|
|
$properties = @{'DictObject' = $dict; 'RandomString' = 'A quick brown fox jumped over the lazy dog'}
|
|
$object = New-Object -TypeName psobject -Property $properties
|
|
$jsonFormat = ConvertTo-Json -InputObject $object
|
|
$jsonFormat | Should -Match '"TestValue1": 123456'
|
|
$jsonFormat | Should -Match '"TestValue2": 78910'
|
|
$jsonFormat | Should -Match '"TestValue3": 99999'
|
|
}
|
|
|
|
It "StopProcessing should succeed" -Pending:$true {
|
|
$ps = [PowerShell]::Create()
|
|
$null = $ps.AddScript({
|
|
$obj = [PSCustomObject]@{P1 = ''; P2 = ''; P3 = ''; P4 = ''; P5 = ''; P6 = ''}
|
|
$obj.P1 = $obj.P2 = $obj.P3 = $obj.P4 = $obj.P5 = $obj.P6 = $obj
|
|
1..100 | Foreach-Object { $obj } | ConvertTo-Json -Depth 10 -Verbose
|
|
# the conversion is expected to take some time, this throw is in case it doesn't
|
|
throw "Should not have thrown exception"
|
|
})
|
|
$null = $ps.BeginInvoke()
|
|
# wait for verbose message from ConvertTo-Json to ensure cmdlet is processing
|
|
Wait-UntilTrue { $ps.Streams.Verbose.Count -gt 0 } | Should -BeTrue
|
|
$null = $ps.BeginStop($null, $null)
|
|
# wait a bit to ensure state has changed, not using synchronous Stop() to avoid blocking Pester
|
|
Start-Sleep -Milliseconds 100
|
|
$ps.InvocationStateInfo.State | Should -BeExactly "Stopped"
|
|
$ps.Dispose()
|
|
}
|
|
|
|
It "The result string is packed in an array symbols when AsArray parameter is used." {
|
|
$output = 1 | ConvertTo-Json -AsArray
|
|
$output | Should -BeLike "``[*1*]"
|
|
|
|
$output = 1,2 | ConvertTo-Json -AsArray
|
|
$output | Should -BeLike "``[*1*2*]"
|
|
}
|
|
|
|
It "The result string is not packed in the array symbols when there is only one input object and AsArray parameter is not used." {
|
|
$output = 1 | ConvertTo-Json
|
|
$output | Should -BeExactly '1'
|
|
}
|
|
|
|
It "The result string should <Name>." -TestCases @(
|
|
@{name = "be not escaped by default."; params = @{}; expected = "{$newline ""abc"": ""'def'""$newline}" }
|
|
@{name = "be not escaped with '-EscapeHandling Default'."; params = @{EscapeHandling = 'Default'}; expected = "{$newline ""abc"": ""'def'""$newline}" }
|
|
@{name = "be escaped with '-EscapeHandling EscapeHtml'."; params = @{EscapeHandling = 'EscapeHtml'}; expected = "{$newline ""abc"": ""\u0027def\u0027""$newline}" }
|
|
) {
|
|
param ($name, $params ,$expected)
|
|
|
|
@{ 'abc' = "'def'" } | ConvertTo-Json @params | Should -BeExactly $expected
|
|
}
|
|
|
|
It "Should handle null" {
|
|
[pscustomobject] @{ prop=$null } | ConvertTo-Json -Compress | Should -BeExactly '{"prop":null}'
|
|
$null | ConvertTo-Json -Compress | Should -Be 'null'
|
|
ConvertTo-Json -Compress $null | Should -Be 'null'
|
|
1, $null, 2 | ConvertTo-Json -Compress | Should -Be '[1,null,2]'
|
|
}
|
|
}
|