https://github.com/amilajack/serde_postgres
Easily Deserialize Postgres rows.
https://github.com/amilajack/serde_postgres
Last synced: 4 months ago
JSON representation
Easily Deserialize Postgres rows.
- Host: GitHub
- URL: https://github.com/amilajack/serde_postgres
- Owner: amilajack
- License: apache-2.0
- Created: 2018-11-22T01:24:18.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-11-22T01:32:20.000Z (over 6 years ago)
- Last Synced: 2024-10-28T05:33:32.775Z (6 months ago)
- Language: Rust
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 1
- Forks: 23
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# Serde Postgres
[](https://travis-ci.org/1aim/serde_postgres)
[](https://crates.io/crates/serde_postgres)
[](https://github.com/Aaronepower/tokei)
[](https://docs.rs/serde_postgres/)Easily deserialize rows from [`postgres`](//docs.rs/postgres) into
arbitrary structs. (Only deserialization is supported).```rust
extern crate serde;
extern crate serde_derive;
extern crate serde_postgres;
extern crate postgres;use std::error::Error;
use serde_derive::Deserialize;
use postgres::{Connection, TlsMode};#[derive(Clone, Debug, Deserialize)]
struct Person {
name: String,
age: i32,
}fn main() -> Result<(), Box> {
let connection = Connection::connect("postgres://postgres@localhost:5432", TlsMode::None)?;connection.execute("CREATE TABLE IF NOT EXISTS Person (
name VARCHAR NOT NULL,
age INT NOT NULL
)", &[])?;connection.execute("INSERT INTO Person (name, age) VALUES ($1, $2)",
&[&"Jane", &23])?;connection.execute("INSERT INTO Person (name, age) VALUES ($1, $2)",
&[&"Alice", &32])?;
let rows = connection.query("SELECT name, age FROM Person", &[])?;let people: Vec = serde_postgres::from_rows(&rows)?;
for person in people {
println!("{:?}", person);
}Ok(())
}
```