2016-03-02 90 views
-1

我尝试实现ASP.NET MVC 5 WebAPI修补程序。但是int值和枚举存在一个问题。 JSON反序列化“思考”,这是更好的转换诠释为长,所以是因为在我int属性我recive总是0 ...c#JSONPatch不与int和枚举工作

因此,我发现“Microsoft.AspNet.JsonPatch.JsonPatchDocument”(还有很多人疗法)

接着的问题是,我recive八方通空在我的模型

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

我使用邮递员补丁派在车身>原始JSON。标题是应用程序/ json。

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

尽管我试图实现一个自定义JsonConverter,但问题是我有很多枚举类型,我需要为每个枚举创建一个?我尝试sothing这样的:

public class Int32EnumConverter<T> : JsonConverter 

但问题是,你需要实现这个WebApiConfig.cs:

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

没有任何人帮我?谢谢!

+0

为什么不直接使用Json.net而不需要注册其他序列化程序呢? – Marco

+0

因为JSON.net在http patch deserialize int long ... – rockxl1

+0

我仍然无法看到问题是什么。 – Marco

回答

1

找到了!我做了我对增量补丁

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%测试!空值失败。