reactiveui/refit · error · JsonException

Cannot convert an empty value to {typeof(TEnum)}.

Error message

Cannot convert an empty value to {typeof(TEnum)}.

What it means

Thrown by CamelCaseStringEnumConverter when the JSON reader is on a string (or property name) token whose value is null or entirely whitespace, and the target type is an enum. An empty value cannot map to any enum field, so deserialization fails with a JsonException.

Source

Thrown at src/Refit/CamelCaseStringEnumConverter.cs:208

                valuesToNames[value] = preferredName;
            }

            return (namesToValues, namesToValuesIgnoreCase, valuesToNames);
        }

        /// <summary>Reads an enum value from either a string name or a numeric value.</summary>
        /// <param name="reader">The reader positioned on the value to read.</param>
        /// <returns>The parsed enum value.</returns>
        /// <exception cref="JsonException">The value is an empty or whitespace string, a name that maps to no enum field, or a token that is neither a string nor a number.</exception>
        internal TEnum ReadValue(ref Utf8JsonReader reader)
        {
            if (reader.TokenType is JsonTokenType.String or JsonTokenType.PropertyName)
            {
                var value = reader.GetString();
                if (value is null || string.IsNullOrWhiteSpace(value))
                {
                    throw new JsonException($"Cannot convert an empty value to {typeof(TEnum)}.");
                }

                if (_namesToValues.TryGetValue(value!, out var namedValue))
                {
                    return namedValue;
                }

                if (_namesToValuesIgnoreCase.TryGetValue(value!, out var namedValueIgnoreCase))
                {
                    return namedValueIgnoreCase;
                }

                throw new JsonException($"Unable to convert '{value}' to {typeof(TEnum)}.");
            }

            if (reader.TokenType == JsonTokenType.Number)
            {
                return EnumHelpers.Info<TEnum>.ReadJsonNumericValue(ref reader);

View on GitHub (pinned to b455f65ecc)

Solutions

  1. Make the receiving property nullable (MyEnum?) and ensure the JSON omits the field or sends null for absent values.
  2. Fix the server/source to send a valid enum name or null instead of an empty string.
  3. Pre-process/sanitize the JSON (replace blank enum strings with null) before deserialization if you cannot change the source.

Example fix

// before — non-nullable enum, API sends ""
public sealed record Payload(Status Status);
var p = JsonSerializer.Deserialize<Payload>("{\"status\":\"\"}"); // throws

// after — nullable enum tolerates missing/blank (after sanitizing)
public sealed record Payload(Status? Status);
// or have the API send null / omit the field / send a valid name
Defensive patterns

Strategy: validation

Validate before calling

// Make the enum nullable so blank values can be tolerated, and sanitize input.
public sealed record Payload(Status? Status);
// Before deserializing, replace blank enum strings with null if you cannot change the source:
var sanitized = Regex.Replace(json, @"""(\w+)""\s*:\s*""\s*""", "$1:null");

Try / catch

try { return JsonSerializer.Deserialize<Payload>(json); }
catch (JsonException) { /* log/handle bad enum value */ return null; }

Prevention

When it happens

Trigger: Deserializing JSON into an enum-typed property where the JSON value is "" or " ", e.g. `{ "status": "" }`. This can occur for either a non-nullable enum or, depending on the converter path, a nullable enum whose value is present but blank.

Common situations: An API returns an empty string for an absent enum value instead of null or omitting the field; data from a form/CSV import that produced blank enum cells; a default value mis-set to empty string.

Related errors


AI-assisted analysis of reactiveui/refit@b455f65ecc (2026-08-13). Data as JSON: /api/errors/d1f59e482444f795. Report an issue: GitHub.