3Hren / msgpack-rust

MessagePack implementation for Rust / msgpack.org[Rust]
MIT License
1.17k stars 130 forks source link

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

Closed StuartHadfield closed 2 years ago

StuartHadfield commented 2 years ago

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?

StuartHadfield commented 2 years ago

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)

StuartHadfield commented 2 years ago

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