sval
A prototype, lightweight, no-std, object-safe, serialization-only API for structured values with serde
support.
Producers of structured values use the value
module. Consumers of structured values use the stream
module. sval
offers a json-like data model, which is more limiting than serde
's, but capable enough to represent Rust datastructures in one form or another.
This library is designed to plug a no-std-object-safe sized hole in Rust's current serialization ecosystem. The driving use-case is structured logging, where individual events are typically small, and there's no complete schema that can tie values in any one event to values in another.
sval_json
and sval_derive
are mostly pilfered from dtolnay's excellent miniserde
project.
rustc
This library requires Rust 1.31.0
, which is currently in beta
.
Add sval
to your crate dependencies:
toml
[dependencies.sval]
version = "0.0.4"
Simple struct-like datastructures can derive sval::Value
:
toml
[dependencies.sval]
features = ["derive"]
```rust
extern crate sval;
struct MyData { id: u64, name: String, } ```
Other datastructures can implement sval::Value
manually:
```rust use sval::value::{self, Value};
struct MyId(u64);
impl Value for MyId { fn stream(&self, stream: &mut value::Stream) -> Result<(), value::Error> { stream.u64(self.0) } } ```
The sval_json
crate can format any sval::Value
as json:
toml
[dependencies.sval_json]
version = "0.0.4"
rust
let my_json = sval_json::to_string(my_data)?;
serde
sval
has out-of-the-box serde
integration between sval::Value
s and serde::Serialize
s. Add the serde
feature to sval
to enable it:
toml
[dependencies.sval]
features = ["serde"]
Then convert between serde
and sval
:
rust
let my_serialize = sval::serde::to_serialize(my_data);