Humanizr/Humanizer · error · InvalidOperationException

Expected JSON array or mapping.

Error message

Expected JSON array or mapping.

What it means

Thrown by the inverted-tens-scale-array builder when the YAML node is neither a JSON array nor a JSON object. The builder accepts two shapes (a sequence of rows or a mapping of token->integer), so any scalar, boolean, or null is rejected because it cannot be enumerated either way.

Source

Thrown at src/Humanizer.SourceGenerators/Common/GenerationHelpers.cs:1215

        builder.Append(", ");
        builder.Append(QuoteLiteral(GetRequiredString(objectElement, "paucal")));
        builder.Append(", ");
        builder.Append(QuoteLiteral(GetRequiredString(objectElement, "plural")));

        if (GetBoolean(objectElement, "omitLeadingOne"))
        {
            builder.Append(", true");
        }

        builder.Append(')');
        return builder.ToString();
    }

    static string CreateInvertedTensTokenArrayExpression(JsonElement arrayElement)
    {
        if (arrayElement.ValueKind is not (JsonValueKind.Array or JsonValueKind.Object))
        {
            throw new InvalidOperationException("Expected JSON array or mapping.");
        }

        var builder = new StringBuilder("new InvertedTensToken[] { ");
        var first = true;

        if (arrayElement.ValueKind == JsonValueKind.Object)
        {
            foreach (var (Name, Value) in arrayElement.EnumerateObject()
                         .Select(static property =>
                         {
                             return property.Value.ValueKind != JsonValueKind.Number || !property.Value.TryGetInt64(out var value)
                                 ? throw new InvalidOperationException($"Inverted tens token '{property.Name}' must map to an integer value.")
                                 : (property.Name, Value: value);
                         })
                         .OrderByDescending(static entry => entry.Value)
                         .ThenBy(static entry => entry.Name, StringComparer.Ordinal))
            {
                if (!first)

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Convert the YAML value to either a sequence of mappings or a mapping of token name to integer value.
  2. For the mapping form, each key is the token string and each value is an integer (e.g. einundzwanzig: 21).
  3. Rebuild to confirm the generator accepts the corrected node.

Example fix

# before
invertedTens: 'einundzwanzig'
# after
invertedTens:
  einundzwanzig: 21
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the inverted-tens node is a YAML sequence or mapping before
// building. Scalars, booleans, and null are rejected.

Prevention

When it happens

Trigger: A locale YAML provides a scalar (string or number) or null for an inverted-tens-scale-array section. The ValueKind pattern match 'is not (Array or Object)' catches it before iteration.

Common situations: Editing an inverted-tens locale (e.g. a Germanic-style inverted tens like 'einundzwanzig') and replacing the token map/list with a single quoted string. Appears as a CS8785 generator failure at build time.

Related errors


AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13). Data as JSON: /api/errors/4c4003ab28560516. Report an issue: GitHub.