Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/Automation/Automation/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Fixed the issue of processing `PSCustomObject` and `Array`.

## Version 1.4.2
* Fixed issue where description was not populated for update management schedules
Expand Down
34 changes: 29 additions & 5 deletions src/Automation/Automation/Common/PowershellJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
using System.Management.Automation;
using System.Text;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using System.Linq;
using System.Collections.Generic;

namespace Microsoft.Azure.Commands.Automation.Common
{
Expand All @@ -31,20 +34,41 @@ public static string Serialize(object inputObject)
{
return null;
}
while(inputObject is PSObject && ((PSObject)inputObject).BaseObject != null)
if (inputObject is string @str)
{
inputObject = ((PSObject)inputObject).BaseObject;
return str.Trim();
}
if(inputObject is string)
else if (inputObject is object[] @objectArray)
{
inputObject = ((string)inputObject).Trim();
return SerializeArray(objectArray);
}
else if (inputObject is PSObject @psObject)
{
return SerializePsObject(psObject);
}
return JsonConvert.SerializeObject(inputObject);
}

private static string SerializePsObject(PSObject @psObject)
{
Dictionary<string, string> hashTable = new Dictionary<string, string>();
foreach (var item in @psObject.Properties)
{
hashTable.Add(item.Name, Serialize(item.Value));
}

return JsonConvert.SerializeObject(hashTable);
}

private static string SerializeArray(object[] objectArray)
{
List<object> objectList = objectArray.ToList();
return string.Format("[{0}]", string.Join(",", objectList.Select(Serialize).ToList()));
}

public static PSObject Deserialize(string json)
{
if (String.IsNullOrEmpty(json))
if (string.IsNullOrEmpty(json))
{
return null;
}
Expand Down