javascriptserializer and json.net enumeration serialized into string
When using the JavaScriptserializer serialized objects that contain enumeration attributes, the default behavior is an integer value output an enumerated value instead of its string representation. This article discusses the method of customizing this behavior and obtaining enumeration string in the JSON output. There is no need to use custom JavaScriptConverters.
JSON.NET provides more convenient solutions through its JSONCONVERTER attributes and built -in StringenumConverter types. By applying the attribute to the related enumeration attributes:
... You can specify the enumeration to serialize as a string. For more information about StringenumConverter, see its documents.
<code class="language-csharp">using Newtonsoft.Json; using Newtonsoft.Json.Converters; [JsonConverter(typeof(StringEnumConverter))] public Gender Gender { get; set; }</code>
There are other methods that can configure this converter global:
Epolid definition:Add the attribute to the lift itself to ensure that all enumeration instances serialize into string.
<code class="language-csharp">serializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());</code>
The above is the detailed content of How to Serialize Enums as Strings Using JavaScriptSerializer and Json.NET?. For more information, please follow other related articles on the PHP Chinese website!