久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <small id='ffXzF'></small><noframes id='ffXzF'>

    • <bdo id='ffXzF'></bdo><ul id='ffXzF'></ul>
  • <legend id='ffXzF'><style id='ffXzF'><dir id='ffXzF'><q id='ffXzF'></q></dir></style></legend>

    1. <i id='ffXzF'><tr id='ffXzF'><dt id='ffXzF'><q id='ffXzF'><span id='ffXzF'><b id='ffXzF'><form id='ffXzF'><ins id='ffXzF'></ins><ul id='ffXzF'></ul><sub id='ffXzF'></sub></form><legend id='ffXzF'></legend><bdo id='ffXzF'><pre id='ffXzF'><center id='ffXzF'></center></pre></bdo></b><th id='ffXzF'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='ffXzF'><tfoot id='ffXzF'></tfoot><dl id='ffXzF'><fieldset id='ffXzF'></fieldset></dl></div>
      <tfoot id='ffXzF'></tfoot>

        Json.net 反序列化 null guid 案例

        Json.net deserialization null guid case(Json.net 反序列化 null guid 案例)

          <small id='lGWby'></small><noframes id='lGWby'>

          • <legend id='lGWby'><style id='lGWby'><dir id='lGWby'><q id='lGWby'></q></dir></style></legend>

            • <bdo id='lGWby'></bdo><ul id='lGWby'></ul>
              <i id='lGWby'><tr id='lGWby'><dt id='lGWby'><q id='lGWby'><span id='lGWby'><b id='lGWby'><form id='lGWby'><ins id='lGWby'></ins><ul id='lGWby'></ul><sub id='lGWby'></sub></form><legend id='lGWby'></legend><bdo id='lGWby'><pre id='lGWby'><center id='lGWby'></center></pre></bdo></b><th id='lGWby'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='lGWby'><tfoot id='lGWby'></tfoot><dl id='lGWby'><fieldset id='lGWby'></fieldset></dl></div>
              <tfoot id='lGWby'></tfoot>

                    <tbody id='lGWby'></tbody>
                  本文介紹了Json.net 反序列化 null guid 案例的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在使用 Json.NET 反序列化一個對象,該對象包含一個 Guid 類型的私有字段和該字段的公共屬性.當我的 Guid 在我的 json 中的值為 null 時,我想將 Guid.Empty 分配給我的字段.

                  I'm deserializing an object using Json.NET that contains a private field of type Guid and a public property for that field. When the value for my Guid is null in my json I want to assign Guid.Empty to my field.

                  public class MyClass
                  {
                      private Guid property;
                      public Guid Property
                      {
                          get { return property; }
                          set 
                          {
                              if (value == null)
                              {
                                  property = Guid.Empty;
                              }
                              else
                              {
                                  property = value;
                              }
                          }
                      }
                  }
                  

                  但是 deserializer 想要訪問私有字段,導致我在嘗試反序列化時收到此錯誤:

                  But the deserializer wants to access the private field, cause I get this error when I try to deserialize:

                  將值 {null} 轉換為類型System.Guid"時出錯.小路'[0].property',第 6 行,第 26 位.

                  Error converting value {null} to type 'System.Guid'. Path '[0].property', line 6, position 26.

                  如何讓它忽略私有字段而使用公共屬性?

                  How can I make it ignore the private field and use the public property instead?

                  推薦答案

                  Json.NET 拒絕為 Guid 設置 null 值,因為它是不可為空的值類型.嘗試在 即時窗口 中輸入 (Guid)nulla> 并且您將看到一條錯誤消息,指示無法在 .Net 中進行此轉換.

                  Json.NET refuses to set a null value for a Guid because it is a non-nullable value type. Try typing (Guid)null in the Immediate Window and you will see an error message indicating that this conversion cannot be made in .Net.

                  要解決此問題,您有幾個選擇:

                  To work around this, you have a couple of options:

                  1. 創建一個 Guid? 可為空的代理屬性.如果您愿意,它可以是私有的,只要它具有 [JsonProperty] 屬性:

                  1. Create a Guid? nullable proxy property. It can be private if you desire as long as it has a [JsonProperty] attribute:

                  public class MyClass
                  {
                      [JsonIgnore]
                      public Guid Property { get; set; }
                  
                      [JsonProperty("Property")]
                      Guid? NullableProperty { get { return Property == Guid.Empty ? null : (Guid?)Property; } set { Property = (value == null ? Guid.Empty : value.Value); } }
                  }
                  

                • 創建一個 JsonConverternull Json 令牌轉換為默認 Guid 值:

                • Create a JsonConverter that converts a null Json token to a default Guid value:

                  public class NullToDefaultConverter<T> : JsonConverter where T : struct
                  {
                      public override bool CanConvert(Type objectType)
                      {
                          return objectType == typeof(T);
                      }
                  
                      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                      {
                          var token = JToken.Load(reader);
                          if (token == null || token.Type == JTokenType.Null)
                              return default(T);
                          return token.ToObject(objectType); // Deserialize using default serializer
                      }
                  
                      // Return false instead if you don't want default values to be written as null
                      public override bool CanWrite { get { return true; } }
                  
                      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                      {
                          if (EqualityComparer<T>.Default.Equals((T)value, default(T)))
                              writer.WriteNull();
                          else
                              writer.WriteValue(value);
                      }
                  }
                  

                  然后將其應用于您的類型,如下所示:

                  Then apply it to your type as follows:

                  public class MyClass
                  {
                      [JsonConverter(typeof(NullToDefaultConverter<Guid>))]
                      public Guid Property { get; set; }
                  }
                  

                  或者,您可以通過將轉換器添加到 T 類型的所有值.htm" rel="noreferrer">JsonSerializerSettings.Converters.而且,要在全球范圍內注冊這樣的轉換器,請參閱例如如何在 MVC 4 Web API 中為 Json.NET 設置自定義 JsonSerializerSettings? 用于 Web API,設置 JsonConvert.DefaultSettings asp net core 2.0 無法正常工作 用于 ASP.NET Core 或 在 Json.Net 中全局注冊自定義 JsonConverter對于控制臺應用程序.

                  Alternatively, you can apply the converter to all values of type T by adding the converter to JsonSerializerSettings.Converters. And, to register such a converter globally, see e.g.How to set custom JsonSerializerSettings for Json.NET in MVC 4 Web API? for Web API, Setting JsonConvert.DefaultSettings asp net core 2.0 not working as expected for ASP.NET Core or Registering a custom JsonConverter globally in Json.Net for a console app.

                  如果您為控制臺應用程序全局注冊轉換器,您可能需要禁用它以進行遞歸調用,如 JSON.Net 在使用 [JsonConvert()] 時拋出 StackOverflowException.

                  If you do register the converter globally for a console app, you may need to disable it for recursive calls as shown in JSON.Net throws StackOverflowException when using [JsonConvert()].

                  如果您只需要 反序列化 Guid 的 null 值而不是重新序列化它,則可以應用 [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] 到 Guid 屬性, 和 null 值將被忽略,盡管 Guid 值無效:

                  If you only need to deserialize a null value for a Guid and not re-serialize it as such, you can apply [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] to the Guid property, and null values will ignored despite being invalid Guid values:

                  public class MyClass
                  {
                      [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
                      public Guid Property { get; set; }
                  }
                  

                  當然,如果您這樣做,您的 Guid 將被重新序列化為 00000000-0000-0000-0000-000000000000".為了改善這種情況,您可以應用 DefaultValueHandling = DefaultValueHandling.Ignore 這將導致在序列化期間省略空的 Guid 值:

                  Of course if you do this your Guid will be re-serialized as "00000000-0000-0000-0000-000000000000". To ameliorate that you could apply DefaultValueHandling = DefaultValueHandling.Ignore which will cause empty Guid values to be omitted during serialization:

                  [JsonProperty(NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
                  public Guid Property { get; set; }
                  

                  請注意,如果在反序列化期間調用的 參數化構造函數具有非-nullable Guid 參數具有相同的名稱,可能需要不同的方法.

                  Note that if a parameterized constructor called during deserialization has a non-nullable Guid argument with the same name, a different approach may be required.

                  這篇關于Json.net 反序列化 null guid 案例的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
                • 相關文檔推薦

                  Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                  XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                  Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標簽的 XML)
                  Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                  delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                  Parse malformed XML(解析格式錯誤的 XML)
                  <tfoot id='VJevu'></tfoot>

                          <tbody id='VJevu'></tbody>

                        <small id='VJevu'></small><noframes id='VJevu'>

                          <i id='VJevu'><tr id='VJevu'><dt id='VJevu'><q id='VJevu'><span id='VJevu'><b id='VJevu'><form id='VJevu'><ins id='VJevu'></ins><ul id='VJevu'></ul><sub id='VJevu'></sub></form><legend id='VJevu'></legend><bdo id='VJevu'><pre id='VJevu'><center id='VJevu'></center></pre></bdo></b><th id='VJevu'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='VJevu'><tfoot id='VJevu'></tfoot><dl id='VJevu'><fieldset id='VJevu'></fieldset></dl></div>
                          <legend id='VJevu'><style id='VJevu'><dir id='VJevu'><q id='VJevu'></q></dir></style></legend>

                            <bdo id='VJevu'></bdo><ul id='VJevu'></ul>

                            主站蜘蛛池模板: 亚洲欧美日韩电影 | 91在线视频| 日韩在线观看视频一区 | 羞羞在线观看视频 | 欧美在线一区二区三区 | 91精品国产一区二区在线观看 | 亚洲国产精品自拍 | 欧美一区二区在线观看视频 | 国产二区视频 | 久久国产精品视频 | 日韩精品一二三 | 国产成人99久久亚洲综合精品 | 人人看人人草 | 久久久久国产精品午夜一区 | 青青久久 | 日韩视频免费 | 一级毛片在线播放 | 在线播放中文字幕 | 国产99久久精品一区二区永久免费 | 欧美日韩亚洲系列 | 亚洲精品第一页 | 日韩高清国产一区在线 | 亚洲综合色网站 | 真人一级毛片 | 欧美色视频免费 | 在线观看中文字幕av | www.99re | 中文字幕不卡在线88 | 精品久久久久久 | 亚洲一区二区三 | 一区二区三区成人 | 黄色a三级| 一级爱爱片 | 日韩综合在线播放 | 精品久久久久久中文字幕 | 91久久网站 | 精品久久久久久久人人人人传媒 | 国产成人aⅴ | 91精品国产色综合久久 | 久久www免费人成看片高清 | 亚洲国产精品久久久久秋霞不卡 |