A helper class for converting JObject and JToken objects to PSObject classes.
        /// <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);
        }
Beispiel #2
0
        /// <summary>
        /// Converts a <see cref="JObject"/> to a <see cref="PSObject"/>
        /// </summary>
        /// <param name="jtoken">The <see cref="JObject"/></param>
        /// <param name="objectType">The type of the object.</param>
        internal static PSObject ToPsObject(this JToken jtoken, string objectType = null)
        {
            if (jtoken == null)
            {
                return(null);
            }

            if (jtoken.Type != JTokenType.Object)
            {
                return(new PSObject(JTokenExtensions.ConvertPropertyValueForPsObject(propertyValue: jtoken)));
            }

            var jobject  = (JObject)jtoken;
            var psObject = new PSObject();

            if (!string.IsNullOrWhiteSpace(objectType))
            {
                psObject.TypeNames.Add(objectType);
            }

            foreach (var property in jobject.Properties())
            {
                psObject.Properties.Add(new PSNoteProperty(
                                            name: property.Name,
                                            value: JTokenExtensions.ConvertPropertyValueForPsObject(propertyValue: property.Value)));
            }

            return(psObject);
        }
        /// <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());
        }