-Refactor

This commit is contained in:
James Newton-King
2018-01-11 22:03:21 +13:00
parent c445141fc6
commit 66c41b5937
2 changed files with 16 additions and 17 deletions
+1 -8
View File
@@ -1776,14 +1776,7 @@ namespace Newtonsoft.Json
#if HAVE_ICONVERTIBLE
if (value is IConvertible convertible)
{
// the value is a non-standard IConvertible
// convert to the underlying value and retry
TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertible);
// if convertible has an underlying typecode of Object then attempt to convert it to a string
typeCode = typeInformation.TypeCode == PrimitiveTypeCode.Object ? PrimitiveTypeCode.String : typeInformation.TypeCode;
Type resolvedType = typeInformation.TypeCode == PrimitiveTypeCode.Object ? typeof(string) : typeInformation.Type;
value = convertible.ToType(resolvedType, CultureInfo.InvariantCulture);
ResolveConvertibleValue(convertible, out typeCode, out value);
continue;
}
#endif
+15 -9
View File
@@ -1658,15 +1658,7 @@ namespace Newtonsoft.Json
#if HAVE_ICONVERTIBLE
if (value is IConvertible convertible)
{
// the value is a non-standard IConvertible
// convert to the underlying value and retry
TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertible);
// if convertible has an underlying typecode of Object then attempt to convert it to a string
typeCode = typeInformation.TypeCode == PrimitiveTypeCode.Object ? PrimitiveTypeCode.String : typeInformation.TypeCode;
Type resolvedType = typeInformation.TypeCode == PrimitiveTypeCode.Object ? typeof(string) : typeInformation.Type;
value = convertible.ToType(resolvedType, CultureInfo.InvariantCulture);
ResolveConvertibleValue(convertible, out typeCode, out value);
continue;
}
#endif
@@ -1683,6 +1675,20 @@ namespace Newtonsoft.Json
}
}
#if HAVE_ICONVERTIBLE
private static void ResolveConvertibleValue(IConvertible convertible, out PrimitiveTypeCode typeCode, out object value)
{
// the value is a non-standard IConvertible
// convert to the underlying value and retry
TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertible);
// if convertible has an underlying typecode of Object then attempt to convert it to a string
typeCode = typeInformation.TypeCode == PrimitiveTypeCode.Object ? PrimitiveTypeCode.String : typeInformation.TypeCode;
Type resolvedType = typeInformation.TypeCode == PrimitiveTypeCode.Object ? typeof(string) : typeInformation.Type;
value = convertible.ToType(resolvedType, CultureInfo.InvariantCulture);
}
#endif
private static JsonWriterException CreateUnsupportedTypeException(JsonWriter writer, object value)
{
return JsonWriterException.Create(writer, "Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null);