diff --git a/Src/Newtonsoft.Json/Bson/BsonReader.cs b/Src/Newtonsoft.Json/Bson/BsonReader.cs index bb36ff8b..5b262e96 100644 --- a/Src/Newtonsoft.Json/Bson/BsonReader.cs +++ b/Src/Newtonsoft.Json/Bson/BsonReader.cs @@ -230,12 +230,12 @@ namespace Newtonsoft.Json.Bson { base.Close(); - if (CloseInput && _reader != null) + if (CloseInput) { #if !(DOTNET || PORTABLE40 || PORTABLE) - _reader.Close(); + _reader?.Close(); #else - _reader.Dispose(); + _reader?.Dispose(); #endif } } diff --git a/Src/Newtonsoft.Json/Bson/BsonWriter.cs b/Src/Newtonsoft.Json/Bson/BsonWriter.cs index 71bd038a..a58743f0 100644 --- a/Src/Newtonsoft.Json/Bson/BsonWriter.cs +++ b/Src/Newtonsoft.Json/Bson/BsonWriter.cs @@ -177,9 +177,9 @@ namespace Newtonsoft.Json.Bson { base.Close(); - if (CloseOutput && _writer != null) + if (CloseOutput) { - _writer.Close(); + _writer?.Close(); } } diff --git a/Src/Newtonsoft.Json/JsonSerializer.cs b/Src/Newtonsoft.Json/JsonSerializer.cs index 00d10483..2ce01065 100644 --- a/Src/Newtonsoft.Json/JsonSerializer.cs +++ b/Src/Newtonsoft.Json/JsonSerializer.cs @@ -1182,11 +1182,7 @@ namespace Newtonsoft.Json internal void OnError(ErrorEventArgs e) { - EventHandler error = Error; - if (error != null) - { - error(this, e); - } + Error?.Invoke(this, e); } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonSerializerSettings.cs b/Src/Newtonsoft.Json/JsonSerializerSettings.cs index 1899aea1..be569074 100644 --- a/Src/Newtonsoft.Json/JsonSerializerSettings.cs +++ b/Src/Newtonsoft.Json/JsonSerializerSettings.cs @@ -231,12 +231,7 @@ namespace Newtonsoft.Json { get { - if (ReferenceResolverProvider == null) - { - return null; - } - - return ReferenceResolverProvider(); + return ReferenceResolverProvider?.Invoke(); } set { diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index bd4f0868..389ab585 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.cs @@ -2414,12 +2414,12 @@ namespace Newtonsoft.Json _chars = null; } - if (CloseInput && _reader != null) + if (CloseInput) { #if !(DOTNET || PORTABLE40 || PORTABLE) - _reader.Close(); + _reader?.Close(); #else - _reader.Dispose(); + _reader?.Dispose(); #endif } diff --git a/Src/Newtonsoft.Json/JsonTextWriter.cs b/Src/Newtonsoft.Json/JsonTextWriter.cs index 304609d1..21d78393 100644 --- a/Src/Newtonsoft.Json/JsonTextWriter.cs +++ b/Src/Newtonsoft.Json/JsonTextWriter.cs @@ -184,12 +184,12 @@ namespace Newtonsoft.Json _writeBuffer = null; } - if (CloseOutput && _writer != null) + if (CloseOutput) { #if !(DOTNET || PORTABLE40 || PORTABLE) - _writer.Close(); + _writer?.Close(); #else - _writer.Dispose(); + _writer?.Dispose(); #endif } } diff --git a/Src/Newtonsoft.Json/JsonValidatingReader.cs b/Src/Newtonsoft.Json/JsonValidatingReader.cs index a9dbc03f..259be33e 100644 --- a/Src/Newtonsoft.Json/JsonValidatingReader.cs +++ b/Src/Newtonsoft.Json/JsonValidatingReader.cs @@ -97,7 +97,7 @@ namespace Newtonsoft.Json private IEnumerable GetRequiredProperties(JsonSchemaModel schema) { - if (schema == null || schema.Properties == null) + if (schema?.Properties == null) { return Enumerable.Empty(); } @@ -358,9 +358,9 @@ namespace Newtonsoft.Json public override void Close() { base.Close(); - if (CloseInput && _reader != null) + if (CloseInput) { - _reader.Close(); + _reader?.Close(); } } diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs index fd5d771e..1b3d5ddb 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.cs @@ -139,11 +139,7 @@ namespace Newtonsoft.Json.Linq /// The instance containing the event data. protected virtual void OnAddingNew(AddingNewEventArgs e) { - AddingNewEventHandler handler = _addingNew; - if (handler != null) - { - handler(this, e); - } + _addingNew?.Invoke(this, e); } /// diff --git a/Src/Newtonsoft.Json/Linq/JProperty.cs b/Src/Newtonsoft.Json/Linq/JProperty.cs index ba4422fc..c0779528 100644 --- a/Src/Newtonsoft.Json/Linq/JProperty.cs +++ b/Src/Newtonsoft.Json/Linq/JProperty.cs @@ -212,17 +212,11 @@ namespace Newtonsoft.Json.Linq return; } - if (Parent != null) - { - ((JObject)Parent).InternalPropertyChanging(this); - } + ((JObject)Parent)?.InternalPropertyChanging(this); base.SetItem(0, item); - if (Parent != null) - { - ((JObject)Parent).InternalPropertyChanged(this); - } + ((JObject)Parent)?.InternalPropertyChanged(this); } internal override bool RemoveItem(JToken item) @@ -263,15 +257,11 @@ namespace Newtonsoft.Json.Linq internal override void MergeItem(object content, JsonMergeSettings settings) { - JProperty p = content as JProperty; - if (p == null) - { - return; - } + JToken value = (content as JProperty)?.Value; - if (p.Value != null && p.Value.Type != JTokenType.Null) + if (value != null && value.Type != JTokenType.Null) { - Value = p.Value; + Value = value; } } diff --git a/Src/Newtonsoft.Json/Linq/JPropertyKeyedCollection.cs b/Src/Newtonsoft.Json/Linq/JPropertyKeyedCollection.cs index 527fdab5..6477ccf5 100644 --- a/Src/Newtonsoft.Json/Linq/JPropertyKeyedCollection.cs +++ b/Src/Newtonsoft.Json/Linq/JPropertyKeyedCollection.cs @@ -72,10 +72,7 @@ namespace Newtonsoft.Json.Linq { base.ClearItems(); - if (_dictionary != null) - { - _dictionary.Clear(); - } + _dictionary?.Clear(); } public bool Contains(string key) @@ -148,10 +145,7 @@ namespace Newtonsoft.Json.Linq private void RemoveKey(string key) { - if (_dictionary != null) - { - _dictionary.Remove(key); - } + _dictionary?.Remove(key); } protected override void SetItem(int index, JToken item) diff --git a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs index d4e784d0..f40d0ea8 100644 --- a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs +++ b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs @@ -177,13 +177,9 @@ namespace Newtonsoft.Json.Linq /// The name of the property. public override void WritePropertyName(string name) { - JObject o = _parent as JObject; - if (o != null) - { - // avoid duplicate property name exception - // last property name wins - o.Remove(name); - } + // avoid duplicate property name exception + // last property name wins + (_parent as JObject)?.Remove(name); AddParent(new JProperty(name)); diff --git a/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs b/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs index 7c67a503..4656fc48 100644 --- a/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs +++ b/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs @@ -532,10 +532,7 @@ namespace Newtonsoft.Json.Linq.JsonPath { CompositeExpression andExpression = new CompositeExpression { Operator = QueryOperator.And }; - if (parentExpression != null) - { - parentExpression.Expressions.Add(andExpression); - } + parentExpression?.Expressions.Add(andExpression); parentExpression = andExpression; @@ -558,10 +555,7 @@ namespace Newtonsoft.Json.Linq.JsonPath { CompositeExpression orExpression = new CompositeExpression { Operator = QueryOperator.Or }; - if (parentExpression != null) - { - parentExpression.Expressions.Add(orExpression); - } + parentExpression?.Expressions.Add(orExpression); parentExpression = orExpression; diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs index 0a7d2854..3b491516 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs @@ -176,7 +176,7 @@ namespace Newtonsoft.Json.Schema { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute(type); - if (containerAttribute != null && !string.IsNullOrEmpty(containerAttribute.Title)) + if (!string.IsNullOrEmpty(containerAttribute?.Title)) { return containerAttribute.Title; } @@ -188,27 +188,24 @@ namespace Newtonsoft.Json.Schema { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute(type); - if (containerAttribute != null && !string.IsNullOrEmpty(containerAttribute.Description)) + if (!string.IsNullOrEmpty(containerAttribute?.Description)) { return containerAttribute.Description; } #if !(DOTNET || PORTABLE40 || PORTABLE) DescriptionAttribute descriptionAttribute = ReflectionUtils.GetAttribute(type); - if (descriptionAttribute != null) - { - return descriptionAttribute.Description; - } -#endif - + return descriptionAttribute?.Description; +#else return null; +#endif } private string GetTypeId(Type type, bool explicitOnly) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute(type); - if (containerAttribute != null && !string.IsNullOrEmpty(containerAttribute.Id)) + if (!string.IsNullOrEmpty(containerAttribute?.Id)) { return containerAttribute.Id; } diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index ffa61cea..733b7269 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -1427,13 +1427,13 @@ namespace Newtonsoft.Json.Serialization string mappedName; bool hasSpecifiedName; - if (propertyAttribute != null && propertyAttribute.PropertyName != null) + if (propertyAttribute?.PropertyName != null) { mappedName = propertyAttribute.PropertyName; hasSpecifiedName = true; } #if !NET20 - else if (dataMemberAttribute != null && dataMemberAttribute.Name != null) + else if (dataMemberAttribute?.Name != null) { mappedName = dataMemberAttribute.Name; hasSpecifiedName = true; @@ -1542,7 +1542,7 @@ namespace Newtonsoft.Json.Serialization property.ItemIsReference = (propertyAttribute != null) ? propertyAttribute._itemIsReference : null; property.ItemConverter = - (propertyAttribute != null && propertyAttribute.ItemConverterType != null) + (propertyAttribute?.ItemConverterType != null) ? JsonTypeReflector.CreateJsonConverterInstance(propertyAttribute.ItemConverterType, propertyAttribute.ItemConverterParameters) : null; property.ItemReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._itemReferenceLoopHandling : null; diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 603a9752..2bd45965 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -377,11 +377,11 @@ namespace Newtonsoft.Json.Serialization // member attribute converter converter = memberConverter; } - else if (containerProperty != null && containerProperty.ItemConverter != null) + else if (containerProperty?.ItemConverter != null) { converter = containerProperty.ItemConverter; } - else if (containerContract != null && containerContract.ItemConverter != null) + else if (containerContract?.ItemConverter != null) { converter = containerContract.ItemConverter; } diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index ac018dc2..7bc937e1 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs @@ -483,39 +483,36 @@ namespace Newtonsoft.Json.Serialization } } - if (contract.ExtensionDataGetter != null) + IEnumerable> extensionData = contract.ExtensionDataGetter?.Invoke(value); + if (extensionData != null) { - IEnumerable> extensionData = contract.ExtensionDataGetter(value); - if (extensionData != null) + foreach (KeyValuePair e in extensionData) { - foreach (KeyValuePair e in extensionData) + JsonContract keyContract = GetContractSafe(e.Key); + JsonContract valueContract = GetContractSafe(e.Value); + + bool escape; + string propertyName = GetPropertyName(writer, e.Key, keyContract, out escape); + + propertyName = (contract.ExtensionDataNameResolver != null) + ? contract.ExtensionDataNameResolver(propertyName) + : propertyName; + + if (ShouldWriteReference(e.Value, null, valueContract, contract, member)) { - JsonContract keyContract = GetContractSafe(e.Key); - JsonContract valueContract = GetContractSafe(e.Value); - - bool escape; - string propertyName = GetPropertyName(writer, e.Key, keyContract, out escape); - - propertyName = (contract.ExtensionDataNameResolver != null) - ? contract.ExtensionDataNameResolver(propertyName) - : propertyName; - - if (ShouldWriteReference(e.Value, null, valueContract, contract, member)) + writer.WritePropertyName(propertyName); + WriteReference(writer, e.Value); + } + else + { + if (!CheckForCircularReference(writer, e.Value, null, valueContract, contract, member)) { - writer.WritePropertyName(propertyName); - WriteReference(writer, e.Value); + continue; } - else - { - if (!CheckForCircularReference(writer, e.Value, null, valueContract, contract, member)) - { - continue; - } - writer.WritePropertyName(propertyName); + writer.WritePropertyName(propertyName); - SerializeValue(writer, e.Value, valueContract, null, contract, member); - } + SerializeValue(writer, e.Value, valueContract, null, contract, member); } } } diff --git a/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs b/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs index 4208513a..5f02146b 100644 --- a/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs +++ b/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs @@ -253,9 +253,9 @@ namespace Newtonsoft.Json.Utilities throw new NotSupportedException(); } #endif - else if (_genericDictionary != null) + else { - _genericDictionary.Add(item); + _genericDictionary?.Add(item); } } diff --git a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs index 68dc0fd9..9d33387d 100644 --- a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs @@ -54,12 +54,7 @@ namespace Newtonsoft.Json.Utilities public static void ReturnBuffer(IArrayPool bufferPool, char[] buffer) { - if (bufferPool == null) - { - return; - } - - bufferPool.Return(buffer); + bufferPool?.Return(buffer); } public static char[] EnsureBufferSize(IArrayPool bufferPool, int size, char[] buffer) diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index b31c31bd..286bbe1e 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -123,13 +123,7 @@ namespace Newtonsoft.Json.Utilities return m.GetBaseDefinition(); } - m = propertyInfo.GetSetMethod(true); - if (m != null) - { - return m.GetBaseDefinition(); - } - - return null; + return propertyInfo.GetSetMethod(true)?.GetBaseDefinition(); } public static bool IsPublic(PropertyInfo property)