【问题标题】:c# JSONPatch not working with int and enumsc# JSONPatch 不适用于 int 和 enums
【发布时间】:2016-03-02 10:43:09
【问题描述】:

我正在尝试实现 ASP.NET MVC 5 WebAPI 补丁。但是 int 值和枚举存在问题。 JSON反序列化“认为”更好地将int转换为long,这是因为在我的int属性中我总是接受0 ...

所以我找到了“Microsoft.AspNet.JsonPatch.JsonPatchDocument”(还有很多其他的)

那么问题是,我的模型中总是返​​回 null

public async Task<IHttpActionResult> Patch( int id, [FromBody] Microsoft.AspNet.JsonPatch.JsonPatchDocument<Customer> model)
{
    //model is allways == null
}

我正在使用 POSTMAN 在 Body>Raw 中以 json 格式发送补丁。标头是 application/json。

我不明白为什么模型为空...我需要在 WebApiConfig 上做些什么?

我仍然尝试实现自定义 JsonConverter,但问题是我有很多枚举类型,我需要为每个枚举创建一个吗?我尝试这样的事情:

 public class Int32EnumConverter<T> : JsonConverter

但问题出在 WebApiConfig.cs 你需要实现这个:

config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new Int32EnumConverter<[I NEED HEAR Dynamic Enum Types>>);    

有没有人可以帮助我?谢谢!

【问题讨论】:

  • 为什么不直接使用开箱即用的 Json.net 而无需注册其他序列化程序?
  • 因为 http 补丁中的 JSON.net 在 long 中反序列化 int...
  • 我还是看不出问题出在哪里。
  • 如果你不知道尝试自己创建这个
  • 你能分享你的Int32EnumConverter&lt;T&gt;吗?

标签: c# asp.net asp.net-mvc asp.net-web-api json.net


【解决方案1】:

找到了!我在 Delta Patch 上做了我的

 public class JSONPatch<T>
    {
        private Dictionary<string, object> propsJson = new Dictionary<string, object>();

        public JSONPatch()
        {
            Stream req = HttpContext.Current.Request.InputStream;
            req.Seek(0, System.IO.SeekOrigin.Begin);
            string json = new StreamReader(req).ReadToEnd();
            propsJson = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
        }

        public JSONPatch(object model)
        {
            propsJson = JsonConvert.DeserializeObject<Dictionary<string, object>>(model.ToString());
        }

        public void Patch(T model)
        {

            PropertyInfo[] properties = model.GetType().GetProperties();

            foreach (PropertyInfo property in properties)
            {
                try
                {
                    if (!propsJson.Any(x => x.Key.Equals(property.Name, StringComparison.InvariantCultureIgnoreCase)))
                        continue;

                    KeyValuePair<string, object> item = propsJson.First(x => x.Key.Equals(property.Name, StringComparison.InvariantCultureIgnoreCase));

                    Type targetProp = model.GetType().GetProperty(property.Name).PropertyType;

                    Type targetNotNuable = Nullable.GetUnderlyingType(targetProp);

                    if (targetNotNuable != null)
                    {
                        targetProp = targetNotNuable;
                    }


                    if (item.Value.GetType() != typeof(Int64))
                    {

                        object newA = Convert.ChangeType(item.Value, targetProp);

                        model.GetType().GetProperty(property.Name).SetValue(model, newA, null);

                    }
                    else
                    {
                        int value = Convert.ToInt32(item.Value);

                        if (targetProp.IsEnum)
                        {
                            object newA = Enum.Parse(targetProp, value.ToString());
                            model.GetType().GetProperty(property.Name).SetValue(model, newA, null);
                        }
                        else
                        {
                            object newA = Convert.ChangeType(value, targetProp);
                            model.GetType().GetProperty(property.Name).SetValue(model, newA, null);
                        }

                    }

                }
                catch
                {

                }


            }
        }

    }

现在:

  public async Task<IHttpActionResult> Patch( int id, [FromBody]JSONPatch<Customer> model)
        {
....

注意:未经过 100% 测试! null 值失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 2022-11-05
    • 2018-11-21
    • 1970-01-01
    • 2012-06-29
    相关资源
    最近更新 更多