Skip to main content

Crate serde_kv

Crate serde_kv 

Source
Expand description

A serde data format for flat key/value lines.

The format is a space-separated list of key=value pairs:

foo=bar baz=42 cool=true message="hello world"

There is no type inference at the format level: a raw token like 42 is interpreted according to the target field’s type. A String field receives "42", while a u64 field receives 42. Quotes are only a transport concern (needed when a value contains spaces or special characters), so foo=42 and foo="42" decode to the identical value.

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Record {
    foo: String,
    baz: u64,
    cool: bool,
    message: String,
}

let line = r#"foo=bar baz=42 cool=true message="hello world""#;
let record: Record = serde_kv::from_str(line).unwrap();
assert_eq!(record.baz, 42);
assert_eq!(serde_kv::to_string(&record).unwrap(), line);

Scope: flat scalars (strings, integers, floats, bool, char), Option<T> (a missing key deserializes to None; None is skipped on serialization), and tolerant handling of unknown keys. Nested structs and sequences are not supported.

Enums§

Error
The error type for serializing and deserializing the key/value format.

Functions§

from_str
Deserialize a value of type T from a key/value line.
to_string
Serialize a value into a key/value line.

Type Aliases§

Result
A specialized Result for this crate.