Humanizr/Humanizer · error · InvalidOperationException
Expected JSON object.
Error message
Expected JSON object.
What it means
CreateTargetTypedConstructorExpression (GenerationHelpers.cs:403-408) emits a target-typed 'new(...)' constructor call from a JSON object's properties. It throws when the supplied element is not a JSON Object (e.g. an array or scalar), because constructor arguments are read from object properties.
Source
Thrown at src/Humanizer.SourceGenerators/Common/GenerationHelpers.cs:407
builder.Append(", ");
}
builder.Append("[TimeUnit.");
builder.Append(ToEnumMemberName(entry.Name));
builder.Append("] = GrammaticalGender.");
builder.Append(ToEnumMemberName(entry.Value.GetString()!));
first = false;
}
builder.Append(" }.ToFrozenDictionary()");
return builder.ToString();
}
static string CreateTargetTypedConstructorExpression(JsonElement element, params JsonConstructorValueEmitter[] emitters)
{
if (element.ValueKind != JsonValueKind.Object)
{
throw new InvalidOperationException("Expected JSON object.");
}
var builder = new StringBuilder("new(");
AppendConstructorValues(builder, element, emitters);
builder.Append(')');
return builder.ToString();
}
static string CreateTypedConstructorArrayExpression(string elementTypeName, JsonElement arrayElement, params JsonConstructorValueEmitter[] emitters)
{
if (arrayElement.ValueKind != JsonValueKind.Array)
{
throw new InvalidOperationException("Expected JSON array.");
}
var builder = new StringBuilder("new ");
builder.Append(elementTypeName);
builder.Append("[] { ");View on GitHub (pinned to ffc2b77c0f)
Solutions
- Ensure the element at the contract's sourcePath is a JSON object.
- Correct the contract member kind (e.g. use the array constructor emitter) if the data is genuinely a list.
- Fix the sourcePath to reference the single object node.
Example fix
// before - array where object expected
[ { "base": 10 } ]
// after - object
{ "base": 10 } Defensive patterns
Strategy: type-guard
Validate before calling
// Target-typed constructor input must be a JSON object (C#)
if (element.ValueKind != JsonValueKind.Object)
throw new InvalidOperationException("Expected JSON object for constructor expression"); Type guard
static bool IsJsonObject(JsonElement el) => el.ValueKind == JsonValueKind.Object;
Prevention
- Ensure contract sourcePaths for constructor members point at a single object.
- Pick the array-constructor emitter when the data is a list.
- Validate the top-level ValueKind matches the emitter before invoking it.
When it happens
Trigger: A contract member that should be a single object is given a JSON array or scalar at its sourcePath.
Common situations: A contract sourcePath points at a list where a single record is expected; data reshaped so a record became an array; wrong member kind declared in the contract.
Related errors
- Property '{propertyName}' must be an integer.
- Indexed string arrays require string values. Property '{prop
- Missing required property '{sourcePath}'.
- Missing required property '{propertyName}'.
- Expected JSON array.
AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13).
Data as JSON: /api/errors/0fce7dcc0ca7e252.
Report an issue: GitHub.