Coalesce nulls rather than null test where applicable. (#1150)
This commit is contained in:
committed by
James Newton-King
parent
2061a8bb9a
commit
f4c17aedb2
@@ -59,13 +59,13 @@ namespace Newtonsoft.Json.Converters
|
|||||||
string keyName = (string)_reflectionObject.GetValue(value, KeyPropertyName);
|
string keyName = (string)_reflectionObject.GetValue(value, KeyPropertyName);
|
||||||
object keyValue = _reflectionObject.GetValue(value, ValuePropertyName);
|
object keyValue = _reflectionObject.GetValue(value, ValuePropertyName);
|
||||||
|
|
||||||
Type keyValueType = (keyValue != null) ? keyValue.GetType() : null;
|
Type keyValueType = keyValue?.GetType();
|
||||||
|
|
||||||
writer.WriteStartObject();
|
writer.WriteStartObject();
|
||||||
writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(KeyPropertyName) : KeyPropertyName);
|
writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(KeyPropertyName) : KeyPropertyName);
|
||||||
writer.WriteValue(keyName);
|
writer.WriteValue(keyName);
|
||||||
writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(TypePropertyName) : TypePropertyName);
|
writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(TypePropertyName) : TypePropertyName);
|
||||||
writer.WriteValue((keyValueType != null) ? keyValueType.FullName : null);
|
writer.WriteValue(keyValueType?.FullName);
|
||||||
|
|
||||||
writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(ValuePropertyName) : ValuePropertyName);
|
writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(ValuePropertyName) : ValuePropertyName);
|
||||||
|
|
||||||
|
|||||||
@@ -1649,7 +1649,7 @@ namespace Newtonsoft.Json.Converters
|
|||||||
{
|
{
|
||||||
if (reader.TokenType == JsonToken.String)
|
if (reader.TokenType == JsonToken.String)
|
||||||
{
|
{
|
||||||
return (reader.Value != null) ? reader.Value.ToString() : null;
|
return reader.Value?.ToString();
|
||||||
}
|
}
|
||||||
else if (reader.TokenType == JsonToken.Integer)
|
else if (reader.TokenType == JsonToken.Integer)
|
||||||
{
|
{
|
||||||
@@ -1837,7 +1837,7 @@ namespace Newtonsoft.Json.Converters
|
|||||||
throw JsonSerializationException.Create(reader, "Unexpected JsonToken: " + reader.TokenType);
|
throw JsonSerializationException.Create(reader, "Unexpected JsonToken: " + reader.TokenType);
|
||||||
}
|
}
|
||||||
|
|
||||||
attributeValue = (reader.Value != null) ? reader.Value.ToString() : null;
|
attributeValue = reader.Value?.ToString();
|
||||||
attributeNameValues.Add(jsonPrefix + ":" + attributeName, attributeValue);
|
attributeNameValues.Add(jsonPrefix + ":" + attributeName, attributeValue);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -271,7 +271,7 @@ namespace Newtonsoft.Json
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual Type ValueType
|
public virtual Type ValueType
|
||||||
{
|
{
|
||||||
get { return (_value != null) ? _value.GetType() : null; }
|
get { return _value?.GetType(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -597,8 +597,7 @@ namespace Newtonsoft.Json
|
|||||||
public static JsonSerializer CreateDefault()
|
public static JsonSerializer CreateDefault()
|
||||||
{
|
{
|
||||||
// copy static to local variable to avoid concurrency issues
|
// copy static to local variable to avoid concurrency issues
|
||||||
Func<JsonSerializerSettings> defaultSettingsCreator = JsonConvert.DefaultSettings;
|
JsonSerializerSettings defaultSettings = JsonConvert.DefaultSettings?.Invoke();
|
||||||
JsonSerializerSettings defaultSettings = (defaultSettingsCreator != null) ? defaultSettingsCreator() : null;
|
|
||||||
|
|
||||||
return Create(defaultSettings);
|
return Create(defaultSettings);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -528,7 +528,7 @@ namespace Newtonsoft.Json
|
|||||||
WritePropertyName(value.ToString());
|
WritePropertyName(value.ToString());
|
||||||
break;
|
break;
|
||||||
case JsonToken.Comment:
|
case JsonToken.Comment:
|
||||||
WriteComment((value != null) ? value.ToString() : null);
|
WriteComment(value?.ToString());
|
||||||
break;
|
break;
|
||||||
case JsonToken.Integer:
|
case JsonToken.Integer:
|
||||||
ValidationUtils.ArgumentNotNull(value, nameof(value));
|
ValidationUtils.ArgumentNotNull(value, nameof(value));
|
||||||
@@ -599,7 +599,7 @@ namespace Newtonsoft.Json
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case JsonToken.Raw:
|
case JsonToken.Raw:
|
||||||
WriteRawValue((value != null) ? value.ToString() : null);
|
WriteRawValue(value?.ToString());
|
||||||
break;
|
break;
|
||||||
case JsonToken.Bytes:
|
case JsonToken.Bytes:
|
||||||
ValidationUtils.ArgumentNotNull(value, nameof(value));
|
ValidationUtils.ArgumentNotNull(value, nameof(value));
|
||||||
|
|||||||
@@ -322,7 +322,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
|
|
||||||
JProperty property = Property(propertyName);
|
JProperty property = Property(propertyName);
|
||||||
|
|
||||||
return (property != null) ? property.Value : null;
|
return property?.Value;
|
||||||
}
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -240,7 +240,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
|
|
||||||
private string SafeToString(object value)
|
private string SafeToString(object value)
|
||||||
{
|
{
|
||||||
return (value != null) ? value.ToString() : null;
|
return value?.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IJsonLineInfo.HasLineInfo()
|
bool IJsonLineInfo.HasLineInfo()
|
||||||
|
|||||||
@@ -423,7 +423,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
{
|
{
|
||||||
if (operation == ExpressionType.Add || operation == ExpressionType.AddAssign)
|
if (operation == ExpressionType.Add || operation == ExpressionType.AddAssign)
|
||||||
{
|
{
|
||||||
result = ((objA != null) ? objA.ToString() : null) + ((objB != null) ? objB.ToString() : null);
|
result = objA?.ToString() + objB?.ToString();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -712,8 +712,8 @@ namespace Newtonsoft.Json.Linq
|
|||||||
get { return _value; }
|
get { return _value; }
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
Type currentType = (_value != null) ? _value.GetType() : null;
|
Type currentType = _value?.GetType();
|
||||||
Type newType = (value != null) ? value.GetType() : null;
|
Type newType = value?.GetType();
|
||||||
|
|
||||||
if (currentType != newType)
|
if (currentType != newType)
|
||||||
{
|
{
|
||||||
@@ -744,10 +744,10 @@ namespace Newtonsoft.Json.Linq
|
|||||||
switch (_valueType)
|
switch (_valueType)
|
||||||
{
|
{
|
||||||
case JTokenType.Comment:
|
case JTokenType.Comment:
|
||||||
writer.WriteComment((_value != null) ? _value.ToString() : null);
|
writer.WriteComment(_value?.ToString());
|
||||||
return;
|
return;
|
||||||
case JTokenType.Raw:
|
case JTokenType.Raw:
|
||||||
writer.WriteRawValue((_value != null) ? _value.ToString() : null);
|
writer.WriteRawValue(_value?.ToString());
|
||||||
return;
|
return;
|
||||||
case JTokenType.Null:
|
case JTokenType.Null:
|
||||||
writer.WriteNull();
|
writer.WriteNull();
|
||||||
@@ -798,7 +798,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
case JTokenType.String:
|
case JTokenType.String:
|
||||||
writer.WriteValue((_value != null) ? _value.ToString() : null);
|
writer.WriteValue(_value?.ToString());
|
||||||
return;
|
return;
|
||||||
case JTokenType.Boolean:
|
case JTokenType.Boolean:
|
||||||
writer.WriteValue(Convert.ToBoolean(_value, CultureInfo.InvariantCulture));
|
writer.WriteValue(Convert.ToBoolean(_value, CultureInfo.InvariantCulture));
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
&& string.Equals(reader.Value.ToString(), JsonTypeReflector.IdPropertyName, StringComparison.Ordinal))
|
&& string.Equals(reader.Value.ToString(), JsonTypeReflector.IdPropertyName, StringComparison.Ordinal))
|
||||||
{
|
{
|
||||||
reader.ReadAndAssert();
|
reader.ReadAndAssert();
|
||||||
id = (reader.Value != null) ? reader.Value.ToString() : null;
|
id = reader.Value?.ToString();
|
||||||
reader.ReadAndAssert();
|
reader.ReadAndAssert();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -705,7 +705,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
throw JsonSerializationException.Create(reader, "JSON reference {0} property must have a string or null value.".FormatWith(CultureInfo.InvariantCulture, JsonTypeReflector.RefPropertyName));
|
throw JsonSerializationException.Create(reader, "JSON reference {0} property must have a string or null value.".FormatWith(CultureInfo.InvariantCulture, JsonTypeReflector.RefPropertyName));
|
||||||
}
|
}
|
||||||
|
|
||||||
string reference = (reader.Value != null) ? reader.Value.ToString() : null;
|
string reference = reader.Value?.ToString();
|
||||||
|
|
||||||
reader.ReadAndAssert();
|
reader.ReadAndAssert();
|
||||||
|
|
||||||
@@ -745,7 +745,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
{
|
{
|
||||||
reader.ReadAndAssert();
|
reader.ReadAndAssert();
|
||||||
|
|
||||||
id = (reader.Value != null) ? reader.Value.ToString() : null;
|
id = reader.Value?.ToString();
|
||||||
|
|
||||||
reader.ReadAndAssert();
|
reader.ReadAndAssert();
|
||||||
metadataProperty = true;
|
metadataProperty = true;
|
||||||
@@ -771,9 +771,9 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
private void ResolveTypeName(JsonReader reader, ref Type objectType, ref JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, string qualifiedTypeName)
|
private void ResolveTypeName(JsonReader reader, ref Type objectType, ref JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, string qualifiedTypeName)
|
||||||
{
|
{
|
||||||
TypeNameHandling resolvedTypeNameHandling =
|
TypeNameHandling resolvedTypeNameHandling =
|
||||||
((member != null) ? member.TypeNameHandling : null)
|
member?.TypeNameHandling
|
||||||
?? ((containerContract != null) ? containerContract.ItemTypeNameHandling : null)
|
?? containerContract?.ItemTypeNameHandling
|
||||||
?? ((containerMember != null) ? containerMember.ItemTypeNameHandling : null)
|
?? containerMember?.ItemTypeNameHandling
|
||||||
?? Serializer._typeNameHandling;
|
?? Serializer._typeNameHandling;
|
||||||
|
|
||||||
if (resolvedTypeNameHandling != TypeNameHandling.None)
|
if (resolvedTypeNameHandling != TypeNameHandling.None)
|
||||||
|
|||||||
@@ -154,9 +154,9 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonConverter converter =
|
JsonConverter converter =
|
||||||
((member != null) ? member.Converter : null) ??
|
member?.Converter ??
|
||||||
((containerProperty != null) ? containerProperty.ItemConverter : null) ??
|
containerProperty?.ItemConverter ??
|
||||||
((containerContract != null) ? containerContract.ItemConverter : null) ??
|
containerContract?.ItemConverter ??
|
||||||
valueContract.Converter ??
|
valueContract.Converter ??
|
||||||
Serializer.GetMatchingConverter(valueContract.UnderlyingType) ??
|
Serializer.GetMatchingConverter(valueContract.UnderlyingType) ??
|
||||||
valueContract.InternalConverter;
|
valueContract.InternalConverter;
|
||||||
@@ -554,7 +554,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
if (memberValue == null)
|
if (memberValue == null)
|
||||||
{
|
{
|
||||||
JsonObjectContract objectContract = contract as JsonObjectContract;
|
JsonObjectContract objectContract = contract as JsonObjectContract;
|
||||||
Required resolvedRequired = property._required ?? ((objectContract != null) ? objectContract.ItemRequired : null) ?? Required.Default;
|
Required resolvedRequired = property._required ?? objectContract?.ItemRequired ?? Required.Default;
|
||||||
if (resolvedRequired == Required.Always)
|
if (resolvedRequired == Required.Always)
|
||||||
{
|
{
|
||||||
throw JsonSerializationException.Create(null, writer.ContainerPath, "Cannot write a null value for property '{0}'. Property requires a value.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName), null);
|
throw JsonSerializationException.Create(null, writer.ContainerPath, "Cannot write a null value for property '{0}'. Property requires a value.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName), null);
|
||||||
@@ -1000,9 +1000,9 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
private bool ShouldWriteType(TypeNameHandling typeNameHandlingFlag, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
|
private bool ShouldWriteType(TypeNameHandling typeNameHandlingFlag, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
|
||||||
{
|
{
|
||||||
TypeNameHandling resolvedTypeNameHandling =
|
TypeNameHandling resolvedTypeNameHandling =
|
||||||
((member != null) ? member.TypeNameHandling : null)
|
member?.TypeNameHandling
|
||||||
?? ((containerProperty != null) ? containerProperty.ItemTypeNameHandling : null)
|
?? containerProperty?.ItemTypeNameHandling
|
||||||
?? ((containerContract != null) ? containerContract.ItemTypeNameHandling : null)
|
?? containerContract?.ItemTypeNameHandling
|
||||||
?? Serializer._typeNameHandling;
|
?? Serializer._typeNameHandling;
|
||||||
|
|
||||||
if (HasFlag(resolvedTypeNameHandling, typeNameHandlingFlag))
|
if (HasFlag(resolvedTypeNameHandling, typeNameHandlingFlag))
|
||||||
|
|||||||
@@ -649,7 +649,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
|
|
||||||
private static object EnsureTypeAssignable(object value, Type initialType, Type targetType)
|
private static object EnsureTypeAssignable(object value, Type initialType, Type targetType)
|
||||||
{
|
{
|
||||||
Type valueType = (value != null) ? value.GetType() : null;
|
Type valueType = value?.GetType();
|
||||||
|
|
||||||
if (value != null)
|
if (value != null)
|
||||||
{
|
{
|
||||||
@@ -672,7 +672,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new ArgumentException("Could not cast or convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, (initialType != null) ? initialType.ToString() : "{null}", targetType));
|
throw new ArgumentException("Could not cast or convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, initialType?.ToString() ?? "{null}", targetType));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !(DOTNET || PORTABLE40 || PORTABLE)
|
#if !(DOTNET || PORTABLE40 || PORTABLE)
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
|
|
||||||
public static Type GetObjectType(object v)
|
public static Type GetObjectType(object v)
|
||||||
{
|
{
|
||||||
return (v != null) ? v.GetType() : null;
|
return v?.GetType();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetTypeName(Type t, TypeNameAssemblyFormatHandling assemblyFormat, ISerializationBinder binder)
|
public static string GetTypeName(Type t, TypeNameAssemblyFormatHandling assemblyFormat, ISerializationBinder binder)
|
||||||
@@ -735,7 +735,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
{
|
{
|
||||||
T[] attributes = GetAttributes<T>(attributeProvider, inherit);
|
T[] attributes = GetAttributes<T>(attributeProvider, inherit);
|
||||||
|
|
||||||
return (attributes != null) ? attributes.FirstOrDefault() : null;
|
return attributes?.FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !(DOTNET || PORTABLE)
|
#if !(DOTNET || PORTABLE)
|
||||||
|
|||||||
Reference in New Issue
Block a user