diff --git a/src/NJsonSchema/JsonSchema.Serialization.cs b/src/NJsonSchema/JsonSchema.Serialization.cs index 7232cae00..95fc901b5 100644 --- a/src/NJsonSchema/JsonSchema.Serialization.cs +++ b/src/NJsonSchema/JsonSchema.Serialization.cs @@ -122,11 +122,11 @@ internal object? DiscriminatorRaw { if (value is string) { - Discriminator = (string)value; + Discriminator = (string) value; } else if (value != null) { - DiscriminatorObject = ((JObject)value).ToObject(); + DiscriminatorObject = ((JObject) value).ToObject(); } } } @@ -139,12 +139,12 @@ internal object? DiscriminatorRaw [JsonProperty("exclusiveMaximum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] internal object? ExclusiveMaximumRaw { - get => ExclusiveMaximum ?? (IsExclusiveMaximum ? (object)true : null); + get => ExclusiveMaximum ?? (IsExclusiveMaximum ? (object) true : null); set { if (value is bool) { - IsExclusiveMaximum = (bool)value; + IsExclusiveMaximum = (bool) value; } else if (value != null && (value.Equals("true") || value.Equals("false"))) { @@ -161,12 +161,12 @@ internal object? ExclusiveMaximumRaw [JsonProperty("exclusiveMinimum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] internal object? ExclusiveMinimumRaw { - get => ExclusiveMinimum ?? (IsExclusiveMinimum ? (object)true : null); + get => ExclusiveMinimum ?? (IsExclusiveMinimum ? (object) true : null); set { if (value is bool) { - IsExclusiveMinimum = (bool)value; + IsExclusiveMinimum = (bool) value; } else if (value != null && (value.Equals("true") || value.Equals("false"))) { @@ -200,7 +200,7 @@ internal object? AdditionalItemsRaw { if (value is bool) { - AllowAdditionalItems = (bool)value; + AllowAdditionalItems = (bool) value; } else if (value != null && (value.Equals("true") || value.Equals("false"))) { @@ -254,7 +254,7 @@ internal object? AdditionalPropertiesRaw { if (value is bool) { - AllowAdditionalProperties = (bool)value; + AllowAdditionalProperties = (bool) value; } else if (value != null && (value.Equals("true") || value.Equals("false"))) { @@ -288,7 +288,7 @@ internal object? ItemsRaw { if (value is JArray) { - Items = new ObservableCollection(((JArray)value).Select(FromJsonWithCurrentSettings)); + Items = new ObservableCollection(((JArray) value).Select(FromJsonWithCurrentSettings)); } else if (value != null) { @@ -315,7 +315,7 @@ internal object? TypeRaw { if (value is JArray) { - Type = ((JArray)value).Aggregate(JsonObjectType.None, (type, token) => type | ConvertStringToJsonObjectType(token.ToString())); + Type = ((JArray) value).Aggregate(JsonObjectType.None, (type, token) => type | ConvertStringToJsonObjectType(token.ToString())); } else { @@ -367,8 +367,8 @@ internal IDictionary? PropertiesRaw internal IDictionary? PatternPropertiesRaw { get => _patternProperties is { Count: > 0 } - ? PatternProperties.ToDictionary(p => p.Key, p => p.Value) - : null; + ? PatternProperties.ToDictionary(p => p.Key, p => p.Value) + : null; set => PatternProperties = value != null ? new ObservableDictionary(value!) : []; } @@ -460,7 +460,7 @@ private void RegisterSchemaCollection(ObservableCollection? oldColle private void InitializeSchemaCollection(object? sender, NotifyCollectionChangedEventArgs? args) { - if (sender is ObservableDictionary properties) + if (sender is ObservableDictionary { Count: > 0 } properties) { foreach (var property in properties) { @@ -468,27 +468,38 @@ private void InitializeSchemaCollection(object? sender, NotifyCollectionChangedE property.Value.Parent = this; } } - else if (sender is ObservableCollection items) + else if (sender is ObservableCollection { Count: > 0 } items) { foreach (var item in items) { item.Parent = this; } } - else if (sender is ObservableDictionary collection) + else if (sender is ObservableDictionary { Count: > 0 } collection) { - foreach (var pair in collection.ToArray()) + List? keysToRemove = null; + foreach (var pair in collection) { if (pair.Value == null) { - collection.Remove(pair.Key); + keysToRemove ??= []; + keysToRemove.Add(pair.Key); } else { pair.Value.Parent = this; } } + + if (keysToRemove != null) + { + for (var i = 0; i < keysToRemove.Count; i++) + { + var key = keysToRemove[i]; + collection.Remove(key); + } + } } } } -} +} \ No newline at end of file