简体   繁体   English

继承时Json的奇怪行为

[英]Odd behavior with Json when inheriting

I've run into a type of issue. 我遇到了一种问题。

I have an ErrorModel class, bare bones, that was inheriting ICollection. 我有一个ErrorModel类,它是在继承ICollection。

public class ErrorsModel : ICollection<string>
    {
        private ICollection<string> Errors { get; set; }
        public bool HasErrors { get { return Errors != null && Errors.Any(); } }
        public string PartialView { get; set; }
}

Omitted the ICollection imp. 省略了ICollection imp。 as it is standard 作为标准

When Returned by a controllers Action Json method 由控制器的Action Json方法返回时

return Json(Errors)

The result is the private Errors object 结果是私有错误对象

[
  "Select a Country",
  "Select a City"
]

However when you remove the inheritance you get a complete serialization of the object. 但是,当您删除继承时,将获得对象的完整序列化。

{
"Count": 2,
  "HasErrors": true,
  "IsReadOnly": false
}

So my question is what is causing the odd serialization behavior when inheriting? 所以我的问题是什么在继承时导致了奇怪的序列化行为?

Your JSON serializer is treating all collection classes as arrays. 您的JSON序列化程序将所有集合类都视为数组。

JSON does not support mixtures of arrays and objects. JSON不支持数组和对象的混合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM