1

Closed

Cannot convert nullable int to empty.string

description

I used version 4.5.11 before upgrading to 5.0.4.
Suddenly I got an error:
Make sure the source type is convertible to the destination type.
When casting from a number, the value must be a number less than infinity.
What my code doing is:
public class MyContractResolver : ContractResolver
    {
        public MyContractResolver()
        {
            NullableValueProviderType = typeof(MyNullableValueProvider);
        }
    }

And in the MyNullableValueProdiver

        public object GetValue(object target)
        {
                      if (type is nullable integer and target == null)
                           return string.Empty;
        }
The same code will work for nullable DateTime that returns empty string.
Is this a bug?
If not, at least they have to be consistent.

Thanks,
Revin

StackTrace:
System.InvalidCastException was unhandled by user code
  HResult=-2147467262
  Message=Specified cast is not valid.
  Source=Newtonsoft.Json
  StackTrace:
       at Newtonsoft.Json.JsonWriter.WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, Object value)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
       at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
       at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, Formatting formatting, JsonSerializerSettings settings)
       at Payroll.Mvc.PayrollBaseController.ModelListData[T](ContractResolver contractResolver, PagedArgument argument, IList`1 list) in c:\Code\PayMetrics\Payroll\Mvc\Utility\PayrollBaseController.cs:line 268
       at Payroll.Mvc.Areas.Global.Controllers.AusTaxCodeEffectiveController.ListData(PagedArgument argument) in c:\Code\PayMetrics\Payroll\Mvc\Areas\Global\Controllers\AusTaxCodeEffectiveController.cs:line 59
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
  InnerException: 
Closed May 3, 2013 at 9:29 AM by JamesNK
DateTime? and int? both break for me with that same error.

For performance reasons json.net now relies on the value returned from the IValueProvider being the same type as the property. I would suggest to use a JsonConverter but unfortunately they are skipped if the value is null, and if I change that then a whole lot of applications will break.

I can't think of a good solution.

comments