Skip to content

ConvertTo-Json -AsArray should be an array "guarantor", not a constructor #10952

@mklement0

Description

@mklement0

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.

Steps to reproduce

ConvertTo-Json -AsArray 1 -Compress | Should -Be '[1]'
ConvertTo-Json -AsArray 1,2 -Compress | Should -Be '[1,2]'
ConvertTo-Json -AsArray @() -Compress | Should -Be '[]'

Expected behavior

All tests should pass.

Actual behavior

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:  '[[]]'

Environment data

PowerShell Core 7.0.0-preview.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking-Changebreaking change that may affect usersIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions