Home >Backend Development >C++ >How to Serialize Enums as Strings Using JavaScriptSerializer?
Attribute -based converter
type. This will indicate that the serialization program converts the enumeration value to a string: [JsonConverter]
StringEnumConverter
<code class="language-csharp">using Newtonsoft.Json; using Newtonsoft.Json.Converters; [JsonConverter(typeof(StringEnumConverter))] public Gender Gender { get; set; }</code>
StringEnumConverter
JsonSerializer
Global configuration
<code class="language-csharp">serializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());</code>
enumeration: JsonConverter
jsonconveter:
<code class="language-csharp"> [JsonConverter(typeof(StringEnumConverter))] public enum Gender { Male, Female }</code>
Other options Lialingcase:
Use the<code class="language-csharp"> JsonConvert.SerializeObject(MyObject, new Newtonsoft.Json.Converters.StringEnumConverter());</code>Construction function to control the string of the string and whether to accept the number.
The above is the detailed content of How to Serialize Enums as Strings Using JavaScriptSerializer?. For more information, please follow other related articles on the PHP Chinese website!