-Refactor
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user