1

I want to serialize enum as string using JSON.NET but I want Enum value to serialize without string quotes

    [JsonConverter(typeof(StringEnumConverter))]
    enum Gender { Male, Female }

    class Test
    {
        public Gender { get; set; }
    }

Right now I'm getting output as { "Gender": "Male" }

{ "Gender": Male }  //Male is without quotes
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66

2 Answers2

2

It is not possible because that json will be invalid. You can check how valid json looks like on https://jsonlint.com.

All JSON Convert libraries are following valid json structure, you can always write your own json converter to do that, but I wouldn't recommend that at all.

Djuro
  • 384
  • 2
  • 9
0

This is not possible (and really not recommended) because it does not respect JSON syntax.

See : [https://www.w3schools.com/js/js_json_syntax.asp][1]

In JSON, values must be one of the following data types:

a string a number an object (JSON object) an array a boolean null