newtonsoft json serialize enum as string

beginer
beginer
156 Points
48 Posts

I want to serialize enums in the serialization object as string instead of numeric value.

Not like this:

},
"gender": 3,
"birthDate": "1980-01-01",

Like this:

},
"gender": "Male",
"birthDate": "1980-01-01",

I'm using following:

using Newtonsoft.Json.Serialization; // package

JsonConvert.SerializeObject(userObject);
Views: 167
Total Answered: 1
Total Marked As Answer: 0
Posted On: 08-Jul-2020 23:52

Share:   fb twitter linkedin
Answers
Jak
Jak
Member
392 Points
161 Posts
         

You can pass JsonSerialier setting as:

var jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
JsonConvert.SerializeObject(userObject, jsonSerializerSettings);
Posted On: 14-Jul-2020 01:05
 Log In to Chat