ConvertPropertyValueForPsObject() статический приватный Метод

Converts a property value for a JObject into an object that can be used as the value of a PSNoteProperty.
static private ConvertPropertyValueForPsObject ( JToken propertyValue ) : object
propertyValue JToken The value.
Результат object
Пример #1
0
        /// <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);
        }
Пример #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);
        }
Пример #3
0
        /// <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());
        }