pkl-binary Encoding
Pkl values can be encoded into a binary format called "pkl-binary". This format is a lossless serialization of the underlying Pkl values.
Pkl code can be rendered into this format using the pkl:pklbinary standard library module.
Alternatively, many language bindings also provide methods to evaluate into pkl-binary, such as the evaluateExpressionPklBinary method in org.pkl.core.Evaluator.
The binary format is uses MessagePack encoding.
Non-primitives
All non-primitive values are encoded as MessagePack arrays. The first slot of the array designates the value’s type. The remaining slots have fixed meanings depending on the type. Additional slots may be added to types in future Pkl releases. Decoders must be designed to defensively discard values beyond the number of known slots for a type or provide meaningful error messages.
The array’s length is the number of slots that are filled. For example, List is encoded as an MessagePack array with two elements.
| Pkl type | Slot 1 | Slot 2 | Slot 3 | Slot 4 | |||
|---|---|---|---|---|---|---|---|
code |
type |
description |
type |
description |
type |
description |
|
|
Enclosing module URI |
Array of object members |
|||||
|
Map of |
||||||
|
Map of |
||||||
|
Array of |
||||||
|
Array of |
||||||
|
Array of |
||||||
|
Duration value |
Duration unit ( |
|||||
|
Value (float64) |
DataSize unit ( |
|||||
|
|
First value |
|
Second value |
|||
|
Start |
End |
Step |
||||
|
Regex string representation |
||||||
|
Module URI |
||||||
|
Module URI |
||||||
|
|||||||
|
Binary contents |
||||||
|
Type names have specific encoding rules:
|
Object Members
Like non-primitive values, object members are encoded as MessagePack arrays, where the first slot designates the value’s type.
| Member type | Slot 1 | Slot 2 | Slot 3 | ||
|---|---|---|---|---|---|
code |
type |
description |
type |
description |
|
Property |
|
key |
|
property value |
|
Entry |
|
|
entry key |
|
entry value |
Element |
|
index |
|
element value |
|