MessagePack is a binary serialization format. Similar to JSON but lighter and faster. You can read the specification here.
messagepack.gd contains an implementation of the spec written in pure GDScript for Godot 4.
Copy the messagepack.gd file into your project to start using it with the Messagepack class.
The Messagepack class contains two main static methods for serialization: encode and decode:
Converts a Godot Variant into MessagePack binary format.
Returns:
A dictionary with:
status (bool):trueif successful,falseon errorvalue (PackedByteArray): Encoded binary data
Converts MessagePack binary data back into Godot Variants.
Returns:
A dictionary with:
status (bool):trueif successful,falseon errorvalue (Variant): Decoded data
Both methods are static so you can simply call them with Messagepack.encode() and Messagepack.decode().
# Encoding
var to_encode = {
"hp": 200,
"speed": 8.5,
"is_invincible": false,
"actions": ["jump","dash","attack"]
}
var encoded = Messagepack.encode(to_encode)
if encoded.status != null: # Check for status to verify encoding issues
printerr(encoded.status)
else:
print(encoded.value)
# Decoding
var to_decode = encoded.value
var decoded = Messagepack.decode(to_decode)
if decoded.status != null: # Check for status to verify decoding issues
printerr(decoded.status)
else:
print(decoded.value)This implementation is based on the work by xtpor.
The project is distributed under the MIT license.