From 66c41b59378e6ce9410c68873956227fe0ee72c4 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 11 Jan 2018 22:03:21 +1300 Subject: [PATCH] -Refactor --- Src/Newtonsoft.Json/JsonWriter.Async.cs | 9 +-------- Src/Newtonsoft.Json/JsonWriter.cs | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonWriter.Async.cs b/Src/Newtonsoft.Json/JsonWriter.Async.cs index c53c0022..fe113736 100644 --- a/Src/Newtonsoft.Json/JsonWriter.Async.cs +++ b/Src/Newtonsoft.Json/JsonWriter.Async.cs @@ -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 diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index b2d7845f..57b1203d 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -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);