JSON Serialization β
Raiqub Enum Utilities makes it easy to customize JSON serialization and deserialization for your enums. By adding [JsonConverterGenerator]
, the source generator can produce specialized converters that keep JSON I/O aligned with [JsonPropertyName]
, [EnumMember]
, or the literal member names.
Example β
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
[JsonConverterGenerator]
[JsonConverter(typeof(SeasonJsonConverter))]
public enum Season
{
[JsonPropertyName("springTime")]
[EnumMember(Value = "π±")]
Spring,
[EnumMember(Value = "βοΈ")]
Summer,
[EnumMember(Value = "π")]
Autumn,
[EnumMember(Value = "β")]
Winter
}
Here, [JsonPropertyName("springTime")]
takes precedence over [EnumMember]
and the literal Spring
, so itβs serialized as springTime
. Meanwhile, Summer
defaults to \u2600\uFE0F
(Unicode for βοΈ).
This will generate code on the following classes:
Whatβs Generated? β
When you decorate your enum with [JsonConverterGenerator]
, a class like SeasonJsonConverter
is generated at build time. This converter:
- Reads from JSON strings or numeric values (if
AllowIntegerValues
istrue
). - Writes out members following
[JsonPropertyName]
β[EnumMember]
β the member name. - Integrates seamlessly with
System.Text.Json
.
Using [JsonPropertyName]
and [EnumMember]
β
Because these attributes are recognized by the generated code, your enums can use custom string identifiers in JSON. When both attributes are present, [JsonPropertyName]
overrides [EnumMember]
.
Order of Precedence for Member Names β
[JsonPropertyName]
[EnumMember]
- Literal enum name
{
"currentSeason": "springTime"
}
This "springTime"
value maps back to Season.Spring
during deserialization.
Example Usage β
var json = JsonSerializer.Serialize(new { currentSeason = Season.Spring });
// => { "currentSeason": "springTime" }
var obj = JsonSerializer.Deserialize<SomeModel>(json);
// obj.currentSeason == Season.Spring
Generator Custom Options β
The [JsonConverterGenerator]
attribute offers additional settings for your converter:
AllowIntegerValues
(default:true
)- Indicates whether integer values are allowed during deserialization.
- If
false
, numeric representations are rejected in favor of strings.
IgnoreCase
(default:false
)- Specifies whether comparisons ignore case.
- When
true
, values like"SpringTime"
or"springtime"
still deserialize toSeason.Spring
.
DeserializationFailureFallbackValue
(default:null
)- Assigns a fallback enum member if deserialization fails.
- If not specified, a failure triggers an exception.
[JsonConverterGenerator(
AllowIntegerValues = false,
IgnoreCase = true,
DeserializationFailureFallbackValue = (int)Season.Autumn
)]
public enum Season
{
// ...
}
When to Use β
Consider [JsonConverterGenerator]
if you:
- Prefer zero-reflection or zero-boilerplate solutions.
- Need to handle custom strings via
[JsonPropertyName]
or[EnumMember]
. - Want granular control over case sensitivity and integer parsing.
- Require a fallback value on deserialization errors.
By relying on source-generated JSON converters, you ensure robust, efficient integration with System.Text.Json
, allowing your enums to behave precisely as intended.