Comments on: How to Serialize Enum to a String in C# https://code-maze.com/csharp-serialize-enum-to-string/ Learn. Code. Succeed. Sat, 03 Dec 2022 11:21:01 +0000 hourly 1 https://wordpress.org/?v=6.7.5 By: Anonymous https://code-maze.com/csharp-serialize-enum-to-string/#comment-6994 Sat, 03 Dec 2022 11:21:01 +0000 https://drafts.code-maze.com/?p=67044#comment-6994 I love you for this tutorial!

]]>
By: Rob https://code-maze.com/csharp-serialize-enum-to-string/#comment-6878 Fri, 04 Nov 2022 18:20:53 +0000 https://drafts.code-maze.com/?p=67044#comment-6878 In reply to Ahsan Ullah.

Exactly. I would expect an exception when giving a value that is not in the enum range,but in stead it is accepted as a “99”

]]>
By: Ahsan Ullah https://code-maze.com/csharp-serialize-enum-to-string/#comment-6877 Fri, 04 Nov 2022 16:00:02 +0000 https://drafts.code-maze.com/?p=67044#comment-6877 In reply to Mohd Imran.

You are most welcome, Imran. We have a lot of great articles here :). I hope you will enjoy reading those too. Thanks for your feedback.

]]>
By: Ahsan Ullah https://code-maze.com/csharp-serialize-enum-to-string/#comment-6876 Fri, 04 Nov 2022 15:55:29 +0000 https://drafts.code-maze.com/?p=67044#comment-6876 In reply to Rob.

Sorry, I’m not quite sure what you’re trying to achieve. You have a JSON string and you want it to deserialize back to enum but not getting expected result – is that what you mean?

]]>
By: Ahsan Ullah https://code-maze.com/csharp-serialize-enum-to-string/#comment-6875 Fri, 04 Nov 2022 14:07:43 +0000 https://drafts.code-maze.com/?p=67044#comment-6875 In reply to Thomas R.

Glad you find it useful :). Thanks for the feedback!

]]>
By: Mohd Imran https://code-maze.com/csharp-serialize-enum-to-string/#comment-6874 Fri, 04 Nov 2022 08:22:56 +0000 https://drafts.code-maze.com/?p=67044#comment-6874 Thanks Ahsan bhai!

]]>
By: Rob https://code-maze.com/csharp-serialize-enum-to-string/#comment-6873 Thu, 03 Nov 2022 13:50:46 +0000 https://drafts.code-maze.com/?p=67044#comment-6873 Can I ask you a quick question: How do I handle a number inside a string as an invalid value for an enum? Like MyNiceEnum= “99” is accepted (what I do not want) and MyNiceEnum= 99 is not accepted (what I want) for this enum:

public enum MyNiceEnum
  {
    [Display(Name = “FirstValue”)]
    MyFirstValue = 0,

    [Display(Name = “Secondvalue”)]
    MySecondValue = 1
  }
and this converter is added :

 options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase, false));

]]>
By: Thomas R https://code-maze.com/csharp-serialize-enum-to-string/#comment-6237 Thu, 04 Aug 2022 13:59:12 +0000 https://drafts.code-maze.com/?p=67044#comment-6237 Thank you, very helpful!

]]>
By: Ahsan Ullah https://code-maze.com/csharp-serialize-enum-to-string/#comment-5983 Wed, 29 Jun 2022 06:59:13 +0000 https://drafts.code-maze.com/?p=67044#comment-5983 In reply to Mike.

Thanks, Mike.

You can easily implement a custom policy with your desired logic:

public class CapitalCaseNamingPolicy : JsonNamingPolicy
  {
    public override string ConvertName(string name)
      => name?.ToUpperInvariant() ?? string.Empty;
  }

and pass it with the converter as usual:

var options = new JsonSerializerOptions();
        options.Converters.Add(new JsonStringEnumConverter(new CapitalCaseNamingPolicy()));


 var json = JsonSerializer.Serialize(new { BackColor = Color.DarkGray }, options);

Assert.Equal("{\"BackColor\":\"DARKGRAY\"}", json);
]]>
By: Mike https://code-maze.com/csharp-serialize-enum-to-string/#comment-5980 Tue, 28 Jun 2022 15:07:14 +0000 https://drafts.code-maze.com/?p=67044#comment-5980 Thanks for this article.
What’s about all capital letters?

]]>