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

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

      如何全局告诉 Json.Net 将 StringEnumConverter 应用于所

      时间:2023-08-23

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

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

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

                  <tbody id='VSAFd'></tbody>

              • <legend id='VSAFd'><style id='VSAFd'><dir id='VSAFd'><q id='VSAFd'></q></dir></style></legend>
              • 本文介绍了如何全局告诉 Json.Net 将 StringEnumConverter 应用于所有枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我想用 json.net 将枚举反序列化为它们的字符串表示形式,反之亦然.我能想出告诉框架应用其 StringEnumConverter 的唯一方法是像这样注释有问题的属性:

                I want to deserialize enumerations to their string representation and vice versa with json.net. The only way I could figure out to tell the framework to apply its StringEnumConverter is to annotate the properties in question like this:

                [JsonConverter(typeof(StringEnumConverter))]
                public virtual MyEnums MyEnum { get; set; }
                

                但是,在我的用例中,全局配置 json.net 会方便得多,这样所有枚举都可以使用 StringEnumConverter 进行(反)序列化,而无需额外的注释.

                However, in my use case, it would be much more convenient to configure json.net globally such that all enumerations get (de)serialized using the StringEnumConverter, without the need of extra annotations.

                有没有办法做到这一点,例如在自定义 JsonSerializerSettings 的帮助下?

                Is there any way to do so, e.g. with the help of custom JsonSerializerSettings?

                推荐答案

                StringEnumConverter 添加到 JsonSerializerSettings 转换器集合.

                Add a StringEnumConverter to the JsonSerializerSettings Converters collection.

                文档:使用 JsonConverters 进行序列化

                Documentation: Serialize with JsonConverters

                如果你想让序列化器使用camelCasing,你也可以设置这个:

                If you want the serializer to use camelCasing, you can set this as well:

                SerializerSettings.Converters.Add(
                    new StringEnumConverter { CamelCaseText = true });
                

                这会将 SomeValue 序列化为 someValue.

                This will serialize SomeValue to someValue.

                这篇关于如何全局告诉 Json.Net 将 StringEnumConverter 应用于所有枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:C# 中的 JSON 反序列化如何工作 下一篇:将字典序列化为数组(键值对)

                相关文章

                最新文章

                  <bdo id='eW51K'></bdo><ul id='eW51K'></ul>
              • <small id='eW51K'></small><noframes id='eW51K'>

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

                    <tfoot id='eW51K'></tfoot>