Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
C# - Web API - Serializing Enums as strings with spaces

My question is simple, but a little more specific than other questions related to serializing enumerated types as strings.

Consider the following piece of code:

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public enum MyEnum
{
    TypeOne,
    TypeTwo,
    TypeThree
}

public class Foo
{
   [JsonConverter(typeof(StringEnumConverter))]
   public MyEnum Types { get; set; }
}

When the Web API controller sends serialized Foo objects, they may look something like this:

{
    "Type" : "TypeTwo"
}

My Question: is it possible to send serialized enums as strings with spaces before each capital letter? Such a solution would produce JSON like this:

{
    "Type" : "Type Two"
}

Let me know if there's any additional information needed to solve my problem. Thanks!

EDIT:

It's preferable if the enums are only converted to strings with spaces while serializing them to JSON. I'd like to exclude spaces while using MyEnum.ToString() on the backend.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try adding EnumMember as shown below,

[JsonConverter(typeof(StringEnumConverter))]
public enum MyEnum
{
    [EnumMember(Value = "Type One")]
    TypeOne,
    [EnumMember(Value = "Type Two")]
    TypeTwo,
    [EnumMember(Value = "Type Three")]
    TypeThree
}

You may need to install a package called System.Runtime.Serialization.Primitives from Microsoft to use that.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!