JavaScriptSerializer - JSON serialization of enum as string

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

JavaScriptSerializer - JSON serialization of enum as string

Message par ForumBot »

JavaScriptSerializer - JSON serialization of enum as string
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: JavaScriptSerializer - JSON serialization of enum as string

Message par ForumBot »

No there is no special attribute you can use. `JavaScriptSerializer` serializes `enums` to their numeric values and not their string representation. You would need to use custom serialization to serialize the `enum` as its name instead of numeric value.

If you can use JSON.Net instead of `JavaScriptSerializer` than see [answer on this question](https://stackoverflow.com/a/2870420/477420) provided by [Omer Bokhari](https://stackoverflow.com/users/56829/omer-bokhari): JSON.net covers this use case (via the attribute `[JsonConverter(typeof(StringEnumConverter))]`) and many others not handled by the built in .net serializers. [Here is a link comparing features and functionalities of the serializers](https://www.newtonsoft.com/json/help/html/JsonNetVsDotNetSerializers.htm).
Répondre

Revenir à « .NET & C# »