public bool Equals(SerializedPropertyInfo other)
        {
            if (other == null)
            {
                return(false);
            }

            if (IsString != other.IsString)
            {
                return(false);
            }

            if (!ReferenceEquals(SerializedValue, other.SerializedValue))
            {
                if (SerializedValue == null || other.SerializedValue == null)
                {
                    return(false);
                }

                if (!SerializedValue.Equals(other.SerializedValue))
                {
                    return(false);
                }
            }

            return(true);
        }
        public T GetProperty <T>(string propertyName)
        {
            if (Properties?.ContainsKey(propertyName) != true)
            {
                throw new InvalidOperationException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              SdkResources.PropertyDoesNotExist,
                              propertyName));
            }

            SerializedPropertyInfo propValue = Properties[propertyName];

            if (propValue == null)
            {
                if (typeof(T).IsValueType)
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  SdkResources.PropertyOfValueTypeCannotBeNull,
                                  propertyName,
                                  typeof(T).FullName));
                }

                // This will return null for reference types. Could not set null here b/c T could be a value type as well.
                return(default);
        public static void Write(JsonWriter writer, SerializedPropertyInfo value)
        {
            SerializedPropertyInfo spi = (SerializedPropertyInfo)value;

            if (spi == null || spi.SerializedValue == null)
            {
                writer.WriteNull();
            }
            else
            {
                writer.WriteRawValue(spi.SerializedValue);
            }
        }
Exemplo n.º 4
0
        public void SetProperty <T>(string propertyName, T value)
        {
            if (Properties == null)
            {
                Properties = new Dictionary <string, SerializedPropertyInfo>();
            }

            bool isString = typeof(T) == typeof(string);

            string serializedValue;

            if (value == null)
            {
                serializedValue = NullValue;
            }
            else
            {
                if (isString)
                {
                    serializedValue = JsonConvert.ToString(value);
                }
                else
                {
                    // Use the appropriate serializer settings
                    JsonSerializerSettings settings = null;

                    if (propertyName.StartsWith("sarifv2/"))
                    {
                        settings = SarifTransformerUtilities.JsonSettingsV2Compact;
                    }
                    else if (propertyName.StartsWith("sarifv1/"))
                    {
                        settings = SarifTransformerUtilities.JsonSettingsV1Compact;
                    }

                    serializedValue = JsonConvert.SerializeObject(value, settings);
                }
            }

            Properties[propertyName] = new SerializedPropertyInfo(serializedValue, isString);
        }
Exemplo n.º 5
0
        public void SetPropertiesFrom(IPropertyBagHolder other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            // We need the concrete class because the IPropertyBagHolder interface
            // doesn't expose the raw Properties array.
            PropertyBagHolder otherHolder = other as PropertyBagHolder;

            Debug.Assert(otherHolder != null);

            Properties = other.PropertyNames.Count > 0 ? new Dictionary <string, SerializedPropertyInfo>() : null;

            foreach (string propertyName in other.PropertyNames)
            {
                SerializedPropertyInfo otherInfo = otherHolder.Properties[propertyName];
                Properties[propertyName] = new SerializedPropertyInfo(otherInfo.SerializedValue, otherInfo.IsString);
            }
        }
Exemplo n.º 6
0
        public void SetProperty <T>(string propertyName, T value)
        {
            if (Properties == null)
            {
                Properties = new Dictionary <string, SerializedPropertyInfo>();
            }

            bool isString = typeof(T) == typeof(string);

            string serializedValue;

            if (value == null)
            {
                serializedValue = NullValue;
            }
            else
            {
                serializedValue = isString
                    ? JsonConvert.ToString(value)
                    : JsonConvert.SerializeObject(value);
            }

            Properties[propertyName] = new SerializedPropertyInfo(serializedValue, isString);
        }