Skip to content

Commit bb88b49

Browse files
committed
Merge pull request #8 from kiranisaac/master
use ConvertTo-Json instead of Newtonsoft
2 parents 3f57ac0 + 04a9233 commit bb88b49

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

src/ServiceManagement/Automation/Commands.Automation/Common/AutomationClient.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ public Variable CreateVariable(Variable variable)
412412
Name = variable.Name,
413413
Properties = new AutomationManagement.Models.EncryptedVariableCreateProperties()
414414
{
415-
Value = JsonConvert.SerializeObject(variable.Value),
415+
Value = PowerShellJsonConverter.Serialize(variable.Value),
416416
Description = variable.Description
417417
}
418418
};
@@ -430,7 +430,7 @@ public Variable CreateVariable(Variable variable)
430430
Name = variable.Name,
431431
Properties = new AutomationManagement.Models.VariableCreateProperties()
432432
{
433-
Value = JsonConvert.SerializeObject(variable.Value),
433+
Value = PowerShellJsonConverter.Serialize(variable.Value),
434434
Description = variable.Description
435435
}
436436
};
@@ -497,7 +497,7 @@ public Variable UpdateVariable(Variable variable, VariableUpdateFields updateFie
497497
{
498498
updateParams.Properties = new AutomationManagement.Models.EncryptedVariableUpdateProperties()
499499
{
500-
Value = JsonConvert.SerializeObject(variable.Value)
500+
Value = PowerShellJsonConverter.Serialize(variable.Value)
501501
};
502502
}
503503

@@ -521,7 +521,7 @@ public Variable UpdateVariable(Variable variable, VariableUpdateFields updateFie
521521
{
522522
updateParams.Properties = new AutomationManagement.Models.VariableUpdateProperties()
523523
{
524-
Value = JsonConvert.SerializeObject(variable.Value)
524+
Value = PowerShellJsonConverter.Serialize(variable.Value)
525525
};
526526
}
527527

@@ -1226,8 +1226,7 @@ public Connection UpdateConnectionFieldValue(string automationAccountName, strin
12261226
if (connectionModel.Properties.FieldDefinitionValues.ContainsKey(connectionFieldName))
12271227
{
12281228
connectionModel.Properties.FieldDefinitionValues[connectionFieldName] =
1229-
JsonConvert.SerializeObject(value,
1230-
new JsonSerializerSettings() {DateFormatHandling = DateFormatHandling.MicrosoftDateFormat});
1229+
PowerShellJsonConverter.Serialize(value);
12311230
}
12321231
else
12331232
{
@@ -1534,7 +1533,7 @@ private IDictionary<string, string> ProcessRunbookParameters(string automationAc
15341533
object paramValue = parameters[runbookParameter.Key];
15351534
try
15361535
{
1537-
filteredParameters.Add(runbookParameter.Key, JsonConvert.SerializeObject(paramValue, new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat }));
1536+
filteredParameters.Add(runbookParameter.Key, PowerShellJsonConverter.Serialize(paramValue));
15381537
}
15391538
catch (JsonSerializationException)
15401539
{

src/ServiceManagement/Automation/Commands.Automation/Common/Constants.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,16 @@ public class AutomationAccountState
4444

4545
public const string Suspended = "Suspended";
4646
}
47+
48+
public const string PsCommandConvertToJson = "ConvertTo-Json";
49+
50+
public const string PsCommandConvertFromJson = "ConvertFrom-Json";
51+
52+
public const string PsCommandParamInputObject = "InputObject";
53+
54+
public const string PsCommandParamDepth = "Depth";
55+
56+
public const int PsCommandValueDepth = 10;
57+
4758
}
4859
}

src/ServiceManagement/Automation/Commands.Automation/Common/PowershellJsonConverter.cs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using Microsoft.Azure.Commands.Automation.Properties;
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Automation.Properties;
216
using System;
317
using System.Collections;
418
using System.Collections.ObjectModel;
@@ -9,12 +23,27 @@
923

1024
namespace Microsoft.Azure.Commands.Automation.Common
1125
{
12-
public static class PowershellJsonConverter
26+
public static class PowerShellJsonConverter
1327
{
14-
private const string PsCommandConvertToJson = "ConvertTo-Json";
15-
private const string PsCommandConvertFromJson = "ConvertFrom-Json";
16-
private const string PsCommandParamInputObject = "InputObject";
17-
private const string PsCommandParamDepth = "Depth";
28+
public static string Serialize(object inputObject)
29+
{
30+
if (inputObject == null)
31+
{
32+
return null;
33+
}
34+
35+
Hashtable parameters = new Hashtable();
36+
parameters.Add(Constants.PsCommandParamInputObject, inputObject);
37+
parameters.Add(Constants.PsCommandParamDepth, Constants.PsCommandValueDepth);
38+
var result = PowerShellJsonConverter.InvokeScript(Constants.PsCommandConvertToJson, parameters);
39+
40+
if (result.Count != 1)
41+
{
42+
return null;
43+
}
44+
45+
return result[0].ToString();
46+
}
1847

1948
public static PSObject Deserialize(string json)
2049
{
@@ -24,8 +53,8 @@ public static PSObject Deserialize(string json)
2453
}
2554

2655
Hashtable parameters = new Hashtable();
27-
parameters.Add(PsCommandParamInputObject, json);
28-
var result = PowershellJsonConverter.InvokeScript(PsCommandConvertFromJson, parameters);
56+
parameters.Add(Constants.PsCommandParamInputObject, json);
57+
var result = PowerShellJsonConverter.InvokeScript(Constants.PsCommandConvertFromJson, parameters);
2958
if (result.Count != 1)
3059
{
3160
return null;

src/ServiceManagement/Automation/Commands.Automation/Model/Variable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Variable(AutomationManagement.Models.Variable variable, string automation
4646
}
4747
else
4848
{
49-
this.Value = PowershellJsonConverter.Deserialize(variable.Properties.Value);
49+
this.Value = PowerShellJsonConverter.Deserialize(variable.Properties.Value);
5050
}
5151

5252
this.Description = variable.Properties.Description;

0 commit comments

Comments
 (0)