Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.

Commit 37fecca

Browse files
committed
Move to System.Text.Json
1 parent 0d5d134 commit 37fecca

File tree

90 files changed

+585
-545
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+585
-545
lines changed

Commands/Admin/AddTenantTheme.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
using PnP.PowerShell.Commands.Base.PipeBinds;
99
using System;
1010
using PnP.PowerShell.Commands.Model;
11-
using Newtonsoft.Json;
1211
using System.Linq;
12+
using System.Text.Json;
1313

1414
namespace PnP.PowerShell.Commands.Admin
1515
{
@@ -73,14 +73,17 @@ protected override void ExecuteCmdlet()
7373
{
7474
if (Overwrite.ToBool())
7575
{
76-
Tenant.UpdateTenantTheme(Identity.Name, JsonConvert.SerializeObject(theme));
76+
Tenant.UpdateTenantTheme(Identity.Name, JsonSerializer.Serialize(theme));
7777
ClientContext.ExecuteQueryRetry();
78-
} else
78+
}
79+
else
7980
{
8081
WriteError(new ErrorRecord(new Exception($"Theme exists"), "THEMEEXISTS", ErrorCategory.ResourceExists, Identity.Name));
8182
}
82-
} else {
83-
Tenant.AddTenantTheme(Identity.Name, JsonConvert.SerializeObject(theme));
83+
}
84+
else
85+
{
86+
Tenant.AddTenantTheme(Identity.Name, JsonSerializer.Serialize(theme));
8487
ClientContext.ExecuteQueryRetry();
8588
}
8689
}

Commands/Admin/GetStorageEntity.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
using PnP.PowerShell.Commands.Base;
88
using PnP.PowerShell.Commands.Enums;
99
using System.Collections.Generic;
10-
using Newtonsoft.Json;
11-
using Newtonsoft.Json.Linq;
10+
using System.Text.Json;
1211

