Expand description
§Serde KLV
KLV(Key-Length-Value) is a data encoding standard, often used to embed information in video feeds.
This is provide KLV serialier and deserializer for struct.
Examples
use serde_klv::{from_bytes, to_bytes};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
// Set Universal Key string or byte-literal
#[serde(rename = "TESTDATA00000000")]
// #[serde(rename = "\x06\x0e\x2b\x34\x02\x0b\x01\x01\x0e\x01\x03\x01\x01\x00\x00\x00")]
struct TestStruct<'a> {
// rename to u8 range number
#[serde(rename = "10")]
u8: u8,
#[serde(rename = "11")]
u64: u64,
// can use Option
#[serde(rename = "120", skip_serializing_if = "Option::is_none")]
none_skip_some: Option<u16>,
#[serde(rename = "121", skip_serializing_if = "Option::is_none")]
none_skip_none: Option<u16>,
#[serde(rename = "60")]
str: &'a str,
#[serde(rename = "70")]
child: TestChild,
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct TestChild {
#[serde(rename = "10")]
x: i8,
#[serde(rename = "11")]
y: f32,
}
let t = TestStruct {
u8: 127,
u64: u32::MAX as u64 + 1,
none_skip_some: Some(2016),
none_skip_none: None,
str: "this is string",
child: TestChild{x: -64, y: 1.23}
};
let buf = to_bytes(&t).unwrap();
let x = from_bytes::<TestStruct>(&buf).unwrap();
assert_eq!(&t, &x);
// with checksum
use serde_klv::{from_bytes_with_checksum, to_bytes_with_checksum, WrappedCRC};
let buf = to_bytes_with_checksum(&t, WrappedCRC::default()).unwrap();
let x: TestStruct = from_bytes_with_checksum(&buf, WrappedCRC::default()).unwrap();
assert_eq!(&t, &x);Modules§
Structs§
- KLVMap
- Parse unknown KLVdata
- KLVRaw
- Single KLV Record
- WrappedCRC
- use crc crate
Crc<u16>
Traits§
Functions§
- from_
bytes - Deserialize from bytes Checksumのフィールドは無視される
- from_
bytes_ with_ checksum - Deserialize from bytes with checksum デシリアライズ前にChecksumでデータの破損がないか確認する
- parse_
length - parse length rule by BER
- to_
bytes - Serialize to bytes
- to_
bytes_ with_ checksum - Serialize to bytes append CRC at last field バッファの最後に16bit長のChecksumを追加する