serde-sqlx
Allows deserializing Postgres rows into Rust types using serde
. Work in progress.
Implementation Status
Completed ✅
-
Simple Primitives:
- Strings: TEXT, VARCHAR, BPCHAR
- Booleans
- Integers: i16, i32, i64 (INT2, INT4, INT8/BIGINT)
- Floating point: f32 (REAL), f64 (DOUBLE PRECISION)
- Support for special float values (NaN, Infinity)
-
Structs and Tuples:
- Deserialize into named structs with primitive fields
- Support for tuple structs and anonymous tuples
- Deep nesting of structs using Serde's flattening
-
Optional Values:
- NULL values into Option
-
JSON and JSONB:
- Directly deserialize JSON data into Rust structures
-
Newtypes:
- Support for newtype pattern (e.g.,
struct UserId(i32)
)
- Support for newtype pattern (e.g.,
-
PostgreSQL Arrays:
- Convert Postgres arrays into Rust vectors
- Support for arrays of primitive types and nullable types
Planned/untested 📝
- Enums:
- Support for Rust enums with Postgres enums or discriminated JSON
- Timestamps and Dates:
- Testing for chrono and time types
- UUID:
- Testing for uuid crate types
Usage
Add serde-sqlx
to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["derive"] }
= { = "0.6", = ["postgres", "runtime-tokio-native-tls"] }
Basic example:
use Deserialize;
async