ElectronNET/Electron.NET · error · JsonException

Invalid value for PageSize. Expected string or an object.

Error message

Invalid value for PageSize. Expected string or an object.

What it means

PageSizeConverter.Read deserializes a PageSize that Electron may send either as a string (preset name like 'A4') or as an object (explicit width/height). Any other JSON token — number, true/false, etc. — has no mapping, so the converter throws JsonException "Invalid value for PageSize. Expected string or an object.".

Solutions

  1. Send PageSize as a preset string: "pageSize": "A4".
  2. Or send an explicit object: "pageSize": { "width": 100, "height": 100 }.
  3. Convert numeric page-size codes to the preset name on the JS side before sending.
  4. If numbers are a legitimate new input, update PageSizeConverter.Read to handle JsonTokenType.Number.

Example fix

// before
{ "pageSize": 9 }
// after
{ "pageSize": "A4" }
Defensive patterns

Strategy: type-guard

Validate before calling

function isValidPageSize(v: unknown): boolean {
  return typeof v === 'string' || (typeof v === 'object' && v !== null && 'width' in v && 'height' in v);
}
if (!isValidPageSize(options.pageSize)) throw new TypeError('pageSize must be a preset string or {width,height}');

Type guard

function isPageSize(v: unknown): v is string | { width: number; height: number } {
  return typeof v === 'string' ||
    (typeof v === 'object' && v !== null && typeof (v as any).width === 'number' && typeof (v as any).height === 'number');
}

Try / catch

try
{
    var pageSize = JsonSerializer.Deserialize<PageSize>(element.GetRawText(), ElectronJson.Options);
}
catch (JsonException ex) when (ex.Message.Contains("Invalid value for PageSize"))
{
    Logger.Error("pageSize must be a preset string (e.g. \"A4\") or an object with width/height.");
}

Prevention

When it happens

Trigger: Deserializing a PageSize property from JSON where the value is a number (e.g. "pageSize": 9), boolean, or other non-string/non-object token, via JsonSerializer.Deserialize<PageSize> under ElectronJson.Options.

Common situations: JS caller passed a numeric page-size code instead of a preset name or {width,height} object; API version change where Electron began sending numbers; hand-written test payloads with the wrong type; copy/paste from docs targeting a different API.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of ElectronNET/Electron.NET@87cc6f98b6 (2026-09-14). Data as JSON: /api/errors/4d53185efa5b7ee4. Report an issue: GitHub.

Appendix: source

Thrown at src/ElectronNET.API/Converter/PageSizeConverter.cs:23

using System.Text.Json.Serialization;

namespace ElectronNET.Converter;

public class PageSizeConverter : JsonConverter<PageSize>
{
    public override PageSize Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.String)
        {
            return reader.GetString();
        }
        else if (reader.TokenType == JsonTokenType.StartObject)
        {
            return JsonSerializer.Deserialize<PageSize>(ref reader, ElectronJson.Options);
        }
        else
        {
            throw new JsonException("Invalid value for PageSize. Expected string or an object.");
        }
    }

    public override void Write(Utf8JsonWriter writer, PageSize value, JsonSerializerOptions options)
    {
        if (value is null)
        {
            return;
        }

        var str = (string)value;

        if (str is not null)
        {
            writer.WriteStringValue(str);
        }
        else
        {

View on GitHub (pinned to 87cc6f98b6)