#nbt #minecraft #serialization #serde

serde_nbt

A Serde library for Minecraft's Named Binary Tag (NBT) format

2 releases

0.1.1 Sep 22, 2022
0.1.0 Sep 3, 2022

#1927 in Encoding

Download history 18/week @ 2024-02-19 21/week @ 2024-02-26 4/week @ 2024-03-04 41/week @ 2024-03-11 26/week @ 2024-04-01

68 downloads per month
Used in valence

MIT license

75KB
2K SLoC

NOTE: This crate has been superseded by valence_nbt. If you have a use for this crate name, contact the Valence developers.

A Serde library for serializing and deserializing Minecraft's Named Binary Tag (NBT) format.

For more information, see the documentation here.


lib.rs:

A serde library for the serialization and deserialization of Minecraft's Named Binary Tag (NBT) format.

Examples

Write an NBT compound to a byte buffer.

use serde::Serialize;
use serde_nbt::binary::to_writer;

#[derive(Serialize)]
struct Example {
    boolean: bool,
    string: String,
    list_of_float: Vec<f32>,
    #[serde(with = "serde_nbt::int_array")]
    int_array: Vec<i32>,
}

let example = Example {
    boolean: true,
    string: "abc123".to_owned(),
    list_of_float: vec![3.1415, 2.7182, 1.4142],
    int_array: vec![7, 8, 9],
};

let mut buf = Vec::new();
to_writer(&mut buf, &example).unwrap();

Sometimes the structure of the NBT data is not known ahead of time. For this, you can use Value.

use serde_nbt::binary::from_reader;
use serde_nbt::{Compound, Value};

let some_bytes = [10, 0, 0, 3, 0, 3, 105, 110, 116, 0, 0, 222, 173, 0];
let reader = &mut some_bytes.as_slice();

let value: Value = from_reader(reader).unwrap();

let expected_value = Value::Compound(Compound::from_iter([(
    "int".to_owned(),
    Value::Int(0xdead),
)]));

assert_eq!(value, expected_value);

Dependencies

~1.2–1.6MB
~28K SLoC