Use null-propogation where applicable and useful.

Primarily where it means one fewer field-access or simplifies
event delegate invocation, though one or two cases have just the
lesser advantage of allowing an IL stack dup rather than local access.
This commit is contained in:
Jon Hanna
2017-01-03 20:08:12 +00:00
parent 3ae09a80f0
commit b567a066e6
19 changed files with 67 additions and 123 deletions
+3 -3
View File
@@ -230,12 +230,12 @@ namespace Newtonsoft.Json.Bson
{ {
base.Close(); base.Close();
if (CloseInput && _reader != null) if (CloseInput)
{ {
#if !(DOTNET || PORTABLE40 || PORTABLE) #if !(DOTNET || PORTABLE40 || PORTABLE)
_reader.Close(); _reader?.Close();
#else #else
_reader.Dispose(); _reader?.Dispose();
#endif #endif
} }
} }
+2 -2
View File
@@ -177,9 +177,9 @@ namespace Newtonsoft.Json.Bson
{ {
base.Close(); base.Close();
if (CloseOutput && _writer != null) if (CloseOutput)
{ {
_writer.Close(); _writer?.Close();
} }
} }
+1 -5
View File
@@ -1182,11 +1182,7 @@ namespace Newtonsoft.Json
internal void OnError(ErrorEventArgs e) internal void OnError(ErrorEventArgs e)
{ {
EventHandler<ErrorEventArgs> error = Error; Error?.Invoke(this, e);
if (error != null)
{
error(this, e);
}
} }
} }
} }
@@ -231,12 +231,7 @@ namespace Newtonsoft.Json
{ {
get get
{ {
if (ReferenceResolverProvider == null) return ReferenceResolverProvider?.Invoke();
{
return null;
}
return ReferenceResolverProvider();
} }
set set
{ {
+3 -3
View File
@@ -2414,12 +2414,12 @@ namespace Newtonsoft.Json
_chars = null; _chars = null;
} }
if (CloseInput && _reader != null) if (CloseInput)
{ {
#if !(DOTNET || PORTABLE40 || PORTABLE) #if !(DOTNET || PORTABLE40 || PORTABLE)
_reader.Close(); _reader?.Close();
#else #else
_reader.Dispose(); _reader?.Dispose();
#endif #endif
} }
+3 -3
View File
@@ -184,12 +184,12 @@ namespace Newtonsoft.Json
_writeBuffer = null; _writeBuffer = null;
} }
if (CloseOutput && _writer != null) if (CloseOutput)
{ {
#if !(DOTNET || PORTABLE40 || PORTABLE) #if !(DOTNET || PORTABLE40 || PORTABLE)
_writer.Close(); _writer?.Close();
#else #else
_writer.Dispose(); _writer?.Dispose();
#endif #endif
} }
} }
+3 -3
View File
@@ -97,7 +97,7 @@ namespace Newtonsoft.Json
private IEnumerable<string> GetRequiredProperties(JsonSchemaModel schema) private IEnumerable<string> GetRequiredProperties(JsonSchemaModel schema)
{ {
if (schema == null || schema.Properties == null) if (schema?.Properties == null)
{ {
return Enumerable.Empty<string>(); return Enumerable.Empty<string>();
} }
@@ -358,9 +358,9 @@ namespace Newtonsoft.Json
public override void Close() public override void Close()
{ {
base.Close(); base.Close();
if (CloseInput && _reader != null) if (CloseInput)
{ {
_reader.Close(); _reader?.Close();
} }
} }
+1 -5
View File
@@ -139,11 +139,7 @@ namespace Newtonsoft.Json.Linq
/// <param name="e">The <see cref="AddingNewEventArgs"/> instance containing the event data.</param> /// <param name="e">The <see cref="AddingNewEventArgs"/> instance containing the event data.</param>
protected virtual void OnAddingNew(AddingNewEventArgs e) protected virtual void OnAddingNew(AddingNewEventArgs e)
{ {
AddingNewEventHandler handler = _addingNew; _addingNew?.Invoke(this, e);
if (handler != null)
{
handler(this, e);
}
} }
/// <summary> /// <summary>
+5 -15
View File
@@ -212,17 +212,11 @@ namespace Newtonsoft.Json.Linq
return; return;
} }
if (Parent != null) ((JObject)Parent)?.InternalPropertyChanging(this);
{
((JObject)Parent).InternalPropertyChanging(this);
}
base.SetItem(0, item); base.SetItem(0, item);
if (Parent != null) ((JObject)Parent)?.InternalPropertyChanged(this);
{
((JObject)Parent).InternalPropertyChanged(this);
}
} }
internal override bool RemoveItem(JToken item) internal override bool RemoveItem(JToken item)
@@ -263,15 +257,11 @@ namespace Newtonsoft.Json.Linq
internal override void MergeItem(object content, JsonMergeSettings settings) internal override void MergeItem(object content, JsonMergeSettings settings)
{ {
JProperty p = content as JProperty; JToken value = (content as JProperty)?.Value;
if (p == null)
{
return;
}
if (p.Value != null && p.Value.Type != JTokenType.Null) if (value != null && value.Type != JTokenType.Null)
{ {
Value = p.Value; Value = value;
} }
} }
@@ -72,10 +72,7 @@ namespace Newtonsoft.Json.Linq
{ {
base.ClearItems(); base.ClearItems();
if (_dictionary != null) _dictionary?.Clear();
{
_dictionary.Clear();
}
} }
public bool Contains(string key) public bool Contains(string key)
@@ -148,10 +145,7 @@ namespace Newtonsoft.Json.Linq
private void RemoveKey(string key) private void RemoveKey(string key)
{ {
if (_dictionary != null) _dictionary?.Remove(key);
{
_dictionary.Remove(key);
}
} }
protected override void SetItem(int index, JToken item) protected override void SetItem(int index, JToken item)
+3 -7
View File
@@ -177,13 +177,9 @@ namespace Newtonsoft.Json.Linq
/// <param name="name">The name of the property.</param> /// <param name="name">The name of the property.</param>
public override void WritePropertyName(string name) public override void WritePropertyName(string name)
{ {
JObject o = _parent as JObject; // avoid duplicate property name exception
if (o != null) // last property name wins
{ (_parent as JObject)?.Remove(name);
// avoid duplicate property name exception
// last property name wins
o.Remove(name);
}
AddParent(new JProperty(name)); AddParent(new JProperty(name));
+2 -8
View File
@@ -532,10 +532,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
{ {
CompositeExpression andExpression = new CompositeExpression { Operator = QueryOperator.And }; CompositeExpression andExpression = new CompositeExpression { Operator = QueryOperator.And };
if (parentExpression != null) parentExpression?.Expressions.Add(andExpression);
{
parentExpression.Expressions.Add(andExpression);
}
parentExpression = andExpression; parentExpression = andExpression;
@@ -558,10 +555,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
{ {
CompositeExpression orExpression = new CompositeExpression { Operator = QueryOperator.Or }; CompositeExpression orExpression = new CompositeExpression { Operator = QueryOperator.Or };
if (parentExpression != null) parentExpression?.Expressions.Add(orExpression);
{
parentExpression.Expressions.Add(orExpression);
}
parentExpression = orExpression; parentExpression = orExpression;
@@ -176,7 +176,7 @@ namespace Newtonsoft.Json.Schema
{ {
JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute<JsonContainerAttribute>(type); JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute<JsonContainerAttribute>(type);
if (containerAttribute != null && !string.IsNullOrEmpty(containerAttribute.Title)) if (!string.IsNullOrEmpty(containerAttribute?.Title))
{ {
return containerAttribute.Title; return containerAttribute.Title;
} }
@@ -188,27 +188,24 @@ namespace Newtonsoft.Json.Schema
{ {
JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute<JsonContainerAttribute>(type); JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute<JsonContainerAttribute>(type);
if (containerAttribute != null && !string.IsNullOrEmpty(containerAttribute.Description)) if (!string.IsNullOrEmpty(containerAttribute?.Description))
{ {
return containerAttribute.Description; return containerAttribute.Description;
} }
#if !(DOTNET || PORTABLE40 || PORTABLE) #if !(DOTNET || PORTABLE40 || PORTABLE)
DescriptionAttribute descriptionAttribute = ReflectionUtils.GetAttribute<DescriptionAttribute>(type); DescriptionAttribute descriptionAttribute = ReflectionUtils.GetAttribute<DescriptionAttribute>(type);
if (descriptionAttribute != null) return descriptionAttribute?.Description;
{ #else
return descriptionAttribute.Description;
}
#endif
return null; return null;
#endif
} }
private string GetTypeId(Type type, bool explicitOnly) private string GetTypeId(Type type, bool explicitOnly)
{ {
JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute<JsonContainerAttribute>(type); JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute<JsonContainerAttribute>(type);
if (containerAttribute != null && !string.IsNullOrEmpty(containerAttribute.Id)) if (!string.IsNullOrEmpty(containerAttribute?.Id))
{ {
return containerAttribute.Id; return containerAttribute.Id;
} }
@@ -1427,13 +1427,13 @@ namespace Newtonsoft.Json.Serialization
string mappedName; string mappedName;
bool hasSpecifiedName; bool hasSpecifiedName;
if (propertyAttribute != null && propertyAttribute.PropertyName != null) if (propertyAttribute?.PropertyName != null)
{ {
mappedName = propertyAttribute.PropertyName; mappedName = propertyAttribute.PropertyName;
hasSpecifiedName = true; hasSpecifiedName = true;
} }
#if !NET20 #if !NET20
else if (dataMemberAttribute != null && dataMemberAttribute.Name != null) else if (dataMemberAttribute?.Name != null)
{ {
mappedName = dataMemberAttribute.Name; mappedName = dataMemberAttribute.Name;
hasSpecifiedName = true; hasSpecifiedName = true;
@@ -1542,7 +1542,7 @@ namespace Newtonsoft.Json.Serialization
property.ItemIsReference = (propertyAttribute != null) ? propertyAttribute._itemIsReference : null; property.ItemIsReference = (propertyAttribute != null) ? propertyAttribute._itemIsReference : null;
property.ItemConverter = property.ItemConverter =
(propertyAttribute != null && propertyAttribute.ItemConverterType != null) (propertyAttribute?.ItemConverterType != null)
? JsonTypeReflector.CreateJsonConverterInstance(propertyAttribute.ItemConverterType, propertyAttribute.ItemConverterParameters) ? JsonTypeReflector.CreateJsonConverterInstance(propertyAttribute.ItemConverterType, propertyAttribute.ItemConverterParameters)
: null; : null;
property.ItemReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._itemReferenceLoopHandling : null; property.ItemReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._itemReferenceLoopHandling : null;
@@ -377,11 +377,11 @@ namespace Newtonsoft.Json.Serialization
// member attribute converter // member attribute converter
converter = memberConverter; converter = memberConverter;
} }
else if (containerProperty != null && containerProperty.ItemConverter != null) else if (containerProperty?.ItemConverter != null)
{ {
converter = containerProperty.ItemConverter; converter = containerProperty.ItemConverter;
} }
else if (containerContract != null && containerContract.ItemConverter != null) else if (containerContract?.ItemConverter != null)
{ {
converter = containerContract.ItemConverter; converter = containerContract.ItemConverter;
} }
@@ -483,39 +483,36 @@ namespace Newtonsoft.Json.Serialization
} }
} }
if (contract.ExtensionDataGetter != null) IEnumerable<KeyValuePair<object, object>> extensionData = contract.ExtensionDataGetter?.Invoke(value);
if (extensionData != null)
{ {
IEnumerable<KeyValuePair<object, object>> extensionData = contract.ExtensionDataGetter(value); foreach (KeyValuePair<object, object> e in extensionData)
if (extensionData != null)
{ {
foreach (KeyValuePair<object, object> 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); writer.WritePropertyName(propertyName);
JsonContract valueContract = GetContractSafe(e.Value); WriteReference(writer, e.Value);
}
bool escape; else
string propertyName = GetPropertyName(writer, e.Key, keyContract, out escape); {
if (!CheckForCircularReference(writer, e.Value, null, valueContract, contract, member))
propertyName = (contract.ExtensionDataNameResolver != null)
? contract.ExtensionDataNameResolver(propertyName)
: propertyName;
if (ShouldWriteReference(e.Value, null, valueContract, contract, member))
{ {
writer.WritePropertyName(propertyName); continue;
WriteReference(writer, e.Value);
} }
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);
}
} }
} }
} }
@@ -253,9 +253,9 @@ namespace Newtonsoft.Json.Utilities
throw new NotSupportedException(); throw new NotSupportedException();
} }
#endif #endif
else if (_genericDictionary != null) else
{ {
_genericDictionary.Add(item); _genericDictionary?.Add(item);
} }
} }
@@ -54,12 +54,7 @@ namespace Newtonsoft.Json.Utilities
public static void ReturnBuffer(IArrayPool<char> bufferPool, char[] buffer) public static void ReturnBuffer(IArrayPool<char> bufferPool, char[] buffer)
{ {
if (bufferPool == null) bufferPool?.Return(buffer);
{
return;
}
bufferPool.Return(buffer);
} }
public static char[] EnsureBufferSize(IArrayPool<char> bufferPool, int size, char[] buffer) public static char[] EnsureBufferSize(IArrayPool<char> bufferPool, int size, char[] buffer)
@@ -123,13 +123,7 @@ namespace Newtonsoft.Json.Utilities
return m.GetBaseDefinition(); return m.GetBaseDefinition();
} }
m = propertyInfo.GetSetMethod(true); return propertyInfo.GetSetMethod(true)?.GetBaseDefinition();
if (m != null)
{
return m.GetBaseDefinition();
}
return null;
} }
public static bool IsPublic(PropertyInfo property) public static bool IsPublic(PropertyInfo property)