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 {uri-stdlib-pklbinaryModule}[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.
All Pkl primitives turn into their respective MessagePack primitive.
| Pkl Type | MessagePack format |
|---|---|
|
Note
|
Pkl integers are encoded into the smallest int type that the number will fit into.
For example, value 8 gets encoded as MessagePack int8 format.
|
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 |
||||||
|
{uri-messagepack-float}[float64] |
Duration value |
Duration unit ( |
||||
|
Value (float64) |
DataSize unit ( |
|||||
|
|
First value |
|
Second value |
|||
|
Start |
End |
Step |
||||
|
Regex string representation |
||||||
|
Module URI |
||||||
|
Module URI |
||||||
|
|||||||
|
Binary contents |
||||||
|
|
Root value |
Array of property access values |
||||
|
str (when a property access) or nil (when a subscript access) |
Property name |
|
Key value |
|||
|
Note
|
Type names have specific encoding rules:
|
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 |
|