serde_rusqlite
Documentation
Usage
Add this to your Cargo.toml:
[dependencies]
serde_rusqlite = "0.21"
Serde Rusqlite
This crate provides convenience functions to bridge serde and rusqlite. With their help
you can "deserialize" rusqlite Row's into serde Deserialize types and "serialize" types
implementing Serialize into bound query arguments (positional or named) that rusqlite expects.
Serialization of named bound arguments is only supported from structs and maps because other
serde types lack column name information. Likewise, serialization of positional bound arguments
is only supported from tuples, sequences and primitive non-iterable types. In the latter case
the result will be single-element vector. Each serialized field or element must implement
rusqlite::types::ToSql.
For deserialization you can use two families of functions: from_*() and from_*_with_columns().
The most used one is the former. The latter allows you to specify column names for types that need
them, but don't supply them. This includes different Map types like HashMap. Specifying columns
for deserialization into e.g. struct doesn't have any effect as the field list of the struct itself
will be used in any case.
SQLite only supports 5 types: NULL (None), INTEGER (i64), REAL (f64), TEXT (String)
and BLOB (Vec<u8>). Corresponding rust types are inside brackets.
Some types employ non-trivial handling, these are described below:
-
Serialization of
u64will fail if it can't be represented byi64due to sqlite limitations. -
Simple
enums will be serialized as strings so:enum Gender { M, F, }will have two possible
TEXToptions in the database "M" and "F". Deserialization intoenumfromTEXTis also supported. -
bools are serialized asINTEGERs 0 or 1, can be deserialized fromINTEGERandREALwhere 0 and 0.0 arefalse, anything else istrue. -
f64andf32values ofNaNare serialized asNULLs. When deserializing such valueOption<f64>will have value ofNoneandf64will have value ofNaN. The same applies tof32. -
Bytes,ByteBuffromserde_bytesare supported as optimized way of handlingBLOBs. -
unitserializes toNULL. -
Only
sequences ofu8are serialized and deserialized,BLOBdatabase type is used. It's more optimal though to useBytesandByteBuffromserde_bytesfor such fields. -
unit_structserializes tostructname asTEXT, when deserializing the check is made to ensure thatstructname coincides with the string in the database.
Examples
extern crate rusqlite;
extern crate serde_derive;
extern crate serde_rusqlite;
use *;
use NO_PARAMS;
License: LGPL-3.0