Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.PowerShell.Commands
/// The ConvertTo-Json command.
/// This command converts an object to a Json string representation.
/// </summary>
[Cmdlet(VerbsData.ConvertTo, "Json", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096925", RemotingCapability = RemotingCapability.None)]
[Cmdlet(VerbsData.ConvertTo, "Json", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096925", RemotingCapability = RemotingCapability.None, DefaultParameterSetName = "Default")]
[OutputType(typeof(string))]
public class ConvertToJsonCommand : PSCmdlet, IDisposable
{
Expand Down Expand Up @@ -65,9 +65,17 @@ public int Depth
/// be returned with surrounding '[', ']' chars. Otherwise,
/// the array symbols will occur only if there is more than one input object.
/// </summary>
[Parameter]
[Parameter(ParameterSetName = "Array")]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this still be used with -AsStream, e.g.

"foo", "bar" | ConvertTo-Json -AsArray -AsStream
# ["foo"]
# ["bar"]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could. Alternatively, the -AsArray -AsStream combo could output a single Json array, but output as multiple strings such that the data would not all be buffered in memory.

I'm undecided which would be best.

public SwitchParameter AsArray { get; set; }

/// <summary>
/// Gets or sets the AsStream property.
/// If the AsStream property is set to be true, the input objects are converted
/// to JSON individually. There will be one output string for each input object.
/// </summary>
[Parameter(ParameterSetName = "Stream")]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure a new parameter set is actually needed here, the switch itself is what is signalling whether to stream or not. I don't think that -AsArray and -AsStream are mutually exclusive as well as you can stream JSON arrays.

One thing to keep in mind about this pattern, if someone was to set the switch to false it will still go through the parameter set locking you into whatever parameters are exposed in there.

If -AsArray was mutually exclusive (just a hypothetical) then doing "foo" | ConvertTo-Json -AsArray -AsStream:$AsStream would fail if $AsStream was false. You would have to conditionally add the switch which is not ideal.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the parameter sets to disallow the -AsArray -AsStream combo.
That way it will not be a breaking change when it is decided to make the combo do something.

public SwitchParameter AsStream { get; set; }

/// <summary>
/// Specifies how strings are escaped when writing JSON text.
/// If the EscapeHandling property is set to EscapeHtml, the result JSON string will
Expand Down Expand Up @@ -100,13 +108,40 @@ protected virtual void Dispose(bool disposing)
}

private readonly List<object> _inputObjects = new();
private JsonObject.ConvertToJsonContext _context;

/// <summary>
/// Initialize the _context for converting objects to Json.
/// </summary>
protected override void BeginProcessing()
{
_context = new JsonObject.ConvertToJsonContext(
Depth,
EnumsAsStrings.IsPresent,
Compress.IsPresent,
EscapeHandling,
targetCmdlet: this,
_cancellationSource.Token);
}

/// <summary>
/// Caching the input objects for the command.
/// In AsStream mode, convert and output the input object.
/// Otherwise cache the input objects for the command.
/// </summary>
protected override void ProcessRecord()
{
_inputObjects.Add(InputObject);
if (AsStream)
{
string output = JsonObject.ConvertToJson(InputObject, in _context);
if (output != null)
{
WriteObject(output);
}
}
else
{
_inputObjects.Add(InputObject);
}
}

/// <summary>
Expand All @@ -118,17 +153,9 @@ protected override void EndProcessing()
{
object objectToProcess = (_inputObjects.Count > 1 || AsArray) ? (_inputObjects.ToArray() as object) : _inputObjects[0];

var context = new JsonObject.ConvertToJsonContext(
Depth,
EnumsAsStrings.IsPresent,
Compress.IsPresent,
EscapeHandling,
targetCmdlet: this,
_cancellationSource.Token);

// null is returned only if the pipeline is stopping (e.g. ctrl+c is signaled).
// in that case, we shouldn't write the null to the output pipe.
string output = JsonObject.ConvertToJson(objectToProcess, in context);
string output = JsonObject.ConvertToJson(objectToProcess, in _context);
if (output != null)
{
WriteObject(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,52 @@ Describe 'ConvertTo-Json' -tags "CI" {
$actual = ConvertTo-Json -Compress -InputObject $obj
$actual | Should -Be '{"Positive":18446744073709551615,"Negative":-18446744073709551615}'
}

It "Should convert objects to JSON as Stream" {
$objects = @(
[pscustomobject]@{ Name = "Object1"; Value = 10 },
[pscustomobject]@{ Name = "Object2"; Value = 20 },
[pscustomobject]@{ Name = "Object3"; Value = 30 }
)
$returnObject = $objects | ConvertTo-Json -Compress -AsStream
$returnObject -is [System.Array] | Should -BeTrue
$returnObject | Should -HaveCount 3
$returnObject[0] | Should -Be '{"Name":"Object1","Value":10}'
$returnObject[1] | Should -Be '{"Name":"Object2","Value":20}'
$returnObject[2] | Should -Be '{"Name":"Object3","Value":30}'
Comment on lines +169 to +171
}

It "Should convert nested array to JSON as Stream" {
$objects = ((1,2),3,(4,5))
$returnObject = $objects | ConvertTo-Json -Compress -AsStream
$returnObject -is [System.Array] | Should -BeTrue
$returnObject | Should -HaveCount 3
$returnObject[0] | Should -Be '[1,2]'
$returnObject[1] | Should -Be '3'
$returnObject[2] | Should -Be '[4,5]'
Comment thread
jshigetomi marked this conversation as resolved.
Resolved
}

It "StopProcessing as stream should succeed" {
$ps = [PowerShell]::Create()
$dataCollection = [System.Management.Automation.PSDataCollection[PSObject]]::new()
$null = $ps.AddScript({
$obj1 = [PSCustomObject]@{Prop = 'value1'}
$obj2 = [PSCustomObject]@{} | Add-Member -MemberType ScriptProperty -Name Prop -Value {
Start-Sleep -Seconds 5
'value2'
} -PassThru
$obj1,$obj2 | ConvertTo-Json -AsStream
throw "Should not have thrown exception"
})
$null = $ps.BeginInvoke($dataCollection,$dataCollection)
Start-Sleep -Milliseconds 1000
$null = $ps.BeginStop($null, $null)
Start-Sleep -Milliseconds 1000
$dataCollection.Count | Should -BeExactly 1
$ps.InvocationStateInfo.State | Should -BeExactly "Stopped"
$ps.Dispose()
}
Comment on lines +185 to +203

#region Comprehensive Scalar Type Tests (Phase 1)
# Test coverage for ConvertTo-Json scalar serialization
# Covers: Pipeline vs InputObject, ETS vs no ETS, all primitive and special types
Expand Down
Loading