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

    1. <i id='Mt5FA'><tr id='Mt5FA'><dt id='Mt5FA'><q id='Mt5FA'><span id='Mt5FA'><b id='Mt5FA'><form id='Mt5FA'><ins id='Mt5FA'></ins><ul id='Mt5FA'></ul><sub id='Mt5FA'></sub></form><legend id='Mt5FA'></legend><bdo id='Mt5FA'><pre id='Mt5FA'><center id='Mt5FA'></center></pre></bdo></b><th id='Mt5FA'></th></span></q></dt></tr></i><div id='Mt5FA'><tfoot id='Mt5FA'></tfoot><dl id='Mt5FA'><fieldset id='Mt5FA'></fieldset></dl></div>
        <bdo id='Mt5FA'></bdo><ul id='Mt5FA'></ul>
        <legend id='Mt5FA'><style id='Mt5FA'><dir id='Mt5FA'><q id='Mt5FA'></q></dir></style></legend>
        <tfoot id='Mt5FA'></tfoot>
      1. 有选择地使用默认 JSON 转换器

        时间:2023-08-24

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

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

            • <legend id='tlyzn'><style id='tlyzn'><dir id='tlyzn'><q id='tlyzn'></q></dir></style></legend><tfoot id='tlyzn'></tfoot>

                  <tbody id='tlyzn'></tbody>
                • 本文介绍了有选择地使用默认 JSON 转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在我的 Web API 项目的 Startup.cs 中使用以下内容将枚举 JSON 序列化为字符串:

                  I use the following in my Web API project's Startup.cs to JSON-serialize Enums into strings:

                  // Configure JSON Serialization
                  var jsonSerializationSettings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
                  jsonSerializationSettings.Formatting = Newtonsoft.Json.Formatting.None;
                  jsonSerializationSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
                  

                  这是为了避免使用 [JsonConverter(typeof(StringEnumConverter))]

                  现在,我如何有选择地退出某些 Enum 属性的全局序列化设置并使用转换为整数的默认序列化程序?

                  Now, how can I selectively opt out of my global serialization setting for some Enum properties and use the default serializer that converts to integers?

                  推荐答案

                  您可以将虚拟转换器添加到有问题的属性中,但不执行任何操作:

                  You could add a dummy converter to the properties in question that does nothing:

                  public class NoConverter : JsonConverter
                  {
                      public override bool CanConvert(Type objectType)
                      {
                          // Note - not called when attached directly via [JsonConverter(typeof(NoConverter))]
                          throw new NotImplementedException();
                      }
                  
                      public override bool CanRead { get { return false; } }
                  
                      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                      {
                          throw new NotImplementedException();
                      }
                  
                      public override bool CanWrite { get { return false; } }
                  
                      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                      {
                          throw new NotImplementedException();
                      }
                  }
                  

                  然后使用 [JsonConverter(typeof(NoConverter))].这样做后,JsonConverter 属性的转换器取代了全局指定的转换器,但由于 CanReadCanWrite 都返回 false,因此不执行任何转换.对于枚举集合,您可以使用 [JsonProperty(ItemConverterType = typeof(NoConverter))].

                  Then attach it to the property using [JsonConverter(typeof(NoConverter))]. Having done so, the JsonConverter attribute's converter supersedes the globally specified converter, but since CanRead and CanWrite both return false no conversion is performed. For collections of enums, you could use [JsonProperty(ItemConverterType = typeof(NoConverter))].

                  例如,如果您定义类型:

                  For instance, if you define the types:

                  public enum Foo { A, B, C }
                  
                  public class RootObject
                  {
                      [JsonConverter(typeof(NoConverter))]
                      public Foo FooAsInteger { get; set; }
                  
                      public Foo FooAsString { get; set; }
                  }
                  

                  然后

                  var root = new RootObject { FooAsInteger = Foo.B, FooAsString = Foo.B };
                  
                  var json = JsonConvert.SerializeObject(root, Formatting.Indented, new StringEnumConverter());
                  
                  Console.WriteLine(json);
                  

                  产生输出

                  {
                    "FooAsInteger": 1,
                    "FooAsString": "B"
                  }
                  

                  请注意,如果您希望所有数据模型中所有出现的枚举都序列化为整数,也可以将 NoConverter 直接应用于枚举:

                  Note that you can also apply NoConverter directly to the enum, if you want all occurrences of the enum in all data models to be serialized as an integer:

                  [JsonConverter(typeof(NoConverter))]
                  public enum FooAlwaysAsInteger { A, B, C }
                  

                  示例 fiddle.

                  这篇关于有选择地使用默认 JSON 转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Json.net 全局设置 下一篇:确保 .NET 中的 json 键是小写的

                  相关文章

                  最新文章

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

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