newtonsoft json serialize enum as string

beginer
beginer
1544 Points
52 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: 22115
Total Answered: 2
Total Marked As Answer: 1
Posted On: 08-Jul-2020 23:52

Share:   fb twitter linkedin
Answers
Jak
Jak
908 Points
132 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
Thanks
 - beginer  10-Jul-2023 05:01
Great!
 - Rashmi  10-Jul-2023 05:03
great!
 - Raj  25-Oct-2023 06:09
Rashmi
Rashmi
1068 Points
19 Posts
         

Great! Help me a lot.

Posted On: 10-Jul-2023 05:25
 Log In to Chat