Update Newtonsoft.Json project to use nullable reference types (#1950)

This commit is contained in:
James Newton-King
2019-07-29 09:13:42 +12:00
committed by GitHub
parent f940f21804
commit cdf10151d5
169 changed files with 2264 additions and 1859 deletions
@@ -32,6 +32,8 @@ using System.Reflection;
using System.Collections;
using System.Globalization;
using System.Text;
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;
#if !HAVE_LINQ
using Newtonsoft.Json.Utilities.LinqBridge;
#else
@@ -96,7 +98,7 @@ namespace Newtonsoft.Json.Utilities
{
ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo));
MethodInfo m = propertyInfo.GetGetMethod(true);
MethodInfo? m = propertyInfo.GetGetMethod(true);
if (m != null && m.IsVirtual)
{
return true;
@@ -111,11 +113,11 @@ namespace Newtonsoft.Json.Utilities
return false;
}
public static MethodInfo GetBaseDefinition(this PropertyInfo propertyInfo)
public static MethodInfo? GetBaseDefinition(this PropertyInfo propertyInfo)
{
ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo));
MethodInfo m = propertyInfo.GetGetMethod(true);
MethodInfo? m = propertyInfo.GetGetMethod(true);
if (m != null)
{
return m.GetBaseDefinition();
@@ -126,11 +128,13 @@ namespace Newtonsoft.Json.Utilities
public static bool IsPublic(PropertyInfo property)
{
if (property.GetGetMethod() != null && property.GetGetMethod().IsPublic)
var getMethod = property.GetGetMethod();
if (getMethod != null && getMethod.IsPublic)
{
return true;
}
if (property.GetSetMethod() != null && property.GetSetMethod().IsPublic)
var setMethod = property.GetSetMethod();
if (setMethod != null && setMethod.IsPublic)
{
return true;
}
@@ -138,12 +142,12 @@ namespace Newtonsoft.Json.Utilities
return false;
}
public static Type GetObjectType(object v)
public static Type? GetObjectType(object? v)
{
return v?.GetType();
}
public static string GetTypeName(Type t, TypeNameAssemblyFormatHandling assemblyFormat, ISerializationBinder binder)
public static string GetTypeName(Type t, TypeNameAssemblyFormatHandling assemblyFormat, ISerializationBinder? binder)
{
string fullyQualifiedTypeName = GetFullyQualifiedTypeName(t, binder);
@@ -158,11 +162,11 @@ namespace Newtonsoft.Json.Utilities
}
}
private static string GetFullyQualifiedTypeName(Type t, ISerializationBinder binder)
private static string GetFullyQualifiedTypeName(Type t, ISerializationBinder? binder)
{
if (binder != null)
{
binder.BindToName(t, out string assemblyName, out string typeName);
binder.BindToName(t, out string? assemblyName, out string? typeName);
#if (NET20 || NET35)
// for older SerializationBinder implementations that didn't have BindToName
if (assemblyName == null & typeName == null)
@@ -294,7 +298,7 @@ namespace Newtonsoft.Json.Utilities
return ImplementsGenericDefinition(type, genericInterfaceDefinition, out _);
}
public static bool ImplementsGenericDefinition(Type type, Type genericInterfaceDefinition, out Type implementingType)
public static bool ImplementsGenericDefinition(Type type, Type genericInterfaceDefinition, [NotNullWhen(true)]out Type? implementingType)
{
ValidationUtils.ArgumentNotNull(type, nameof(type));
ValidationUtils.ArgumentNotNull(genericInterfaceDefinition, nameof(genericInterfaceDefinition));
@@ -341,7 +345,7 @@ namespace Newtonsoft.Json.Utilities
return InheritsGenericDefinition(type, genericClassDefinition, out _);
}
public static bool InheritsGenericDefinition(Type type, Type genericClassDefinition, out Type implementingType)
public static bool InheritsGenericDefinition(Type type, Type genericClassDefinition, out Type? implementingType)
{
ValidationUtils.ArgumentNotNull(type, nameof(type));
ValidationUtils.ArgumentNotNull(genericClassDefinition, nameof(genericClassDefinition));
@@ -354,7 +358,7 @@ namespace Newtonsoft.Json.Utilities
return InheritsGenericDefinitionInternal(type, genericClassDefinition, out implementingType);
}
private static bool InheritsGenericDefinitionInternal(Type currentType, Type genericClassDefinition, out Type implementingType)
private static bool InheritsGenericDefinitionInternal(Type currentType, Type genericClassDefinition, out Type? implementingType)
{
do
{
@@ -377,7 +381,7 @@ namespace Newtonsoft.Json.Utilities
/// </summary>
/// <param name="type">The type.</param>
/// <returns>The type of the typed collection's items.</returns>
public static Type GetCollectionItemType(Type type)
public static Type? GetCollectionItemType(Type type)
{
ValidationUtils.ArgumentNotNull(type, nameof(type));
@@ -385,14 +389,14 @@ namespace Newtonsoft.Json.Utilities
{
return type.GetElementType();
}
if (ImplementsGenericDefinition(type, typeof(IEnumerable<>), out Type genericListType))
if (ImplementsGenericDefinition(type, typeof(IEnumerable<>), out Type? genericListType))
{
if (genericListType.IsGenericTypeDefinition())
if (genericListType!.IsGenericTypeDefinition())
{
throw new Exception("Type {0} is not a collection.".FormatWith(CultureInfo.InvariantCulture, type));
}
return genericListType.GetGenericArguments()[0];
return genericListType!.GetGenericArguments()[0];
}
if (typeof(IEnumerable).IsAssignableFrom(type))
{
@@ -402,18 +406,18 @@ namespace Newtonsoft.Json.Utilities
throw new Exception("Type {0} is not a collection.".FormatWith(CultureInfo.InvariantCulture, type));
}
public static void GetDictionaryKeyValueTypes(Type dictionaryType, out Type keyType, out Type valueType)
public static void GetDictionaryKeyValueTypes(Type dictionaryType, out Type? keyType, out Type? valueType)
{
ValidationUtils.ArgumentNotNull(dictionaryType, nameof(dictionaryType));
if (ImplementsGenericDefinition(dictionaryType, typeof(IDictionary<,>), out Type genericDictionaryType))
if (ImplementsGenericDefinition(dictionaryType, typeof(IDictionary<,>), out Type? genericDictionaryType))
{
if (genericDictionaryType.IsGenericTypeDefinition())
if (genericDictionaryType!.IsGenericTypeDefinition())
{
throw new Exception("Type {0} is not a dictionary.".FormatWith(CultureInfo.InvariantCulture, dictionaryType));
}
Type[] dictionaryGenericArguments = genericDictionaryType.GetGenericArguments();
Type[] dictionaryGenericArguments = genericDictionaryType!.GetGenericArguments();
keyType = dictionaryGenericArguments[0];
valueType = dictionaryGenericArguments[1];
@@ -522,7 +526,7 @@ namespace Newtonsoft.Json.Utilities
/// <param name="member">The member.</param>
/// <param name="target">The target.</param>
/// <param name="value">The value.</param>
public static void SetMemberValue(MemberInfo member, object target, object value)
public static void SetMemberValue(MemberInfo member, object target, object? value)
{
ValidationUtils.ArgumentNotNull(member, nameof(member));
ValidationUtils.ArgumentNotNull(target, nameof(target));
@@ -721,12 +725,12 @@ namespace Newtonsoft.Json.Utilities
return true;
}
public static T GetAttribute<T>(object attributeProvider) where T : Attribute
public static T? GetAttribute<T>(object attributeProvider) where T : Attribute
{
return GetAttribute<T>(attributeProvider, true);
}
public static T GetAttribute<T>(object attributeProvider, bool inherit) where T : Attribute
public static T? GetAttribute<T>(object attributeProvider, bool inherit) where T : Attribute
{
T[] attributes = GetAttributes<T>(attributeProvider, inherit);
@@ -746,7 +750,7 @@ namespace Newtonsoft.Json.Utilities
return a.Cast<T>().ToArray();
}
public static Attribute[] GetAttributes(object attributeProvider, Type attributeType, bool inherit)
public static Attribute[] GetAttributes(object attributeProvider, Type? attributeType, bool inherit)
{
ValidationUtils.ArgumentNotNull(attributeProvider, nameof(attributeProvider));
@@ -797,7 +801,7 @@ namespace Newtonsoft.Json.Utilities
return GetAttributes(attributeProvider, typeof(T), inherit).Cast<T>().ToArray();
}
public static Attribute[] GetAttributes(object provider, Type attributeType, bool inherit)
public static Attribute[] GetAttributes(object provider, Type? attributeType, bool inherit)
{
switch (provider)
{
@@ -819,12 +823,12 @@ namespace Newtonsoft.Json.Utilities
}
#endif
public static StructMultiKey<string, string> SplitFullyQualifiedTypeName(string fullyQualifiedTypeName)
public static StructMultiKey<string?, string> SplitFullyQualifiedTypeName(string fullyQualifiedTypeName)
{
int? assemblyDelimiterIndex = GetAssemblyDelimiterIndex(fullyQualifiedTypeName);
string typeName;
string assemblyName;
string? assemblyName;
if (assemblyDelimiterIndex != null)
{
@@ -837,7 +841,7 @@ namespace Newtonsoft.Json.Utilities
assemblyName = null;
}
return new StructMultiKey<string, string>(assemblyName, typeName);
return new StructMultiKey<string?, string>(assemblyName, typeName);
}
private static int? GetAssemblyDelimiterIndex(string fullyQualifiedTypeName)
@@ -1038,7 +1042,7 @@ namespace Newtonsoft.Json.Utilities
return isMethodOverriden;
}
public static object GetDefaultValue(Type type)
public static object? GetDefaultValue(Type type)
{
if (!type.IsValueType())
{
@@ -1089,4 +1093,4 @@ namespace Newtonsoft.Json.Utilities
return Activator.CreateInstance(type);
}
}
}
}