How to tell Json.Net globally to apply the StringEnumConverter to all enums
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When an API sends enums as integers, clients become tightly coupled to the server’s internal numeric values. A renamed member or inserted enum item can silently break consumers that assumed a fixed numeric mapping. In most public APIs, enum strings are safer because they are explicit, readable, and easier to debug in logs. Json.NET solves this with StringEnumConverter, but many teams only apply it on individual properties and end up with inconsistent payloads. The reliable approach is to configure conversion globally so every enum is serialized and deserialized using the same rule.
Global configuration also reduces repetition. You do not need [JsonConverter] attributes on every model, and new enums automatically follow the project standard. This article shows how to set that up, how to harden deserialization behavior, and how to avoid common mistakes that produce confusing runtime bugs.
Core Sections
Configure a global converter in plain Json.NET
If you serialize with JsonConvert.SerializeObject, attach the converter once through JsonSerializerSettings and reuse that settings object.
This ensures every enum in the object graph uses string values. If your API contract expects exact enum names, instantiate StringEnumConverter() without a naming strategy.
Configure globally in ASP.NET Core (Newtonsoft.Json)
If your application is ASP.NET Core and uses AddNewtonsoftJson, register the converter in startup so controllers inherit it automatically.
Now any request/response model enum goes through the same converter. This keeps contract behavior consistent across endpoints and avoids per-controller drift.
Handle unknown enum values intentionally
By default, deserialization throws if an enum string cannot map to a member. For external clients, that can produce noisy failures. You can introduce a defensive fallback by adding an Unknown member and validating input.
This pattern is especially useful for queue consumers, import jobs, or backward-compatible APIs where you prefer graceful degradation over hard failure.
Use flags enums carefully
[Flags] enums serialize as comma-separated names (for example, "Read, Write") when values combine. That may be hard for non-.NET clients. If interoperability matters, document the format explicitly or avoid flags in external contracts.
For internal services, this is fine. For public APIs, consider sending a string array (["read","write"]) through a dedicated DTO so consumers parse predictably.
Prefer one policy for naming
If enum strings are camelCase but enum declarations are PascalCase, team members may assume a bug when reading payloads. Pick one naming strategy and enforce it in tests.
Contract tests like this catch accidental configuration changes during framework upgrades or refactors.
Common Pitfalls
- Registering
StringEnumConverterin one serialization path but not another, causing the same model to emit both integers and strings. - Mixing
System.Text.Jsondefaults with Json.NET settings in the same app and assuming converters apply to both serializers. - Applying a naming strategy globally without checking client expectations, which can break strict string matching in existing integrations.
- Forgetting to document
[Flags]enum output format, leading external consumers to mis-parse combined values. - Treating unknown enum values as impossible and letting imports fail instead of providing an explicit fallback policy.
Summary
Global enum string handling in Json.NET is mostly about consistency. Configure StringEnumConverter once at the serializer level, decide your naming policy, and test contract output so behavior stays stable over time. If your application receives data from outside your control, add a strategy for unknown values instead of relying on exceptions alone. For public APIs, be careful with flags enums and document wire formats clearly. With these practices, enum serialization becomes predictable, readable, and safer for long-lived integrations.
Related reading
- How to terminate a thread in C?
- How to touch a file in C?
- How to truncate milliseconds off of a .NET DateTime
- How to turn off or handle camelCasing in JSON response ASP.NET Core?
- How to turn off the logging done by the ASP.NET core framework
- How to upload a file to amazon S3 super easy using c
- How to upload a file to amazon S3 super easy using c
- How to use a App.config file in WPF applications?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.