When I originally wrote up the proposal for -AsArray in #6327, I meant it to work like @(...):
That is, the input should be converted to an array (and serialized as such) _unless it already is one_.
Instead, -AsArray, as currently implemented, _unconditionally_ treats the input as an array.
With _pipeline_ input, which automatically gets enumerated, that usually makes no difference, but it does when you pass the inputs as an _argument_.
ConvertTo-Json -AsArray 1 -Compress | Should -Be '[1]'
ConvertTo-Json -AsArray 1,2 -Compress | Should -Be '[1,2]'
ConvertTo-Json -AsArray @() -Compress | Should -Be '[]'
All tests should pass.
The last 2 tests fail, because they wrap the already array-valued input in an _extra_ array:
Expected length: 5 Actual length: 7 Strings differ at index 1.
Expected: '[1,2]'
But was: '[[1,2]]'
Expected length: 5 Actual length: 7 Strings differ at index 1.
Expected: '[]'
But was: '[[]]'
PowerShell Core 7.0.0-preview.5
This is a breaking change, but it seems unlikely anyone would want the current behavior.
Most helpful comment
This is a breaking change, but it seems unlikely anyone would want the current behavior.