1312
namespace PnP.PowerShell.Commands
1413
{
@@ -62,7 +61,7 @@ protected override void ExecuteCmdlet()
6261

6362
if (!string.IsNullOrEmpty(storageEntitiesIndex))
6463
{
65-
var storageEntitiesDict = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(storageEntitiesIndex);
64+
var storageEntitiesDict = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(storageEntitiesIndex);
6665

6766
var storageEntities = new List<StorageEntity>();
6867
foreach (var key in storageEntitiesDict.Keys)

Commands/Admin/GetTenantId.cs

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
#if !ONPREMISES
22
using Microsoft.SharePoint.Client;
3-
using Newtonsoft.Json.Linq;
43
using PnP.PowerShell.CmdletHelpAttributes;
54
using PnP.PowerShell.Commands.Base;
65
using System;
7-
using System.Collections.Generic;
8-
using System.Linq;
96
using System.Management.Automation;
10-
using System.Text;
11-
using System.Threading.Tasks;
7+
using System.Text.Json;
128
using System.Web;
139

1410
namespace PnP.PowerShell.Commands.Admin
@@ -47,14 +43,17 @@ protected override void ProcessRecord()
4743
}
4844
catch (Exception ex)
4945
{
50-
#if !NETSTANDARD2_1
5146
if (ex.InnerException != null)
5247
{
5348
if (ex.InnerException is HttpException)
5449
{
5550
var message = ex.InnerException.Message;
56-
var obj = JObject.Parse(message);
57-
WriteObject(obj["error_description"].ToString());
51+
52+
using (var jdoc = JsonDocument.Parse(message))
53+
{
54+
var errorDescription = jdoc.RootElement.GetProperty("error_description").GetString();
55+
WriteObject(errorDescription);
56+
}
5857
}
5958
else
6059
{
@@ -65,9 +64,6 @@ protected override void ProcessRecord()
6564
{
6665
throw ex;
6766
}
68-
#else
69-
throw ex;
70-
#endif
7167
}
7268
}
7369
}

Commands/Admin/GetTenantTheme.cs

+23-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System;
1010
using System.Linq;
1111
using PnP.PowerShell.Commands.Model;
12+
using System.Text.Json;
1213

1314
namespace PnP.PowerShell.Commands.Admin
1415
{
@@ -23,26 +24,46 @@ namespace PnP.PowerShell.Commands.Admin
2324
[CmdletExample(
2425
Code = @"PS:> Get-PnPTenantTheme -Name ""MyCompanyTheme""",
2526
Remarks = @"Returns the specified theme", SortOrder = 1)]
27+
[CmdletExample(
28+
Code = @"PS:> Get-PnPTenantTheme -Name ""MyCompanyTheme"" -AsJson",
29+
Remarks = @"Returns the specified theme formatted as JSON", SortOrder = 2)]
2630
public class GetTenantTheme : PnPAdminCmdlet
2731
{
2832
[Parameter(Mandatory = false, Position = 0, ValueFromPipeline = true, HelpMessage = "The name of the theme to retrieve")]
2933
public string Name;
3034

35+
[Parameter(Mandatory = false, Position = 1, HelpMessage = "Outputs the themes as JSON")]
36+
public SwitchParameter AsJson;
37+
3138
protected override void ExecuteCmdlet()
3239
{
3340
if (ParameterSpecified(nameof(Name)))
3441
{
3542
var theme = Tenant.GetTenantTheme(Name);
3643
ClientContext.Load(theme);
3744
ClientContext.ExecuteQueryRetry();
38-
WriteObject(new SPOTheme(theme.Name, theme.Palette, theme.IsInverted));
45+
if (AsJson)
46+
{
47+
WriteObject(JsonSerializer.Serialize(theme.Palette));
48+
}
49+
else
50+
{
51+
WriteObject(new SPOTheme(theme.Name, theme.Palette, theme.IsInverted));
52+
}
3953
}
4054
else
4155
{
4256
var themes = Tenant.GetAllTenantThemes();
4357
ClientContext.Load(themes);
4458
ClientContext.ExecuteQueryRetry();
45-
WriteObject(themes.Select(t => new SPOTheme(t.Name, t.Palette, t.IsInverted)), true);
59+
if (AsJson)
60+
{
61+
WriteObject(JsonSerializer.Serialize(themes.Select(t => new SPOTheme(t.Name, t.Palette, t.IsInverted))));
62+
}
63+
else
64+
{
65+
WriteObject(themes.Select(t => new SPOTheme(t.Name, t.Palette, t.IsInverted)), true);
66+
}
4667
}
4768

4869
}

Commands/Admin/RemoveStorageEntity.cs

-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
#if !ONPREMISES
2-
using System.Linq;
32
using System.Management.Automation;
4-
using Microsoft.Online.SharePoint.TenantAdministration;
53
using Microsoft.SharePoint.Client;
64
using PnP.PowerShell.CmdletHelpAttributes;
7-
using PnP.PowerShell.Commands.Base;
85
using PnP.PowerShell.Commands.Enums;
9-
using System.Collections.Generic;
10-
using Newtonsoft.Json;
11-
using Newtonsoft.Json.Linq;
126

137
namespace PnP.PowerShell.Commands
148
{

Commands/Admin/SetStorageEntity.cs

-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
#if !ONPREMISES
2-
using System.Linq;
32
using System.Management.Automation;
4-
using Microsoft.Online.SharePoint.TenantAdministration;
53
using Microsoft.SharePoint.Client;
64
using PnP.PowerShell.CmdletHelpAttributes;
7-
using PnP.PowerShell.Commands.Base;
85
using PnP.PowerShell.Commands.Enums;
9-
using System.Collections.Generic;
10-
using Newtonsoft.Json;
11-
using Newtonsoft.Json.Linq;
126

137
namespace PnP.PowerShell.Commands
148
{

Commands/Base/AddStoredCredential.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace PnP.PowerShell.Commands.Base
88
{
99
[Cmdlet(VerbsCommon.Add, "PnPStoredCredential")]
10-
#if !NETSTANDARD2_1
10+
#if !PNPPSCORE
1111
[CmdletHelp("Adds a credential to the Windows Credential Manager",
1212
@"Adds an entry to the Windows Credential Manager. If you add an entry in the form of the URL of your tenant/server PnP PowerShell will check if that entry is available when you connect using Connect-PnPOnline. If it finds a matching URL it will use the associated credentials.
1313
@@ -41,7 +41,7 @@ public class AddStoredCredential : PSCmdlet
4141
[Parameter(Mandatory = false, HelpMessage = @"If not specified you will be prompted to enter your password.
4242
If you want to specify this value use ConvertTo-SecureString -String 'YourPassword' -AsPlainText -Force")]
4343
public SecureString Password;
44-
#if NETSTANDARD2_1
44+
#if PNPPSCORE
4545
[Parameter(Mandatory = false)]
4646
public SwitchParameter Overwrite;
4747
#endif
@@ -53,7 +53,7 @@ protected override void ProcessRecord()
5353
Password = Host.UI.ReadLineAsSecureString();
5454
}
5555

56-
#if !NETSTANDARD2_1
56+
#if !PNPPSCORE
5757
Utilities.CredentialManager.AddCredential(Name, Username, Password);
5858
#else
5959
Utilities.CredentialManager.AddCredential(Name, Username, Password, Overwrite.ToBool());

Commands/Base/BasePSCmdlet.cs

+45-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ namespace PnP.PowerShell.Commands.Base
1010
/// </summary>
1111
public class BasePSCmdlet : PSCmdlet
1212
{
13-
private Assembly newtonsoftAssembly;
13+
private static Assembly newtonsoftAssembly;
14+
private static Assembly systemBuffersAssembly;
15+
private static Assembly systemRuntimeCompilerServicesUnsafeAssembly;
16+
private static Assembly systemThreadingTasksExtensionsAssembly;
1417

1518
protected override void BeginProcessing()
1619
{
@@ -32,21 +35,42 @@ protected override void EndProcessing()
3235

3336
private void FixAssemblyResolving()
3437
{
35-
var newtonsoftAssemblyByLocation = Path.Combine(AssemblyDirectoryFromLocation, "Newtonsoft.Json.dll");
36-
if (File.Exists(newtonsoftAssemblyByLocation))
38+
if (BasePSCmdlet.newtonsoftAssembly == null)
3739
{
38-
// Local run, network run, etc.
39-
newtonsoftAssembly = Assembly.LoadFrom(newtonsoftAssemblyByLocation);
40+
newtonsoftAssembly = GetAssembly("Newtonsoft.Json.dll");
4041
}
41-
else
42+
if (systemBuffersAssembly == null)
43+
{
44+
systemBuffersAssembly = GetAssembly("System.Buffers.dll");
45+
}
46+
if (systemRuntimeCompilerServicesUnsafeAssembly == null)
47+
{
48+
systemRuntimeCompilerServicesUnsafeAssembly = GetAssembly("System.Runtime.CompilerServices.Unsafe.dll");
49+
}
50+
if (systemThreadingTasksExtensionsAssembly == null)
4251
{
43-
// Running from Azure Function
44-
var newtonsoftAssemblyByCodeBase = Path.Combine(AssemblyDirectoryFromCodeBase, "Newtonsoft.Json.dll");
45-
newtonsoftAssembly = Assembly.LoadFrom(newtonsoftAssemblyByCodeBase);
52+
systemThreadingTasksExtensionsAssembly = GetAssembly("System.Threading.Tasks.Extensions.dll");
4653
}
54+
4755
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
4856
}
4957

58+
private Assembly GetAssembly(string assemblyName)
59+
{
60+
Assembly assembly = null;
61+
var assemblyPath = Path.Combine(AssemblyDirectoryFromLocation, assemblyName);
62+
if (File.Exists(assemblyPath))
63+
{
64+
assembly = Assembly.LoadFrom(assemblyPath);
65+
}
66+
else
67+
{
68+
var codebasePath = Path.Combine(AssemblyDirectoryFromCodeBase, assemblyName);
69+
assembly = Assembly.LoadFrom(codebasePath);
70+
}
71+
return assembly;
72+
}
73+
5074
private string AssemblyDirectoryFromLocation
5175
{
5276
get
@@ -74,6 +98,18 @@ private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs a
7498
{
7599
return newtonsoftAssembly;
76100
}
101+
if (args.Name.StartsWith("System.Buffers", StringComparison.InvariantCultureIgnoreCase))
102+
{
103+
return systemBuffersAssembly;
104+
}
105+
if (args.Name.StartsWith("System.Runtime.CompilerServices.Unsafe", StringComparison.InvariantCultureIgnoreCase))
106+
{
107+
return systemRuntimeCompilerServicesUnsafeAssembly;
108+
}
109+
if (args.Name.StartsWith("System.Threading.Tasks.Extensions", StringComparison.InvariantCultureIgnoreCase))
110+
{
111+
return systemThreadingTasksExtensionsAssembly;
112+
}
77113
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
78114
{
79115
if (assembly.FullName == args.Name)

0 commit comments

Comments
 (0)