Skip to content

Commit

Permalink
[Automation]Fix the issue of processing PSCustomObject (Azure#14045)
Browse files Browse the repository at this point in the history
* Fix the issue of processing PSCustomObject.

* Add changelog

* Update ChangeLog.md

Co-authored-by: wyunchi-ms <yunwang@microsoft.com>
  • Loading branch information
wyunchi-ms and wyunchi-ms authored Jan 29, 2021
1 parent 6727874 commit dadfd0f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
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

0 comments on commit dadfd0f

Please sign in to comment.