pub struct BinaryDeserializer;
Expand description

A structure to deserialize binary data into Rust values.

By default, if a token is unable to be resolved then it will be ignored by the default. Construct a custom instance through the builder method to tweak this behavior.

The example below demonstrates how to deserialize data

use jomini::{BinaryDeserializer, Encoding, JominiDeserialize, Windows1252Encoding};
use serde::Deserialize;
use std::{borrow::Cow, collections::HashMap};

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct StructA {
  field1: String,
  field2: i32
}

#[derive(Debug, Default)]
pub struct BinaryTestFlavor;

impl jomini::binary::BinaryFlavor for BinaryTestFlavor {
    fn visit_f32(&self, data: [u8; 4]) -> f32 {
        f32::from_le_bytes(data)
    }

    fn visit_f64(&self, data: [u8; 8]) -> f64 {
        f64::from_le_bytes(data)
    }
}

impl Encoding for BinaryTestFlavor {
    fn decode<'a>(&self, data: &'a [u8]) -> Cow<'a, str> {
        Windows1252Encoding::decode(data)
    }
}

let data = [
   0x82, 0x2d, 0x01, 0x00, 0x0f, 0x00, 0x03, 0x00, 0x45, 0x4e, 0x47,
   0x83, 0x2d, 0x01, 0x00, 0x0c, 0x00, 0x59, 0x00, 0x00, 0x00,
];

let mut map = HashMap::new();
map.insert(0x2d82, String::from("field1"));
map.insert(0x2d83, String::from("field2"));

let a: StructA = BinaryDeserializer::builder_flavor(BinaryTestFlavor)
    .from_slice(&data[..], &map)?;
assert_eq!(a, StructA {
  field1: "ENG".to_string(),
  field2: 89,
});

It is not recommended to use the flatten serde attribute, as then it would be difficult to reuse the same struct for binary and text deserialization. See TextDeserializer for more info

Implementations§

A customized builder for a certain flavor of binary data

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.