#serialization #data #byte #stupid #recursion #deserializing #structures

lize

A stupid way of serializing and deserializing really small data into bytes. Supports recursive data structures.

2 releases

0.1.1 Mar 1, 2025
0.1.0 Jan 28, 2025

#1042 in Data structures

Download history 88/week @ 2025-01-26 24/week @ 2025-02-02 77/week @ 2025-02-23 76/week @ 2025-03-02

155 downloads per month

MIT license

21KB
484 lines

lize

A stupid way of serializing data into a slice. Designed for really small data.

use lize::Value;

// You can create hashmaps like so:
let value = Value::HashMap(vec![
    (Value::Slice(b"hello"), Value::Slice(b"world")),
    (Value::Slice(b"money"), Value::I64(6969694200)),
]);

// ..then serialize it into a SmallVec (recommended)
let mut buffer = lize::SmallVec::<[u8; lize::STACK_N]>::new();
value.serialize_into(&mut buffer)?;

// Alternatively, you can serialize it into a Vec.
// That'd be more convenient.
// let buffer = value.serialize()?;

// Then, we can take a look at our deserialized data.
let deserialized = Value::deserialize_from(&buffer)?;
println!("{deserialized:?}");

let mut buffer = SmallVec::<[u8; lize::STACK_N]>::new();
value.serialize_into(&mut buffer)?;

Value

There are some other cool usages other than just hashmaps.

let value = Value::Vector(vec![
    Value::Bool(true),
    Value::Slice(b"hello"),

    Value::F64(std::f64::consts::PI),
    Value::I64(1234567890123456789),
    Value::I32(123456789),

    Value::Optional(Some(Box::new(Value::Slice(b"world")))),
    Value::Optional(None),

    Value::U8(1_u8),
    Value::SmallU8(123_u8) // Must be <= 235. Occupies a single byte.
]);

Of course, if you would, you can use Value::from(...) instead of doing that manually. Saves time!

let a = 123_i64;
let v = Value::from(a);

assert!(matches!(v, Value::I64(_)));

(de)serializing directly

You can use serialize(...) and deserialize(...) to have the Bincode-like interface.

let a = 123_i64;
let ser = serialize(a)?;

let b: i64 = deserialize(&ser)?;

assert_eq!(a, b);

(c) 2025 AWeirdDev

Dependencies

~200KB