我正在尝试进行ajax调用以获取来自某个国家/地区的州,但我一直在获得无效的恶习。 我正在使用MVC4和json.NET。 我可以在我的测试中序列化对象而没有问题,但是当我进行ajax调用时,我不断收到错误。
这是值类型:
[JsonConverter(typeof(ValueObjectConverter))]
public class Code : IValueObject
{
private readonly string _code;
private Code(string code)
{
_code = code;
}
public override string ToString()
{
return _code;
}
public static implicit operator Code(string code)
{
return new Code(code);
}
public static implicit operator String(Code code)
{
return code.ToString();
}
}
这是我的目标:
public class CountryState : Entity
{
public Code CodeWPS { get; set; }
public Code CodeIAM { get; set; }
public Name Description { get; set; }
}
这是jsonconverter:
public class ValueObjectConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(IValueObject));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return (IValueObject)existingValue;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var item = (IValueObject)value;
writer.WriteValue(item.ToString());
writer.Flush();
}
}
这是API方法:
public IEnumerable<CountryState> GetStatesByCountry(string codeType, string countryCodeIam)
{
var states = _getAllCountriesQueryHandler.Handle(countryCodeIam);
return states;
}
这是我用firebug得到的错误:
{"Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Error getting value from 'CodeWPS' on 'CNH.CSCN.BBS.Entities.CountryState'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":" at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.<WriteToStreamAsync>b__c()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Unable to cast object of type 'System.String' to type 'CNH.CSCN.BBS.Entities.ValueTypes.Code'.","ExceptionType":"System.InvalidCastException","StackTrace":" at GetCodeWPS(Object )\r\n at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)"}}}
更新:
我添加了一个contractresolver:
public class SpecialContractResolver : DefaultContractResolver
{
protected override IValueProvider CreateMemberValueProvider(MemberInfo member)
{
if (member.MemberType == MemberTypes.Property)
{
var pi = (PropertyInfo)member;
if (typeof(IValueObject).IsAssignableFrom(pi.PropertyType))
{
return new IValueObjectValueProvider(member, pi.PropertyType);
}
}
else if (member.MemberType == MemberTypes.Field)
{
var fi = (FieldInfo)member;
if (typeof(IValueObject).IsAssignableFrom(fi.FieldType))
{
return new IValueObjectValueProvider(member, fi.FieldType);
}
}
return base.CreateMemberValueProvider(member);
}
}
和IValueProvider:
public class IValueObjectValueProvider : IValueProvider
{
private readonly object _defaultValue;
private readonly IValueProvider _underlyingValueProvider;
public IValueObjectValueProvider(MemberInfo memberInfo, Type underlyingType)
{
_underlyingValueProvider = new DynamicValueProvider(memberInfo);
_defaultValue = underlyingType;
}
public void SetValue(object target, object value)
{
target = value;
}
public object GetValue(object target)
{
return target.ToString();
}
}
我知道我正处于正确的轨道上,我不再收到错误消息,但结果如下:
Object {CodeWPS =“CNH.CSCN.BBS.Entities.CountryState”,CodeIAM =“CNH.CSCN.BBS.Entities.CountryState”,Description =“CNH.CSCN.BBS.Entities.CountryState”}
我认为我的contractresolver和ivalueprovider还不是100%正确......
答案 0 :(得分:1)
这是因为这段代码:
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(IValueObject));
}
objectType
不是IValueObject
,而是Code
- 所以CanConvert
返回false
并且您的转换器未被使用。您的代码应如下所示:
return typeof(IValueObject).IsAssignableFrom(objectType);
在这种情况下,实现类也是匹配的。
答案 1 :(得分:0)
我创建了一个SpecialContractResolver
public class SpecialContractResolver : DefaultContractResolver
{
protected override IValueProvider CreateMemberValueProvider(MemberInfo member)
{
if (member.MemberType == MemberTypes.Property)
{
var pi = (PropertyInfo)member;
if (typeof(IValueObject).IsAssignableFrom(pi.PropertyType))
{
return new ExpressionValueProvider(member);
}
}
return base.CreateMemberValueProvider(member);
}
}
我创建了一个ValueObjectConverter:
public class ValueObjectConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(IValueObject).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return Activator.CreateInstance(objectType, serializer.Deserialize<string>(reader));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var item = (IValueObject)value;
writer.WriteValue(item.ToString());
writer.Flush();
}
}
我把它添加到我的WebApiConfig:
var jsonFormatter = config.Formatters.JsonFormatter;
jsonFormatter.SerializerSettings.ContractResolver = new SpecialContractResolver();
jsonFormatter.SerializerSettings.Converters.Insert(0, new ValueObjectConverter());
希望这有助于某人!