OrchardCMS/OrchardCore · error · JsonException

Cannot parse number.

Error message

Cannot parse number.

What it means

This JsonException is thrown by JValue.GetObjectValue when the underlying JSON token is a Number but it cannot be materialized as any supported numeric type — the integer conversions fail and TryGetValue<double> also fails. Effectively the number is malformed or outside all supported ranges.

Solutions

  1. Fix the source JSON so numbers are valid and within double range
  2. Pre-validate the number text before parsing the document
  3. Catch JsonException around GetObjectValue and treat as parse failure
  4. Use a JsonElement TryGet pattern (TryGetInt64/TryGetDouble) to inspect the token yourself

Example fix

// before
var v = jValue.GetObjectValue();
// after
double v;
try { v = Convert.ToDouble(jValue.GetObjectValue()); }
catch (JsonException) { v = 0; }
Defensive patterns

Strategy: try-catch

Validate before calling

if (jsonElement.ValueKind != JsonValueKind.Number)
    throw new InvalidOperationException("Token is not a number");
if (!jsonElement.TryGetDouble(out _))
    throw new InvalidOperationException("Number cannot be parsed as double");

Try / catch

object? v;
try { v = jValue.GetObjectValue(); }
catch (JsonException ex) { /* log ex, treat as bad number */ v = null; }

Prevention

When it happens

Trigger: Calling GetObjectValue() on a JValue whose JsonElement ValueKind is Number but whose text cannot be parsed to double (e.g. extremely large exponents, malformed number text produced by custom writers).

Common situations: JSON documents with hand-edited or corrupted number literals; numbers exceeding double range (e.g. 1e400); interop with producers emitting non-standard numeric syntax.

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 OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/8cd91bc1f48f82b5. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.Abstractions/Json/Nodes/JValue.cs:93

                    return longValue;
                }

                if (jsonValue.TryGetValue<decimal>(out var decimalValue))
                {
                    return decimalValue;
                }

                if (jsonElement.TryGetBigInteger(out var bigInteger))
                {
                    return bigInteger;
                }

                if (jsonValue.TryGetValue<double>(out var doubleValue))
                {
                    return doubleValue;
                }

                throw new JsonException("Cannot parse number.");

            default:
                throw new JsonException(string.Format("Unknown token type {0}", jsonElement.ValueKind));
        }
    }
}

View on GitHub (pinned to 4306c0717f)