ElectronNET/Electron.NET · error · JsonException

Unexpected end while reading object.

Error message

Unexpected end while reading object.

What it means

JsonToBoxedPrimitivesConverter.ReadValue throws this JsonException when it finishes consuming tokens without ever encountering the object's EndObject token — i.e. the JSON stream ended while still inside an object. It is the outer 'unexpected EOF inside object' guard, distinct from the per-property truncation errors.

Solutions

  1. Ensure objects are closed with '}' — validate JSON before sending
  2. Check transport (IPC/pipe) is not truncating messages
  3. Log the raw received string on failure to find where it cuts off
  4. Use JSON.stringify/JSON.parse round-trip on the sender to guarantee well-formed output

Example fix

// before
var json = "{ \"width\": 800"; // missing }
// after
var json = "{ \"width\": 800 }";
Defensive patterns

Strategy: try-catch

Validate before calling

bool isCompleteObject(string json) =>
    json.TrimStart().StartsWith("{") && json.TrimEnd().EndsWith("}");

Try / catch

try
{
    var value = JsonSerializer.Deserialize<object>(json, ElectronJson.Options);
}
catch (JsonException ex)
{
    logger.LogWarning(ex, "Incomplete JSON object: {Raw}", json);
}

Prevention

When it happens

Trigger: A JSON object missing its closing brace, e.g. '{ "width": 800 ' with no '}', or input stream ending mid-object while the reader loop runs out of tokens.

Common situations: Payloads cut off by IPC/network limits; incomplete file reads; string slicing bugs dropping the tail of the JSON.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


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

Appendix: source

Thrown at src/ElectronNET.API/Serialization/JsonToBoxedPrimitivesConverter.cs:43

                        {
                            return obj;
                        }

                        if (r.TokenType != JsonTokenType.PropertyName)
                        {
                            throw new JsonException("Expected property name.");
                        }

                        string name = r.GetString()!;
                        if (!r.Read())
                        {
                            throw new JsonException("Unexpected end while reading property value.");
                        }

                        obj[name] = ReadValue(ref r);
                    }

                    throw new JsonException("Unexpected end while reading object.");

                case JsonTokenType.StartArray:

                    var list = new List<object>();
                    while (r.Read())
                    {
                        if (r.TokenType == JsonTokenType.EndArray)
                        {
                            return list;
                        }

                        list.Add(ReadValue(ref r));
                    }

                    throw new JsonException("Unexpected end while reading array.");

                case JsonTokenType.True: return true;
                case JsonTokenType.False: return false;

View on GitHub (pinned to 87cc6f98b6)