Search code examples
c#serializationjsonconvert

Enum serialization as string without string quotes


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

Solution

  • 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.