Loading Rust to MessagePack Converter...

How to Convert a Rust Value to MessagePack

Writing a test for a service that speaks MessagePack usually means wiring up a small program just to see what the bytes look like. Paste the Rust value here instead and you get the encoding straight away, field names, integer widths and all.

Step 1

Paste a Rust value

Paste a struct literal such as Reading { sensor_id: 7, ok: true }, a vec![...] of them, or a let statement. You can paste the struct and enum definitions above the value too, or upload a .rs file, or click Sample to see a full example.

Reading {
    sensor_id: 8901240544102066246,
    zip: "00501".to_string(),
    celsius: 14.50,
    ok: true,
}
Step 2

Read the encoded bytes

The MessagePack bytes appear on the right a moment after you stop typing, as Base64 or as hex. A blue note explains anything worth knowing, such as a float written as f64 because no type was given. If the input is not plain Rust data, the error names the line and column.

The example above becomes 51 bytes. In Base64:

hKlzZW5zb3JfaWTPe4ePHTOCKEajemlwpTAwNTAxp2NlbHNpdXPLQC0AAAAAAACib2vD

And in hex, where you can spot the parts: 84 is a map with four entries, cf starts the uint64 ID, a5 3030353031 is the string "00501", and cb is the 64-bit float for 14.50.

84a973656e736f725f6964cf7b878f1d33822846a37a6970a53030353031a763656c73697573cb402d000000000000a26f6bc3
Step 3

Copy or download

Copy puts the Base64 or hex text on your clipboard, whichever is showing. Download saves the raw bytes as data.msgpack, ready to feed to a MessagePack reader or a test.

What is MessagePack?

MessagePack is a binary format with the same building blocks as JSON (maps, arrays, strings, numbers, booleans and nil) plus raw binary and real 64-bit integers. In Rust it is most often written with rmp-serde on top of Serde, and this page follows its rules.

To look inside the bytes again, paste them into MessagePack to JSON or check them with the MessagePack Validator. If your data is JSON rather than Rust, JSON to MessagePack does the same job.

Frequently Asked Questions

Are these the same bytes rmp-serde writes?

For the values this page understands, yes: we compared the output with rmp_serde::to_vec_named on the same data, byte for byte. Structs become maps keyed by field name, which is what to_vec_named does. Plain rmp_serde::to_vec writes structs as arrays of values without the names, so its bytes are shorter and will not match this page.

How are Rust types written?

Integers use the smallest MessagePack integer that holds them, the same choice rmp makes, so 200 takes two bytes and 8901240544102066246 is a uint64. f64 becomes a float 64, f32 a float 32, String and char a string, bool a boolean, and None or () becomes nil. Vec, arrays, tuples and tuple structs become arrays, a newtype like Id(9) is just 9, and maps such as BTreeMap become MessagePack maps. A unit enum value like Status::Active is the string "Active", and an enum with data like Shape::Circle(1.5) is a one-entry map {"Circle": 1.5}. i128 and u128 are written as 16 bytes of binary, as rmp-serde does.

Why paste the struct definitions too?

The value alone does not say whether 2.5 is an f32 or an f64, and that changes the bytes. With the definitions pasted above the value, fields are written in the order they are declared, f32 and u128 fields get the right encoding, and serde attributes like #[serde(rename = "...")], rename_all, skip and skip_serializing_if = "Option::is_none" are applied. Without them, decimals default to f64; add a suffix such as 2.5f32 if you need f32.

Will long IDs and decimals stay exact?

Integers are read with exact big-integer arithmetic, so 8901240544102066246 and -9223372036854775808 keep every digit in the bytes, and a literal that does not fit its type, like 300u8, is reported instead of being wrapped. A number in a String such as "00501" stays a string. Decimals are stored as binary floats, as in Rust, so 14.50 is the same float as 14.5; keep a value as a String if its exact spelling matters.

What can it not convert?

Anything that needs your program to run: variables, function calls other than the usual constructors (String::from, Box::new, Vec::new, HashMap::from and friends), most macros apart from vec!, and method calls other than .to_string(), .into(), .clone() and similar. Serde attributes such as flatten, tag or untagged are not applied, and the page tells you when it sees one. Byte strings like b"hi" are written as a list of numbers, which is what serde does for &[u8] unless you use serde_bytes.

Is my code uploaded anywhere?

No. The conversion runs in your browser, the code is not sent to our server, and nothing is kept: a reload starts with an empty editor. Uploaded files are read in the browser as well, up to 10 MB.