0

我正在使用ASP.NET MVC 5 Web Api。Web API:未能序列化内容类型的响应正文

我现在有很多API的应用程序。 最近我已经实现了自定义的JsonConverter,它将按照时区转换Date。 “发生了一个错误。”, “ExceptionMessage”:

public class CustomInfoConverter : JsonConverter 
{ 

    public override bool CanConvert(Type objectType) 
    { 
     return objectType == typeof(CustomType); 
    } 
    public override bool CanRead 
    { 
     get 
     { 
      return false; 
     } 
    } 
    public override bool CanWrite 
    { 
     get 
     { 
      return true; 
     } 
    } 
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     var customType = (CustomType)value; 
     if (customType == null || null== customType.TimeZone) return; 
     //DateTime currentDateTime = customType.Date??DateTime.Now; 
     DateTime currentDateTime = DateTime.SpecifyKind(customType.Date ?? DateTime.Now, DateTimeKind.Unspecified); 

     DateTime userDateTime = TimeZoneInfo.ConvertTimeFromUtc(currentDateTime, customType.TimeZone); 
     customType.Date = userDateTime; 
     JsonSerializer innerSerializer = new JsonSerializer(); 
     foreach (var converter in serializer.Converters.Where(c => !(c is CustomInfoConverter))) 
     { 
      innerSerializer.Converters.Add(converter); 
     } 
     innerSerializer.Serialize(writer, customType); 


    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     throw new NotImplementedException(); 
    } 


} 

实现这一习俗JsonConverter所有的API,不同的是一个API,它是抛出异常下面

{ “消息” 工作之后的“ 'ObjectContent`1'类型未能序列化响应正文 内容类型'application/json; charset = utf-8'。“,”ExceptionType“:”System.InvalidOperationException“,”StackTrace“:null,”InnerException “:{”Message“:”发生了 错误。“,”ExceptionMessage“:”状态为的令牌属性名称属性会导致无效的JSON对象。路径 'Data.Forms [0]' “” ExceptionType。 “:” Newtonsoft.Json.JsonWriterException “ ”堆栈跟踪“:” 在Newtonsoft.Json.JsonWriter.AutoComplete(JsonToken tokenBeingWritten个)\ r \ n在 Newtonsoft.Json.JsonWriter.InternalWritePropertyName(字符串名称)\ r \ n 在Newtonsoft.Json.JsonTextWriter.WritePropertyName(字符串名称, 布尔逃逸)\ r \ n在 Newtonsoft.Json.Serialization.JsonProperty.WritePropertyName(JsonWriter 作者)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty个)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter 作家,对象的值,JsonContract valueContract,JsonProperty构件, JsonContainerContract containerContract,JsonProperty containerProperty个)\ r \ n在 Newtonsoft.Json。 Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter 作家,IEnumerable的值,JsonArrayContract合同,JsonProperty 构件,JsonContainerContract collectionContract,JsonProperty containerProperty个)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter 作家,对象的值, JsonContract valueContract,JsonProperty成员, JsonContainerContract containerContract,JsonProperty containerProperty个)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter 作家,对象的值,JsonObjectContract合同,JsonProperty 构件,JsonContainerContract collectionContract,JsonProperty containerProperty个)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter 作家,对象的值,JsonContract valueContract,JsonProperty构件, JsonContainerContract containerContract,JsonProperty containerProperty个)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter。SerializeValue(JsonWriter 作家,对象的值,JsonContract valueContract,JsonProperty构件, JsonContainerContract containerContract,JsonProperty containerProperty个)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,对象的值,类型的objectType)\ r \ñ在 Newtonsoft.Json.JsonSerializer.SerializeInternal(jsonWriter jsonWriter,对象的值,类型的objectType个)\ r \ n在 Newtonsoft.Json.JsonSerializer.Serialize(jsonWriter jsonWriter,对象 值)\ r \ n在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(类型 类型,对象值,流writeStream,编码 effectiveEncoding个)\ r \ n在 System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(类型 类型,对象的值,流writeStream,编码 effectiveEncoding个)\ r \ n在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter .WriteToStream(类型 类型,对象的值,流writeStream,HttpContent内容个)\ r \ n在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(类型 类型,对象的值,流writeStream,HttpContent内容, TransportContext transportContext ,CancellationToken cancellationToken)\ r \ n ---以前位置的堆栈跟踪结束 抛出异常--- \ r \ n在 System.Runtime.CompilerServices .TaskAwaiter.ThrowForNonSuccess(任务 任务个)\ r \ n在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务个)\ r \ n在 System.Runtime.CompilerServices.TaskAwaiter.GetResult(个)\ r \ n在 System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext()“}}

您可以参考这个link了解更多详情。

回答

0

的问题看起来是,在某些情况下,你是从WriteJson()返回不写任何东西,特别是当customType.TimeZone == null

var customType = (CustomType)value; 
    if (customType == null || null== customType.TimeZone) return; 

这样做会导致一个无效的JSON对象,因为物业将已经写入调用者,从而导致:

{ 
    "customType": 
} 

试图做到这一点会导致您看到的例外情况。

相反,您需要防止属性本身被序列化。这在转换器中是不可能的,但它需要在包含类型中完成。

为了避免序列化的属性与空值,你应该设置NullValueHandling = NullValueHandling.Ignoreserializer settings,或对物业本身:

public class ContainerClass 
{ 
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] 
    public CustomType CustomType { get; set; } 
} 

为了防止序列化的属性时,其TimeZone属性为null,你应该使用conditional property serialization通过在包含类型中添加ShouldSerializeXXX()方法,其中XXX与您的房产名称完全匹配:

public class ContainerClass 
{ 
    public CustomType CustomType { get; set; } 

    public bool ShouldSerializeCustomType() 
    { 
     return CustomType != null && CustomType.TimeZone != null; 
    } 
} 
相关问题