Skip to main content

Module value

Module value 

Source
Expand description

Untyped value helpers for MessagePack.

This module provides a dynamic representation of MessagePack data and utility adapters for extension types.

§Examples

Serialize a struct into Value and then deserialize it back. Both Value and ValueRef implement serde::Deserializer, so any T: serde::Deserialize can be decoded from them.

use serde::{Deserialize, Serialize};
use messagepack_serde::value::{to_value, Value};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct User<'a> {
    id: u64,
    name: &'a str,
    active: bool,
}

let original = User { id: 42, name: "alice", active: true };

// Serialize Rust value to an owned MessagePack "Value" tree
let v: Value = to_value(&original).unwrap();

// Deserialize back from &Value
let decoded = User::deserialize(&v).unwrap();
assert_eq!(decoded, original);

Borrowed decoding from ValueRef. This avoids copying strings and byte slices when possible.

use serde::Deserialize;
use messagepack_serde::value::ValueRef;

// Borrowed primitives without allocation
let s = <&str>::deserialize(&ValueRef::String("hello")).unwrap();
assert_eq!(s, "hello");

// Decode a tuple from a borrowed array
let v = ValueRef::Array(vec![
    ValueRef::from(1u64),
    ValueRef::from("hello"),
    ValueRef::from(false),
]);
let tup = <(u64, &str, bool)>::deserialize(&v).unwrap();
assert_eq!(tup, (1, "hello", false));

Enums§

Number
Represents any number, it could be int or float.
Valuealloc
Owned representation of any MessagePack value.
ValueRefalloc
Represents any messagepack value.

Functions§

to_valuealloc
Convert T to crate::value::Value