Skip to content

Commit 9562911

Browse files
authored
String with escape chars cannot be converted into json object (Azure#12283)
* Fix the issue that string with escape chars cannot be converted into json object. * Change changelog.md * Change the implement of serializtion and deserializtion of json in automation. Co-authored-by: wyunchi-ms <[email protected]>
1 parent fa0234e commit 9562911

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

src/Automation/Automation/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed the issue that string with escape chars cannot be converted into json object.
2122

2223
## Version 1.3.6
2324
* Fixed typo in Example 1 in reference documentation for `New-AzAutomationSoftwareUpdateConfiguration`

src/Automation/Automation/Common/PowershellJsonConverter.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Globalization;
2020
using System.Management.Automation;
2121
using System.Text;
22+
using Newtonsoft.Json;
2223

2324
namespace Microsoft.Azure.Commands.Automation.Common
2425
{
@@ -31,17 +32,7 @@ public static string Serialize(object inputObject)
3132
return null;
3233
}
3334

34-
Hashtable parameters = new Hashtable();
35-
parameters.Add(Constants.PsCommandParamInputObject, inputObject);
36-
parameters.Add(Constants.PsCommandParamDepth, Constants.PsCommandValueDepth);
37-
var result = PowerShellJsonConverter.InvokeScript(Constants.PsCommandConvertToJson, parameters);
38-
39-
if (result.Count != 1)
40-
{
41-
return null;
42-
}
43-
44-
return result[0].ToString();
35+
return JsonConvert.SerializeObject(inputObject);
4536
}
4637

4738
public static PSObject Deserialize(string json)
@@ -51,16 +42,14 @@ public static PSObject Deserialize(string json)
5142
return null;
5243
}
5344

54-
Hashtable parameters = new Hashtable();
55-
parameters.Add(Constants.PsCommandParamInputObject, json);
56-
var result = PowerShellJsonConverter.InvokeScript(Constants.PsCommandConvertFromJson, parameters);
57-
if (result.Count != 1)
45+
try
5846
{
59-
return null;
47+
object result = JsonConvert.DeserializeObject(json);
48+
return new PSObject(result);
49+
} catch
50+
{
51+
return json;
6052
}
61-
62-
//count == 1. return the first psobject
63-
return result[0];
6453
}
6554

6655
/// <summary>

0 commit comments

Comments
 (0)