3Hren/msgpack-rust

rmp_serde deserialize msgpack file into json file produces... Garbage?

StuartHadfield opened this issue · 2 comments

Hello! Looking for some help with reading files in Rust. Super new to the language.

I have a msgpack file full of data, and I'm trying to convert it into json. I've looked at the serde / rmp_serde crates and come up with the following:

use std::io;
use std::fs::File;
use std::io::{BufReader, BufWriter};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let file_path = "./src/data.msgpack";
    let reader = BufReader::new(File::open(file_path).unwrap());
    // let writer = BufWriter::new(File::create("./src/results.json").unwrap());

    let mut deserializer = rmp_serde::Deserializer::from_read(reader);

    let mut serializer = serde_json::Serializer::new(io::stdout());

    serde_transcode::transcode(&mut deserializer, &mut serializer).unwrap();
    Ok(())
}

My understanding is that I've opened an I/O stream to the msgpack file, deserialised it from this io stream (https://docs.rs/rmp-serde/latest/rmp_serde/decode/fn.from_read.html) and serialized it into stdout with the serde_json serialiser.

The kicker is.... My output is just... "-17" 😳 What am I doing wrong?

FWIW I have deserialised the file with Python and all is well, it produces the results I'd expect, so I'm pretty sure the data file is not the issue here.

import msgpack
fh = open('src/data.msgpack', 'rb')
fh.seek(0)
unp = msgpack.Unpacker(fh, raw=False)
for unpacker in unp:
    print(unpacker)

Returns lots of data (which regrettably I cannot share)

Hmmm, okay, using a second file and things actually read out fine. Ignore me for now, apologies