{"record":{"id":"60f2cd0c761c4f07","repo":"apple/pkl","slug":"values-of-type-bytes-cannot-be-rendered-as-json","errorCode":null,"errorMessage":"Values of type `Bytes` cannot be rendered as JSON. Value: %s","messagePattern":"Values of type `Bytes` cannot be rendered as JSON\\. Value: (.+?)","errorType":"exception","errorClass":"RendererException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/JsonRenderer.java","lineNumber":107,"sourceCode":"        throw new UncheckedIOException(e);\n      }\n    }\n\n    @Override\n    public void visitDuration(Duration value) {\n      throw new RendererException(\n          String.format(\"Values of type `Duration` cannot be rendered as JSON. Value: %s\", value));\n    }\n\n    @Override\n    public void visitDataSize(DataSize value) {\n      throw new RendererException(\n          String.format(\"Values of type `DataSize` cannot be rendered as JSON. Value: %s\", value));\n    }\n\n    @Override\n    public void visitBytes(byte[] value) {\n      throw new RendererException(\n          String.format(\n              \"Values of type `Bytes` cannot be rendered as JSON. Value: %s\", (Object) value));\n    }\n\n    // Pair coming from the runtime can never admit null (in-language null is represented as PNull)\n    @SuppressWarnings(\"DataFlowIssue\")\n    @Override\n    public void visitPair(Pair<?, ?> value) {\n      try {\n        writer.beginArray();\n        visit(value.getFirst());\n        visit(value.getSecond());\n        writer.endArray();\n      } catch (IOException e) {\n        throw new UncheckedIOException(e);\n      }\n    }\n","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/JsonRenderer.java#L89-L125","documentation":"JsonRenderer throws this RendererException when asked to render Pkl Bytes (a byte array) as JSON, because raw binary has no standard JSON encoding. The value's byte[] is included in the message via its string form.","triggerScenarios":"Rendering to JSON any property that evaluates to Bytes (e.g. `data = new Bytes { ... }` or base64 output of binary resources) via visitBytes.","commonSituations":"Modules embedding binary blobs — keys, certificates, hashes, or file contents — rendered to JSON for consumption by other tools.","solutions":["Encode the Bytes as base64 (or hex) text before rendering","Use prop type String/Listing<String> with an explicit encoding instead of Bytes","Exclude binary fields from the JSON output"],"exampleFix":"// before\nprop key: Bytes = ...\n// after\nprop keyBase64: String = key.base64Encoded","handlingStrategy":"type-guard","validationCode":"if (value instanceof byte[] || \"Bytes\".equals(value.getClass().getSimpleName())) {\n  throw new IllegalArgumentException(\"encode Bytes (e.g. base64) before JSON rendering\");\n}","typeGuard":"boolean isJsonRenderable(Object v) {\n  return !(v instanceof Duration || v instanceof DataSize || v instanceof byte[]);\n}","tryCatchPattern":"try {\n  renderer.render(value);\n} catch (RendererException e) {\n  LOG.error(\"JSON rendering unsupported: {}\", e.getMessage());\n}","preventionTips":["Base64-encode binary content into String fields before JSON rendering","Avoid Bytes-typed properties in modules designated as JSON APIs","Validate the value tree for unsupported types (Duration, DataSize, Bytes) prior to rendering"],"tags":["java","pkl","json","renderer"],"backgroundTag":"json-serialization-failed","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}