diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertFromJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertFromJsonCommand.cs index 9ec8ef961c5..4057ea82a3a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertFromJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertFromJsonCommand.cs @@ -35,29 +35,6 @@ public class ConvertFromJsonCommand : Cmdlet #region overrides - /// - /// Prerequisite checks - /// - protected override void BeginProcessing() - { -#if CORECLR - JsonObject.ImportJsonDotNetModule(this); -#else - try - { - System.Reflection.Assembly.Load(new AssemblyName("System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")); - } - catch (System.IO.FileNotFoundException) - { - ThrowTerminatingError(new ErrorRecord( - new NotSupportedException(WebCmdletStrings.ExtendedProfileRequired), - "ExtendedProfileRequired", - ErrorCategory.NotInstalled, - null)); - } -#endif - } - /// /// Buffers InputObjet contents available in the pipeline. /// diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs index 7feb75ce842..4def219c2c6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -12,13 +12,8 @@ using System.Globalization; using Dbg = System.Management.Automation; using System.Management.Automation.Internal; -#if CORECLR using Newtonsoft.Json; using Newtonsoft.Json.Converters; -#else -using System.Collections.Specialized; -using System.Web.Script.Serialization; -#endif // FxCop suppressions for resource strings: [module: SuppressMessage("Microsoft.Naming", "CA1703:ResourceStringsShouldBeSpelledCorrectly", Scope = "resource", Target = "WebCmdletStrings.resources", MessageId = "json")] @@ -88,22 +83,6 @@ protected override void BeginProcessing() ErrorCategory.InvalidOperation, null)); } -#if CORECLR - JsonObject.ImportJsonDotNetModule(this); -#else - try - { - System.Reflection.Assembly.Load(new AssemblyName("System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")); - } - catch (System.IO.FileNotFoundException) - { - ThrowTerminatingError(new ErrorRecord( - new NotSupportedException(WebCmdletStrings.ExtendedProfileRequired), - "ExtendedProfileRequired", - ErrorCategory.NotInstalled, - null)); - } -#endif } private List _inputObjects = new List(); @@ -130,7 +109,6 @@ protected override void EndProcessing() // Pre-process the object so that it serializes the same, except that properties whose // values cannot be evaluated are treated as having the value null. object preprocessedObject = ProcessValue(objectToProcess, 0); -#if CORECLR JsonSerializerSettings jsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None, MaxDepth = 1024 }; if (EnumsAsStrings) { @@ -142,17 +120,6 @@ protected override void EndProcessing() } string output = JsonConvert.SerializeObject(preprocessedObject, jsonSettings); WriteObject(output); -#else - // In Full CLR, we use the JavaScriptSerializer for which RecursionLimit was set to the default value of 100 (the actual recursion limit is 99 since - // at 100 the exception is thrown). See https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.recursionlimit(v=vs.110).aspx - // ProcessValue creates an object to be serialized from 1 to depth. However, the properties of the object at 'depth' should also be serialized, - // and from the perspective of the serializer, this means it needs to support serializing depth + 1. For the JavaScriptSerializer to support this, - // RecursionLimit needs to be set to depth + 2. - JavaScriptSerializer helper = new JavaScriptSerializer() { RecursionLimit = (maxDepthAllowed + 2) }; - helper.MaxJsonLength = Int32.MaxValue; - string output = helper.Serialize(preprocessedObject); - WriteObject(Compress ? output : ConvertToPrettyJsonString(output)); -#endif } } @@ -517,11 +484,7 @@ private object ProcessValue(object obj, int depth) } else { -#if CORECLR rv = ProcessCustomObject(obj, depth); -#else - rv = ProcessCustomObject(obj, depth); -#endif isCustomObj = true; } } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeRestMethodCommand.CoreClr.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeRestMethodCommand.CoreClr.cs index fbe452d6d0a..1001b173022 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeRestMethodCommand.CoreClr.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeRestMethodCommand.CoreClr.cs @@ -69,8 +69,6 @@ internal override void ProcessResponse(HttpResponseMessage response) ); bool convertSuccess = false; - // On CoreCLR, we need to explicitly load Json.NET - JsonObject.ImportJsonDotNetModule(this); if (returnType == RestReturnType.Json) { convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex); diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs index 28c443d4790..215fec32829 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs @@ -8,17 +8,12 @@ using System.Diagnostics.CodeAnalysis; using System.Management.Automation; using System.Text.RegularExpressions; -#if CORECLR using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections.ObjectModel; using System.IO; using System.Management.Automation.Internal; using System.Reflection; -#else -using System.Web.Script.Serialization; -using System.Collections.Specialized; -#endif namespace Microsoft.PowerShell.Commands { @@ -45,7 +40,6 @@ public static object ConvertFromJson(string input, out ErrorRecord error) } error = null; -#if CORECLR object obj = null; try { @@ -89,28 +83,9 @@ public static object ConvertFromJson(string input, out ErrorRecord error) // the same as JavaScriptSerializer does throw new ArgumentException(msg, je); } -#else - //In ConvertTo-Json, to serialize an object with a given depth, we set the RecursionLimit to depth + 2, see JavaScriptSerializer constructor in ConvertToJsonCommand.cs. - // Setting RecursionLimit to depth + 2 in order to support '$object | ConvertTo-Json –depth | ConvertFrom-Json'. - JavaScriptSerializer serializer = new JavaScriptSerializer(new JsonObjectTypeResolver()) { RecursionLimit = (maxDepthAllowed + 2) }; - serializer.MaxJsonLength = Int32.MaxValue; - object obj = serializer.DeserializeObject(input); - - if (obj is IDictionary) - { - var dictionary = obj as IDictionary; - obj = PopulateFromDictionary(dictionary, out error); - } - else if (obj is ICollection) - { - var list = obj as ICollection; - obj = PopulateFromList(list, out error); - } -#endif return obj; } -#if CORECLR // This function is a clone of PopulateFromDictionary using JObject as an input. private static PSObject PopulateFromJDictionary(JObject entries, out ErrorRecord error) { @@ -205,157 +180,5 @@ private static ICollection PopulateFromJArray(JArray list, out ErrorReco } return result.ToArray(); } - - /// - /// Loads the Json.Net module to the given cmdlet execution context. - /// - /// - /// - [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")] - public static void ImportJsonDotNetModule(Cmdlet cmdlet) - { - const string jsonDotNetAssemblyName = "Newtonsoft.Json, Version=7.0.0.0"; - - // Check if the Newtonsoft.Json.dll assembly is loaded. - try - { - System.Reflection.Assembly.Load(new AssemblyName(jsonDotNetAssemblyName)); - } - catch (System.IO.FileNotFoundException) - { - // It is not, try to load it. - // Make sure that PSModuleAutoLoadingPreference is not set to 'None'. - PSModuleAutoLoadingPreference moduleAutoLoadingPreference = - CommandDiscovery.GetCommandDiscoveryPreference(cmdlet.Context, SpecialVariables.PSModuleAutoLoadingPreferenceVarPath, "PSModuleAutoLoadingPreference"); - if (moduleAutoLoadingPreference == PSModuleAutoLoadingPreference.None) - { - cmdlet.ThrowTerminatingError(new ErrorRecord( - new NotSupportedException(WebCmdletStrings.PSModuleAutoloadingPreferenceNotEnable), - "PSModuleAutoloadingPreferenceNotEnable", - ErrorCategory.NotEnabled, - null)); - } - - // Use module auto-loading to import Json.Net. - var jsonNetModulePath = Path.Combine(System.Environment.GetEnvironmentVariable("ProgramFiles"), @"WindowsPowerShell\Modules\Json.Net"); - CmdletInfo cmdletInfo = cmdlet.Context.SessionState.InvokeCommand.GetCmdlet("Microsoft.PowerShell.Core\\Import-Module"); - Exception exception; - Collection importedModule = CommandDiscovery.AutoloadSpecifiedModule(jsonNetModulePath, cmdlet.Context, cmdletInfo.Visibility, out exception); - - if ((importedModule == null) || (importedModule.Count == 0)) - { - string errorMessage = StringUtil.Format(WebCmdletStrings.JsonNetModuleRequired, WebCmdletStrings.CouldNotAutoImportJsonNetModule); - - cmdlet.ThrowTerminatingError(new ErrorRecord( - new NotSupportedException(errorMessage, exception), - "CouldNotAutoImportJsonNetModule", - ErrorCategory.InvalidOperation, - null)); - } - - // Finally, ensure that the Newtonsoft.Json.dll assembly was loaded. - try - { - System.Reflection.Assembly.Load(new AssemblyName(jsonDotNetAssemblyName)); - } - catch (System.IO.FileNotFoundException) - { - string errorMessage = StringUtil.Format( - WebCmdletStrings.JsonNetModuleRequired, - StringUtil.Format(WebCmdletStrings.JsonNetModuleFilesRequired, jsonNetModulePath)); - - cmdlet.ThrowTerminatingError(new ErrorRecord( - new NotSupportedException(errorMessage), - "JsonNetModuleRequired", - ErrorCategory.NotInstalled, - null)); - } - } - } -#else - private static ICollection PopulateFromList(ICollection list, out ErrorRecord error) - { - error = null; - List result = new List(); - - foreach (object element in list) - { - if (element is IDictionary) - { - IDictionary dic = element as IDictionary; - PSObject dicResult = PopulateFromDictionary(dic, out error); - if (error != null) - { - return null; - } - result.Add(dicResult); - } - else if (element is ICollection) - { - ICollection subList = element as ICollection; - ICollection listResult = PopulateFromList(subList, out error); - if (error != null) - { - return null; - } - result.Add(listResult); - } - else - { - result.Add(element); - } - } - - return result.ToArray(); - } - - private static PSObject PopulateFromDictionary(IDictionary entries, out ErrorRecord error) - { - error = null; - PSObject result = new PSObject(); - foreach (KeyValuePair entry in entries) - { - PSPropertyInfo property = result.Properties[entry.Key]; - if (property != null) - { - string errorMsg = string.Format(CultureInfo.InvariantCulture, - WebCmdletStrings.DuplicateKeysInJsonString, property.Name, entry.Key); - error = new ErrorRecord( - new InvalidOperationException(errorMsg), - "DuplicateKeysInJsonString", - ErrorCategory.InvalidOperation, - null); - return null; - } - - if (entry.Value is IDictionary) - { - IDictionary subEntries = entry.Value as IDictionary; - PSObject dicResult = PopulateFromDictionary(subEntries, out error); - if (error != null) - { - return null; - } - result.Properties.Add(new PSNoteProperty(entry.Key, dicResult)); - } - else if (entry.Value is ICollection) - { - ICollection list = entry.Value as ICollection; - ICollection listResult = PopulateFromList(list, out error); - if (error != null) - { - return null; - } - result.Properties.Add(new PSNoteProperty(entry.Key, listResult)); - } - else - { - result.Properties.Add(new PSNoteProperty(entry.Key, entry.Value)); - } - } - - return result; - } -#endif } -} \ No newline at end of file +} diff --git a/src/System.Management.Automation/System.Management.Automation.csproj b/src/System.Management.Automation/System.Management.Automation.csproj index bc07541d081..a9c43f67117 100644 --- a/src/System.Management.Automation/System.Management.Automation.csproj +++ b/src/System.Management.Automation/System.Management.Automation.csproj @@ -9,7 +9,7 @@ - +