Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7409205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:02:50+00:00 2026-05-29T06:02:50+00:00

I have an enum: public enum Action { Remove=1, Add=2 } And a class:

  • 0

I have an enum:

public enum Action {
    Remove=1,
    Add=2
}

And a class:

[DataContract]
public class Container {
    [DataMember]
    public Action Action {get; set;}
}

When serialize instance of Container to json I get: {Action:1} (in case Action is Remove).

I would like to get: {Action:Remove} (instead of int I need to ToString form of the enum)

Can I do it without adding another member to the class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:02:51+00:00Added an answer on May 29, 2026 at 6:02 am

    The JSON formatter has very specialized behaviour when working with enumerations; the normal Data Contract attributes are ignored and it treats your enum as a number, not the more human-readable string you’d expect with other formats. Whilst this makes it easy to deal with flag-type enumerations, it makes most other types much harder to work with.

    From MSDN:

    Enumeration member values are treated as numbers in JSON, which is
    different from how they are treated in data contracts, where they are
    included as member names. For more information about the data contract
    treatment, see Enumeration Types in Data Contracts.

    • For example, if you have public enum Color {red, green, blue, yellow,
      pink}
      , serializing yellow produces the number 3 and not the string
      “yellow”.

    • All enum members are serializable. The EnumMemberAttribute and the
      NonSerializedAttribute attributes are ignored if used.

    • It is possible to deserialize a nonexistent enum value – for example,
      the value 87 can be deserialized into the previous Color enum even
      though there is no corresponding color name defined.

    • A flags enum is not special and is treated the same as any other enum.

    The only practical way to resolve this, to allow end-users to specify a string instead of a number, is to not use the enum in your contract. Instead the practical answer is to replace your enum with a string and perform internal validation on the value such that it can be parsed into one of the valid enum representations.

    Alternatively (though not for the feint of heart), you could replace the JSON formatter with your own, which would respect enumerations in the same way as other formatters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Actions with an Enum in it [DataContract] public class Actions
So, I have an abstract class like: public abstract class AbstractParent <E extends Enum<E>>
i have enum public enum TypeImage { Img=0, Thumb=1 } i want in view
I have enum like this [Flags] public enum Key { None = 0, A
I have enum like this: public enum ObectTypes { TypeOne, TypeTwo, TypeThree, ... TypeTwenty
I have an enum public enum FileExtentions { mp3, mpeg } And I have
I have an enum: public enum baseKey : uint { HKEY_CLASSES_ROOT = 0x80000000, HKEY_CURRENT_USER
I have an enum public enum Color { Red = 0, Blue = 1,
I have the following enum: public enum Status implements StringEnum{ ONLINE(on),OFFLINE(off); private String status
I have this enum: [Flags] public enum ExportFormat { None = 0, Csv =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.