c# JSONPatch 不适用于 int 和枚举

本文关键字:枚举 int 适用于 JSONPatch 不适用 | 更新日期: 2023-09-27 18:36:58

我正在尝试实现 ASP.NET MVC 5 WebAPI Patch。但是 int 值和枚举存在问题。JSON 反序列化"思考",最好将 int 转换为 long,这是因为在我的 int 属性中,我接收所有方式 0...

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

那么问题是,我在我的模型中接收的所有方式为空

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

我正在使用POSTMAN将补丁作为Body>Raw中的json发送。标头是应用程序/json。

我不明白为什么模型为空...我需要在WebApiConfig上做任何事情吗?

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

 public class Int32EnumConverter<T> : JsonConverter

但问题出在 WebApiConfig 中.cs您需要实现这一点:

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

有人帮我吗?谢谢!

c# JSONPatch 不适用于 int 和枚举

找到了!我在三角洲补丁上做了我的

 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)
        {
....

Atention:不是 100% 测试!对于空值失败。