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

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

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

      1. <tfoot id='LGMHJ'></tfoot>

        檢測反序列化對象是否缺少 Json.NET 中 JsonConvert

        Detect if deserialized object is missing a field with the JsonConvert class in Json.NET(檢測反序列化對象是否缺少 Json.NET 中 JsonConvert 類的字段)

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

        1. <small id='fFI5N'></small><noframes id='fFI5N'>

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

                <tbody id='fFI5N'></tbody>

                <bdo id='fFI5N'></bdo><ul id='fFI5N'></ul>
                  <tfoot id='fFI5N'></tfoot>
                  本文介紹了檢測反序列化對象是否缺少 Json.NET 中 JsonConvert 類的字段的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試使用 Json.NET 反序列化一些 JSON 對象.然而,我發現,當我反序列化一個沒有我要查找的屬性的對象時,不會引發任何錯誤,但是當我訪問它們時會為這些屬性返回一個默認值.當我反序列化錯誤類型的對象時,我能夠檢測到這一點很重要.示例代碼:

                  I'm trying to deserialize some JSON objects using Json.NET. I've found however that when I deserialize an object that doesn't have the properties I'm looking for that no error is thrown up but a default value is returned for the properties when I access them. It's important that I'm able to detect when I've deserialized the wrong type of object. Example code:

                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Text;
                  using System.Threading.Tasks;
                  using Newtonsoft.Json;
                  
                  namespace Json_Fail_Test
                  {
                      class Program
                      {
                          [JsonObject(MemberSerialization.OptOut)]
                          private class MyJsonObjView
                          {
                              [JsonProperty("MyJsonInt")]
                              public int MyJsonInt { get; set; }
                          }
                  
                          const string correctData = @"
                          {
                              'MyJsonInt': 42
                          }";
                  
                          const string wrongData = @"
                          {
                              'SomeOtherProperty': 'fbe8c20b'
                          }";
                  
                          static void Main(string[] args)
                          {
                              var goodObj = JsonConvert.DeserializeObject<MyJsonObjView>(correctData);
                              System.Console.Out.WriteLine(goodObj.MyJsonInt.ToString());
                  
                              var badObj = JsonConvert.DeserializeObject<MyJsonObjView>(wrongData);
                              System.Console.Out.WriteLine(badObj.MyJsonInt.ToString());
                          }
                      }
                  }
                  

                  這個程序的輸出是:420

                  The output of this program is: 42 0

                  我寧愿拋出異常而不是靜默失敗.除此之外,有沒有辦法檢測序列化是否找不到參數?

                  I would prefer an exception be thrown to failing silently. Short of that is there a way to detect if the serialization failed to find a parameter?

                  我知道我可以使用 Json 對象解析數據,然后使用鍵值查找檢查參數,但我所在的代碼庫使用上面的模式,如果可能的話,我希望保持一致.

                  I know I can parse the data with a Json object and then check for the parameter with a key value lookup but the codebase I'm in uses the pattern above and I'd like keep that consistent if it's possible.

                  推薦答案

                  Json.Net 序列化程序有一個 MissingMemberHandling 設置,您可以將其設置為 Error.(默認為 Ignore.)這將導致序列化程序在反序列化過程中遇到目標類中沒有對應屬性的 JSON 屬性時拋出 JsonSerializationException.

                  The Json.Net serializer has a MissingMemberHandling setting which you can set to Error. (The default is Ignore.) This will cause the serializer to throw a JsonSerializationException during deserialization whenever it encounters a JSON property for which there is no corresponding property in the target class.

                  static void Main(string[] args)
                  {
                      try
                      {
                          JsonSerializerSettings settings = new JsonSerializerSettings();
                          settings.MissingMemberHandling = MissingMemberHandling.Error;
                  
                          var goodObj = JsonConvert.DeserializeObject<MyJsonObjView>(correctData, settings);
                          System.Console.Out.WriteLine(goodObj.MyJsonInt.ToString());
                  
                          var badObj = JsonConvert.DeserializeObject<MyJsonObjView>(wrongData, settings);
                          System.Console.Out.WriteLine(badObj.MyJsonInt.ToString());
                      }
                      catch (Exception ex)
                      {
                          Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
                      }
                  }
                  

                  結果:

                  42
                  JsonSerializationException: Could not find member 'SomeOtherProperty' on object
                  of type 'MyJsonObjView'. Path 'SomeOtherProperty', line 3, position 33.
                  

                  請參閱:MissingMemberHandling 設置.

                  這篇關于檢測反序列化對象是否缺少 Json.NET 中 JsonConvert 類的字段的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
                1. <small id='iMexq'></small><noframes id='iMexq'>

                  <legend id='iMexq'><style id='iMexq'><dir id='iMexq'><q id='iMexq'></q></dir></style></legend>
                    <tbody id='iMexq'></tbody>

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

                            <bdo id='iMexq'></bdo><ul id='iMexq'></ul>
                            主站蜘蛛池模板: 日韩在线看片 | 欧美日韩精品久久久免费观看 | 免费影视在线观看 | 国产成人短视频在线观看 | 亚洲一区二区三区久久 | 99精品电影 | 九九久久精品 | 精品久久香蕉国产线看观看亚洲 | 91亚洲国产亚洲国产 | 日韩欧美精品在线 | 国产精品污污视频 | 国产成人精品久久二区二区91 | 午夜日韩 | 黄色一级网 | 麻豆国产精品777777在线 | 亚洲综合色视频在线观看 | 亚洲一区二区在线 | 婷婷亚洲综合 | 日韩福利| 亚洲高清视频在线观看 | 91麻豆精品国产91久久久更新资源速度超快 | 日韩成人在线播放 | 欧美黄色一区 | 国产日韩精品视频 | 国产精品久久国产愉拍 | www.色五月.com | 国产综合视频 | 中文字幕亚洲区一区二 | 日本一区二区三区免费观看 | 国产精品不卡视频 | 成人免费观看男女羞羞视频 | 综合国产在线 | 精品国产一区二区三区久久影院 | 国产一区免费 | 羞羞视频在线观看网站 | 国产在线视频三区 | www国产成人免费观看视频,深夜成人网 | 亚洲精品永久免费 | 国产精品亚洲一区 | 国产精品成人在线 | 亚洲精品日韩综合观看成人91 |