/// <summary> /// Converts a <see cref="JObject"/> to a <see cref="PSObject"/> /// </summary> /// <param name="jtoken">The <see cref="JObject"/></param> /// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param> /// <param name="objectType">The type of the object.</param> internal static PSObject ToPsObject(this JToken jtoken, ResourceObjectFormat objectFormat, string objectType = null) { if (jtoken == null) { return null; } if (jtoken.Type != JTokenType.Object) { return new PSObject(JTokenExtensions.ConvertPropertyValueForPsObject(propertyValue: jtoken, objectFormat: objectFormat)); } var jobject = (JObject)jtoken; var psObject = new PSObject(); if (jobject.CanConvertTo<Resource<JToken>>()) { return jobject.ToResource().ToPsObject(objectFormat); } if (!string.IsNullOrWhiteSpace(objectType)) { psObject.TypeNames.Add(objectType); } foreach (var property in jobject.Properties()) { psObject.Properties.Add(new PSNoteProperty( name: JTokenExtensions.ConvertToPascalCase(propertyName: property.Name), value: JTokenExtensions.ConvertPropertyValueForPsObject(propertyValue: property.Value, objectFormat: objectFormat))); } return psObject; }
/// <summary> /// Converts a <see cref="JObject"/> to a <see cref="PSObject"/> /// </summary> /// <param name="jtoken">The <see cref="JObject"/></param> /// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param> /// <param name="objectType">The type of the object.</param> internal static PSObject ToPsObject(this JToken jtoken, ResourceObjectFormat objectFormat, string objectType = null) { if (jtoken == null) { return(null); } if (jtoken.Type != JTokenType.Object) { return(new PSObject(JTokenExtensions.ConvertPropertyValueForPsObject(propertyValue: jtoken, objectFormat: objectFormat))); } var jobject = (JObject)jtoken; var psObject = new PSObject(); if (jobject.CanConvertTo <Resource <JToken> >()) { return(jobject.ToResource().ToPsObject(objectFormat)); } if (!string.IsNullOrWhiteSpace(objectType)) { psObject.TypeNames.Add(objectType); } foreach (var property in jobject.Properties()) { psObject.Properties.Add(new PSNoteProperty( name: JTokenExtensions.ConvertToPascalCase(propertyName: property.Name), value: JTokenExtensions.ConvertPropertyValueForPsObject(propertyValue: property.Value, objectFormat: objectFormat))); } return(psObject); }
/// <summary> /// Gets the properties object /// </summary> /// <param name="resource">The <see cref="Resource{JToken}"/> object.</param> /// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param> private static object GetProperties(Resource <JToken> resource, ResourceObjectFormat objectFormat) { if (resource.Properties == null) { return(null); } return(objectFormat == ResourceObjectFormat.Legacy ? JsonUtilities.DeserializeJson(resource.Properties.ToString()) : (object)resource.Properties.ToPsObject(objectFormat)); }
/// <summary> /// Writes the object /// </summary> /// <param name="resultString">The result as a string</param> /// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param> protected void TryConvertToResourceAndWriteObject(string resultString, ResourceObjectFormat objectFormat) { Resource <JToken> resultResource; if (resultString.TryConvertTo <Resource <JToken> >(out resultResource)) { this.WriteObject(resultResource.ToPsObject(objectFormat)); } else { this.WriteObject(resultString); } }
/// <summary> /// Writes the object /// </summary> /// <param name="resultString">The result as a string</param> /// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param> protected void TryConvertAndWriteObject(string resultString, ResourceObjectFormat objectFormat) { JToken resultJToken; if (resultString.TryConvertTo <JToken>(out resultJToken)) { this.WriteObject(resultJToken, objectFormat); } else { this.WriteObject(resultString); } }
/// <summary> /// Converts a property value for a <see cref="JObject"/> into an <see cref="object"/> that can be /// used as the value of a <see cref="PSNoteProperty"/>. /// </summary> /// <param name="propertyValue">The <see cref="JToken"/> value.</param> /// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param> internal static object ConvertPropertyValueForPsObject(JToken propertyValue, ResourceObjectFormat objectFormat) { if (propertyValue.Type == JTokenType.Object) { return(propertyValue.ToPsObject(objectFormat)); } if (propertyValue.Type == JTokenType.Array) { var jArray = (JArray)propertyValue; var array = new object[jArray.Count]; for (int i = 0; i < array.Length; ++i) { array[i] = JTokenExtensions.ConvertPropertyValueForPsObject(jArray[i], objectFormat); } return(array); } Type primitiveType; if (JTokenExtensions.PrimitiveTypeMap.TryGetValue(propertyValue.Type, out primitiveType)) { try { return(propertyValue.ToObject(primitiveType, JsonExtensions.JsonObjectTypeSerializer)); } catch (FormatException) { } catch (ArgumentException) { } catch (JsonException) { } } return(propertyValue.ToString()); }
/// <summary> /// Converts a <see cref="Resource{JToken}"/> object into a <see cref="PSObject"/> object. /// </summary> /// <param name="resource">The <see cref="Resource{JToken}"/> object.</param> /// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param> internal static PSObject ToPsObject(this Resource <JToken> resource, ResourceObjectFormat objectFormat) { var resourceType = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceType(resource.Id); var extensionResourceType = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetExtensionResourceType(resource.Id); var objectDefinition = new Dictionary <string, object> { { "Name", resource.Name }, { "ResourceId", string.IsNullOrEmpty(resource.Id) ? null : resource.Id }, { "ResourceName", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceName(resource.Id) }, { "ResourceType", resourceType }, { "ExtensionResourceName", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetExtensionResourceName(resource.Id) }, { "ExtensionResourceType", extensionResourceType }, { "Kind", resource.Kind }, { "ResourceGroupName", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceGroupName(resource.Id) }, { "Location", resource.Location }, { "SubscriptionId", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetSubscriptionId(resource.Id) }, { "Tags", TagsHelper.GetTagsHashtables(resource.Tags) }, { "Plan", resource.Plan.ToJToken().ToPsObject(objectFormat) }, { "Properties", ResourceExtensions.GetProperties(resource, objectFormat) }, { "CreatedTime", resource.CreatedTime }, { "ChangedTime", resource.ChangedTime }, { "ETag", resource.ETag }, }; var resourceTypeName = resourceType == null && extensionResourceType == null ? null : (resourceType + extensionResourceType).Replace('/', '.'); var psObject = PowerShellUtilities.ConstructPSObject( resourceTypeName, objectDefinition.Where(kvp => kvp.Value != null).SelectManyArray(kvp => new[] { kvp.Key, kvp.Value })); psObject.TypeNames.Add(Constants.MicrosoftAzureResource); return(psObject); }
/// <summary> /// Writes a <see cref="JToken"/> object as a <see cref="PSObject"/>. /// </summary> /// <param name="result">The result of the action.</param> /// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param> protected void WriteObject(JToken result, ResourceObjectFormat objectFormat) { this.WriteObject(sendToPipeline: result.ToPsObject(objectFormat), enumerateCollection: true); }
/// <summary> /// Converts a property value for a <see cref="JObject"/> into an <see cref="object"/> that can be /// used as the value of a <see cref="PSNoteProperty"/>. /// </summary> /// <param name="propertyValue">The <see cref="JToken"/> value.</param> /// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param> internal static object ConvertPropertyValueForPsObject(JToken propertyValue, ResourceObjectFormat objectFormat) { if (propertyValue.Type == JTokenType.Object) { return propertyValue.ToPsObject(objectFormat); } if (propertyValue.Type == JTokenType.Array) { var jArray = (JArray)propertyValue; var array = new object[jArray.Count]; for (int i = 0; i < array.Length; ++i) { array[i] = JTokenExtensions.ConvertPropertyValueForPsObject(jArray[i], objectFormat); } return array; } Type primitiveType; if (JTokenExtensions.PrimitiveTypeMap.TryGetValue(propertyValue.Type, out primitiveType)) { try { return propertyValue.ToObject(primitiveType, JsonExtensions.JsonObjectTypeSerializer); } catch (FormatException) { } catch (ArgumentException) { } catch (JsonException) { } } return propertyValue.ToString(); }
/// <summary> /// Initializes a new instance of the <see cref="GetAzureResourceCmdlet" /> class. /// </summary> public GetAzureResourceCmdlet() { this.OutputObjectFormat = ResourceObjectFormat.Legacy; }
/// <summary> /// Writes the object /// </summary> /// <param name="resultString">The result as a string</param> /// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param> protected void TryConvertToResourceAndWriteObject(string resultString, ResourceObjectFormat objectFormat) { Resource<JToken> resultResource; if (resultString.TryConvertTo<Resource<JToken>>(out resultResource)) { this.WriteObject(resultResource.ToPsObject(objectFormat)); } else { this.WriteObject(resultString); } }
/// <summary> /// Writes the object /// </summary> /// <param name="resultString">The result as a string</param> /// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param> protected void TryConvertAndWriteObject(string resultString, ResourceObjectFormat objectFormat) { JToken resultJToken; if (resultString.TryConvertTo<JToken>(out resultJToken)) { this.WriteObject(resultJToken, objectFormat); } else { this.WriteObject(resultString); } }