apple/pkl · error · ParseException

Cannot parse `%s` as number.

Error message

Cannot parse `%s` as number.

What it means

While parsing a JSON document, a number token could not be converted to either a Long or a Double. This means the token is syntactically a number per the tokenizer but numerically unrepresentable in Java (typically an out-of-range literal).

Source

Thrown at pkl-core/src/main/java/org/pkl/core/stdlib/json/ParserNodes.java:107

    @Override
    public void endBoolean(boolean value) {
      this.value = value;
    }

    @Override
    public void endString(String string) {
      value = string;
    }

    @Override
    public void endNumber(String string) {
      try {
        value = Long.valueOf(string);
      } catch (NumberFormatException e) {
        try {
          value = Double.valueOf(string);
        } catch (NumberFormatException e2) {
          throw new ParseException("Cannot parse `" + string + "` as number.", getLocation());
        }
      }
    }

    @Override
    public EconomicMap<Object, ObjectMember> startArray() {
      currPath.push(VmValueConverter.WILDCARD_ELEMENT);
      return EconomicMaps.create();
    }

    @Override
    public void endArray(@Nullable EconomicMap<Object, ObjectMember> members) {
      assert members != null;
      value =
          new VmListing(
              VmUtils.createEmptyMaterializedFrame(),
              BaseModule.getListingClass().getPrototype(),
              members,

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Quote oversized numbers as strings in the JSON source and convert manually if needed
  2. Fix the generator producing the JSON to emit numbers within Long/Double range
  3. Pre-transform the JSON (e.g. with a script) to normalize extreme numeric literals

Example fix

// before (input.json)
{"id": 123456789012345678901234567890}
// after
{"id": "123456789012345678901234567890"}
Defensive patterns

Strategy: try-catch

Validate before calling

function isSafeNumber(n) { return Number.isFinite(n) && Math.abs(n) <= Number.MAX_VALUE && Number.isInteger(n) ? n <= Number.MAX_SAFE_INTEGER || typeof n === 'number' : !Number.isFinite(n); }

Type guard

const isInRangeNumber = (s) => { const n = Number(s); return Number.isFinite(n); };

Try / catch

try { const v = Json.parse(text); } catch (e) { if (e instanceof ParseException && /Cannot parse .* as number/.test(e.message)) { /* normalize the numeric literal */ } else throw e }

Prevention

When it happens

Trigger: Parsing JSON (via Pkl's JSON parser used for `fromJson`/import of `.json`) containing a numeric literal too large or too small for Long/Double, e.g. `1e999` or a 20+ digit integer.

Common situations: Importing machine-generated JSON with huge IDs (e.g. 64-bit-plus snowflake IDs serialized as numbers, or `Infinity`-like `1e999` exponents) into Pkl.

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 apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/624bff6bccaeff57. Report an issue: GitHub.