apple/pkl · error · RendererException

Values of type `DataSize` cannot be rendered as JSON. Value:

Error message

Values of type `DataSize` cannot be rendered as JSON. Value: %s

What it means

JsonRenderer throws this RendererException when rendering a Pkl DataSize value as JSON, since JSON has no native datasize representation. Like the Duration case, it refuses instead of guessing an encoding.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/JsonRenderer.java:101

    @Override
    public void visitBoolean(Boolean value) {
      try {
        writer.value(value);
      } catch (IOException e) {
        throw new UncheckedIOException(e);
      }
    }

    @Override
    public void visitDuration(Duration value) {
      throw new RendererException(
          String.format("Values of type `Duration` cannot be rendered as JSON. Value: %s", value));
    }

    @Override
    public void visitDataSize(DataSize value) {
      throw new RendererException(
          String.format("Values of type `DataSize` cannot be rendered as JSON. Value: %s", value));
    }

    @Override
    public void visitBytes(byte[] value) {
      throw new RendererException(
          String.format(
              "Values of type `Bytes` cannot be rendered as JSON. Value: %s", (Object) value));
    }

    // Pair coming from the runtime can never admit null (in-language null is represented as PNull)
    @SuppressWarnings("DataFlowIssue")
    @Override
    public void visitPair(Pair<?, ?> value) {
      try {
        writer.beginArray();
        visit(value.getFirst());
        visit(value.getSecond());

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Convert the DataSize to a scalar first (e.g. value.value in bytes) or expose a String/Number property instead
  2. Exclude datasize fields from the JSON output
  3. Pick an output format that supports sizes, or a custom renderer

Example fix

// before
prop maxMem: DataSize = 2.gb
// after
prop maxMemBytes: Number = 2.gb.value // 2000000000
Defensive patterns

Strategy: type-guard

Validate before calling

if (value instanceof DataSize) {
  throw new IllegalArgumentException("convert DataSize to Number before JSON rendering");
}

Type guard

boolean isJsonRenderable(Object v) {
  return !(v instanceof Duration || v instanceof DataSize || v instanceof byte[]);
}

Try / catch

try {
  renderer.render(value);
} catch (RendererException e) {
  LOG.error("JSON rendering unsupported: {}", e.getMessage());
}

Prevention

When it happens

Trigger: Rendering to JSON a module or property containing a DataSize value (e.g. `size = 5.mb`) via visitDataSize.

Common situations: Config modules declaring memory/disk/bandwidth limits rendered to JSON for APIs or tooling.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/6f5ff5677369f374. Report an issue: GitHub.