Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialization of Blobs using serde confuses types #45

Open
cevans-uk opened this issue Dec 27, 2019 · 2 comments
Open

Deserialization of Blobs using serde confuses types #45

cevans-uk opened this issue Dec 27, 2019 · 2 comments

Comments

@cevans-uk
Copy link

Consider:

use nbt::{Blob, Value}; 

fn main() {
    let mut blob = Blob::new();
    blob.insert("test", Value::Int(123)).unwrap();

    let mut bytes = Vec::new();
    nbt::to_writer(&mut bytes, &blob, None).unwrap();
    println!("{:?}", bytes);

    let de_blob: Blob = nbt::from_reader(&bytes[..]).unwrap();
    println!("{:?}", de_blob);
}

Output:

[10, 0, 0, 3, 0, 4, 116, 101, 115, 116, 0, 0, 0, 123, 0]
Blob { title: "", content: {"test": Byte(123)} }

Serialization seems to work -- that 3 in the 4th position is TAG_Int as you'd expect, but it gets deserialized into a Value::Byte (using larger values will give Shorts and then Ints).

Using Blob::from_reader works, and gives Blob { title: "", content: {"test": Int(123)} }.

I think this is caused by using serde(untagged) for the Value enum, but I really don't understand serde well enough to know how to fix this.

@atheriel atheriel added the bug label Dec 29, 2019
@atheriel
Copy link
Collaborator

I'm not sure why this is the case, either. There is a test for this exact behaviour that seems to pass.

@cevans-uk
Copy link
Author

The test passes since none of the values used can be represented in one of the smaller (or rather, earlier in enum Value) types. This is sufficient for a failure:

--- a/src/tests.rs
+++ b/src/tests.rs
@@ -293,7 +293,7 @@ fn serde_blob() {
 
     let mut nbt = Blob::new();
     nbt.insert("name", "Herobrine").unwrap();
-    nbt.insert("health", 100i8).unwrap();
+    nbt.insert("health", 100i16).unwrap();
     nbt.insert("food", 20.0f32).unwrap();
     nbt.insert("emeralds", 12345i16).unwrap();
     nbt.insert("timestamp", 1424778774i32).unwrap();
@@ -306,10 +306,10 @@ fn serde_blob() {
                 0x6e, 0x61, 0x6d, 0x65,
                 0x00, 0x09,
                 0x48, 0x65, 0x72, 0x6f, 0x62, 0x72, 0x69, 0x6e, 0x65,
-            0x01,
+            0x02,
                 0x00, 0x06,
                 0x68, 0x65, 0x61, 0x6c, 0x74, 0x68,
-                0x64,
+                0x00, 0x64,
             0x05,
                 0x00, 0x04,
                 0x66, 0x6f, 0x6f, 0x64,
---- tests::serde_blob stdout ----
thread 'tests::serde_blob' panicked at 'assertion failed: `(left == right)`
  left: `Blob { title: "", content: {"food": Float(20.0), "emeralds": Short(12345), "name": String("Herobrine"), "timestamp": Int(1424778774), "health": Byte(100)} }`,
 right: `Blob { title: "", content: {"name": String("Herobrine"), "health": Short(100), "emeralds": Short(12345), "timestamp": Int(1424778774), "food": Float(20.0)} }`', src/tests.rs:332:5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants