// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Collections.Generic; using System.Management.Automation; using System.Management.Automation.Internal; using System.Threading; using Newtonsoft.Json; namespace Microsoft.PowerShell.Commands { /// /// The ConvertTo-Json command. /// This command converts an object to a Json string representation. /// [Cmdlet(VerbsData.ConvertTo, "Json", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096925", RemotingCapability = RemotingCapability.None)] [OutputType(typeof(string))] public class ConvertToJsonCommand : PSCmdlet, IDisposable { /// /// Gets or sets the InputObject property. /// [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)] [AllowNull] public object InputObject { get; set; } private int _depth = 2; private readonly CancellationTokenSource _cancellationSource = new(); /// /// Gets or sets the Depth property. /// [Parameter] [ValidateRange(0, 100)] public int Depth { get { return _depth; } set { _depth = value; } } /// /// Gets or sets the Compress property. /// If the Compress property is set to be true, the Json string will /// be output in the compressed way. Otherwise, the Json string will /// be output with indentations. /// [Parameter] public SwitchParameter Compress { get; set; } /// /// Gets or sets the EnumsAsStrings property. /// If the EnumsAsStrings property is set to true, enum values will /// be converted to their string equivalent. Otherwise, enum values /// will be converted to their numeric equivalent. /// [Parameter] public SwitchParameter EnumsAsStrings { get; set; } /// /// Gets or sets the AsArray property. /// If the AsArray property is set to be true, the result JSON string will /// be returned with surrounding '[', ']' chars. Otherwise, /// the array symbols will occur only if there is more than one input object. /// [Parameter] public SwitchParameter AsArray { get; set; } /// /// Specifies how strings are escaped when writing JSON text. /// If the EscapeHandling property is set to EscapeHtml, the result JSON string will /// be returned with HTML (<, >, &, ', ") and control characters (e.g. newline) are escaped. /// [Parameter] public StringEscapeHandling EscapeHandling { get; set; } = StringEscapeHandling.Default; /// /// IDisposable implementation, dispose of any disposable resources created by the cmdlet. /// public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); } /// /// Implementation of IDisposable for both manual Dispose() and finalizer-called disposal of resources. /// /// /// Specified as true when Dispose() was called, false if this is called from the finalizer. /// protected virtual void Dispose(bool disposing) { if (disposing) { _cancellationSource.Dispose(); } } private readonly List _inputObjects = new(); /// /// Caching the input objects for the command. /// protected override void ProcessRecord() { _inputObjects.Add(InputObject); } /// /// Do the conversion to json and write output. /// protected override void EndProcessing() { if (_inputObjects.Count > 0) { 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); if (output != null) { WriteObject(output); } } } /// /// Process the Ctrl+C signal. /// protected override void StopProcessing() { _cancellationSource.Cancel(); } } }