I'm trying to port a JavaScript library which uses msgpack for encoding JavaScript objects to Rust. I found a Rust library for msgpack encoding/decoding, but I don't get what is the equivalent input format in Rust.
This JavaScript code for encoding the object {"a": 5, "b": 6}
gives the output 82 a1 61 03 a1 62 05
:
const msgpack = require("msgpack-lite");
msgpack.encode(obj);
I tried representing the object as a Rust struct and encoding it using rmp-serde library
use rmp_serde::{Deserializer, Serializer};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Test {
a: u32,
b: u32,
}
fn main() {
let mut buf = Vec::new();
let val = Test { a: 3, b: 5 };
val.serialize(&mut Serializer::new(&mut buf)).unwrap();
println!("{:?}", buf);
}
I get the output [146, 3, 5]
. How do I represent JSON input in Rust?