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

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

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

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

      1. 使屬性反序列化但不使用 json.net 序列化

        Making a property deserialize but not serialize with json.net(使屬性反序列化但不使用 json.net 序列化)

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

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

              1. <legend id='xt4f7'><style id='xt4f7'><dir id='xt4f7'><q id='xt4f7'></q></dir></style></legend>

                  本文介紹了使屬性反序列化但不使用 json.net 序列化的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  我們有一些配置文件是通過(guò)使用 Json.net 序列化 C# 對(duì)象生成的.

                  We have some configuration files which were generated by serializing C# objects with Json.net.

                  我們希望將序列化類的一個(gè)屬性從簡(jiǎn)單的枚舉屬性遷移到類屬性中.

                  We'd like to migrate one property of the serialised class away from being a simple enum property into a class property.

                  一種簡(jiǎn)單的方法是,將舊的枚舉屬性保留在類上,并安排 Json.net 在加載配置時(shí)讀取此屬性,但在下一次序列化對(duì)象時(shí)不再保存它.我們將分別處理從舊枚舉生成新類.

                  One easy way to do this, would be to leave the old enum property on the class, and arrange for Json.net to read this property when we load the config, but not to save it again when we next serialize the object. We'll deal with generating the new class from the old enum separately.

                  是否有任何簡(jiǎn)單的方法來(lái)標(biāo)記(例如使用屬性)C# 對(duì)象的屬性,以便 Json.net 僅在序列化時(shí)忽略它,但在反序列化時(shí)關(guān)注它?

                  Is there any simple way to mark (e.g. with attributes) a property of a C# object, so that Json.net will ignore it ONLY when serializing, but attend to it when deserializing?

                  推薦答案

                  實(shí)際上,您可以使用幾種相當(dāng)簡(jiǎn)單的方法來(lái)實(shí)現(xiàn)您想要的結(jié)果.

                  There are actually several fairly simple approaches you can use to achieve the result you want.

                  假設(shè),例如,您的類當(dāng)前定義如下:

                  Let's assume, for example, that you have your classes currently defined like this:

                  class Config
                  {
                      public Fizz ObsoleteSetting { get; set; }
                      public Bang ReplacementSetting { get; set; }
                  }
                  
                  enum Fizz { Alpha, Beta, Gamma }
                  
                  class Bang
                  {
                      public string Value { get; set; }
                  }
                  

                  而你想這樣做:

                  string json = @"{ ""ObsoleteSetting"" : ""Gamma"" }";
                  
                  // deserialize
                  Config config = JsonConvert.DeserializeObject<Config>(json);
                  
                  // migrate
                  config.ReplacementSetting = 
                      new Bang { Value = config.ObsoleteSetting.ToString() };
                  
                  // serialize
                  json = JsonConvert.SerializeObject(config);
                  Console.WriteLine(json);
                  

                  要得到這個(gè):

                  {"ReplacementSetting":{"Value":"Gamma"}}
                  

                  方法一:添加 ShouldSerialize 方法

                  Json.NET 能夠通過(guò)在類中查找相應(yīng)的 ShouldSerialize 方法來(lái)有條件地序列化屬性.

                  Approach 1: Add a ShouldSerialize method

                  Json.NET has the ability to conditionally serialize properties by looking for corresponding ShouldSerialize methods in the class.

                  要使用此功能,請(qǐng)將布爾 ShouldSerializeBlah() 方法添加到您的類中,其中 Blah 替換為您不想序列化的屬性的名稱.讓這個(gè)方法的實(shí)現(xiàn)總是返回false.

                  To use this feature, add a boolean ShouldSerializeBlah() method to your class where Blah is replaced with the name of the property that you do not want to serialize. Make the implementation of this method always return false.

                  class Config
                  {
                      public Fizz ObsoleteSetting { get; set; }
                  
                      public Bang ReplacementSetting { get; set; }
                  
                      public bool ShouldSerializeObsoleteSetting()
                      {
                          return false;
                      }
                  }
                  

                  注意:如果您喜歡這種方法,但又不想通過(guò)引入 ShouldSerialize 方法來(lái)混淆類的公共接口,則可以使用 IContractResolver以編程方式做同樣的事情.請(qǐng)參閱文檔中的條件屬性序列化.

                  Note: if you like this approach but you don't want to muddy up the public interface of your class by introducing a ShouldSerialize method, you can use an IContractResolver to do the same thing programmatically. See Conditional Property Serialization in the documentation.

                  不使用 JsonConvert.SerializeObject 進(jìn)行序列化,而是將配置對(duì)象加載到 JObject 中,然后在寫(xiě)出之前從 JSON 中刪除不需要的屬性.這只是幾行額外的代碼.

                  Instead of using JsonConvert.SerializeObject to do the serialization, load the config object into a JObject, then simply remove the unwanted property from the JSON before writing it out. It's just a couple of extra lines of code.

                  JObject jo = JObject.FromObject(config);
                  
                  // remove the "ObsoleteSetting" JProperty from its parent
                  jo["ObsoleteSetting"].Parent.Remove();
                  
                  json = jo.ToString();
                  

                  方法 3:巧妙(ab)使用屬性

                  1. [JsonIgnore] 屬性應(yīng)用于您不想被序列化的屬性.
                  2. 向與原始屬性類型相同的類添加一個(gè)備用的私有屬性設(shè)置器.將該屬性的實(shí)現(xiàn)設(shè)置為原始屬性.
                  3. [JsonProperty] 屬性應(yīng)用到備用設(shè)置器,為其賦予與原始屬性相同的 JSON 名稱.
                  1. Apply a [JsonIgnore] attribute to the property that you do not want to be serialized.
                  2. Add an alternate, private property setter to the class with the same type as the original property. Make the implementation of that property set the original property.
                  3. Apply a [JsonProperty] attribute to the alternate setter, giving it the same JSON name as the original property.

                  這是修改后的Config類:

                  class Config
                  {
                      [JsonIgnore]
                      public Fizz ObsoleteSetting { get; set; }
                  
                      [JsonProperty("ObsoleteSetting")]
                      private Fizz ObsoleteSettingAlternateSetter
                      {
                          // get is intentionally omitted here
                          set { ObsoleteSetting = value; }
                      }
                  
                      public Bang ReplacementSetting { get; set; }
                  }
                  

                  這篇關(guān)于使屬性反序列化但不使用 json.net 序列化的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

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

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

                    • <tfoot id='eERau'></tfoot>

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

                          <bdo id='eERau'></bdo><ul id='eERau'></ul>
                            主站蜘蛛池模板: 国产精品国产成人国产三级 | 国产精品日韩欧美一区二区 | 天堂av中文在线 | 欧美在线观看一区 | 91精品福利 | 黄色网址在线播放 | www.久 | 99re热精品视频 | 中文字幕亚洲一区 | 91精品在线看 | 999精品在线 | 91精品国产高清一区二区三区 | 青青99 | 精品欧美一区二区精品久久久 | www.99re| 精品99在线| 九九热在线视频 | 国产精品久久久久久久免费大片 | 毛片一级网站 | 综合色播 | 精品国产青草久久久久96 | 中文字幕日韩欧美一区二区三区 | 日本小视频网站 | 亚洲一区二区在线播放 | 亚洲一区二区精品视频在线观看 | 欧美成视频 | 久久久区 | 九一在线| 国产精品一区二区精品 | 男人天堂手机在线视频 | 国产精品久久久久久久久久三级 | 日韩视频在线播放 | 视频一区二区在线观看 | 在线观看午夜视频 | 久久精品在线免费视频 | 久久在线精品 | 精品无码久久久久久国产 | 欧美久久一级特黄毛片 | 九一视频在线观看 | 国产成人精品一区二区三区在线 | 久久精品久久久 |