绑定应用程序/ json到POCO对象在asp.net mvc,序列化异常 [英] Binding application/json to POCO object in asp.net mvc, Serialization exception

查看:118
本文介绍了绑定应用程序/ json到POCO对象在asp.net mvc,序列化异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将json从我的看法传递给我的控制器执行操作的操作。要将发送的json转换为POCO,我使用此操作过滤器:

  public class ObjectFilter:ActionFilterAttribute {
public Type RootType {get;组; }

public override void OnActionExecuting(ActionExecutingContext filterContext){
IList< ErrorInfo> errors = new List< ErrorInfo>();
try {
object o = new DataContractJsonSerializer(RootType).ReadObject(filterContext.HttpContext.Request.InputStream);
filterContext.ActionParameters [postdata] = o;
}
catch(SerializationException ex){
errors.Add(new ErrorInfo(null,ex.Message));
}
finally {
filterContext.ActionParameters [errors] = errors.AsEnumerable();
}

}



它使用DataContractJsonSerializer将JSON映射到我的对象。我的动作然后如此装饰:

  [ObjectFilter(RootType = typeof(MyObject))] 
public JsonResult updateproduct (MyObject postdata,IEnumerable< ErrorInfo>错误){
//检查集合中是否有错误!
}

所以推测,这里发生了什么,如果序列化有问题将JSON转换为对象的类型(如果字符串不能被解析为十进制类型或类似于例如),则会将错误添加到集合中,然后将该错误传递给视图。然后,可以检查这个集合是否有错误并向客户端报告。



问题是我似乎找不到 领域引起了这个问题。理想情况下,我想回覆这个观点,并说这个字段有问题。 SerializationException类似乎没有提供这种灵活性。



集体SO hivemind如何考虑解决这个问题?

解决方案

这样做如何: Json。 Net
它读取一个JSON字符串,然后将其反序列化到给定的POCO对象。

  string jsonResult = GetJsonStringFromSomeService(); 
MyPocoObject myobject = JsonConvert.DeserializeObject&MyPocoObject>(jsonResult);
Console.Write(Damn that easy);

但是,为了确定错误发生的位置,我不太确定。


I'm passing json back up from my view to my controller actions to perform operations. To convert the json being sent in, to a POCO I'm using this Action Filter:

public class ObjectFilter : ActionFilterAttribute {
public Type RootType { get; set; }

public override void OnActionExecuting(ActionExecutingContext filterContext) {
IList<ErrorInfo> errors = new List<ErrorInfo>();
try {
    object o = new DataContractJsonSerializer(RootType).ReadObject(filterContext.HttpContext.Request.InputStream);
    filterContext.ActionParameters["postdata"] = o;
    }
catch (SerializationException ex) {
    errors.Add(new ErrorInfo(null, ex.Message));
}
finally {
    filterContext.ActionParameters["errors"] = errors.AsEnumerable();
}

}

It's using the DataContractJsonSerializer to map the JSON, over to my object. My Action is then decorated like so:

[ObjectFilter(RootType = typeof(MyObject))]
public JsonResult updateproduct(MyObject postdata, IEnumerable<ErrorInfo> errors) {
    // check if errors has any in the collection!
}

So to surmise, what is going on here, if there is a problem serializing the JSON to the type of object (if a string cannot be parsed as a decimal type or similar for eg), it adds the error to a collection and then passes that error up to the view. It can then check if this collection has an errors and report back to the client.

The issue is that I cannot seem to find out which field has caused the problem. Ideally I'd like to pass back to the view and say "THIS FIELD" had a problem. The SerializationException class does not seem to offer this sort of flexibility.

How would the collective SO hivemind consider tackling this problem?

解决方案

How about this: Json.Net It reads a JSON string and then Deserialises it to the given POCO object.

string jsonResult = GetJsonStringFromSomeService();
MyPocoObject myobject = JsonConvert.DeserializeObject<MyPocoObject>(jsonResult);
Console.Write("Damn that is easy");

But for the determing where errors occur, I am not too sure.

这篇关于绑定应用程序/ json到POCO对象在asp.net mvc,序列化异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