Skip to content

How do I use the JSON type? #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jakejscott opened this issue Apr 12, 2015 · 2 comments
Closed

How do I use the JSON type? #112

jakejscott opened this issue Apr 12, 2015 · 2 comments

Comments

@jakejscott
Copy link

I have the following code:

Cargo.toml file has this in it:

[package]
name = "postgres-queue"
version = "0.0.1"

[dependencies]
postgres = "0.8.4"
rustc-serialize = "0.3"
extern crate rustc_serialize as serialize;
extern crate postgres;

use serialize::json;
use postgres::*;


#[derive(Debug)]
struct Job {
    id: i32,
    name: String,
    json: json::Json,
    state: i32,
}

fn main() {
    let conninfo = "postgres://postgres:[email protected]/queuedb";
    let conn = Connection::connect(conninfo, &SslMode::None).unwrap();

    let stmt = conn.prepare("SELECT id, name, json, state FROM job LIMIT 10").unwrap();
    let rows = stmt.query(&[]).unwrap();

    for row in rows {

        let job = Job {
            id: row.get(0),
            name: row.get(1),
            json: row.get(2),
            state: row.get(3),
        };

        println!("Job: {:?}", job);
    }
}

When I compile I get the following error

jakescott@rustbox:~/rust/postgres-queue$ cargo run
   Compiling postgres-queue v0.0.1 (file:///home/jakescott/rust/postgres-queue)
src/main.rs:28:14: 28:20 error: the trait `postgres::types::FromSql` is not implemented for the type `rustc_serialize::json::Json` [E0277]
src/main.rs:28          json: row.get(2),
                                  ^~~~~~
error: aborting due to previous error
Could not compile `postgres-queue`.

To learn more, run the command again with --verbose.
jakescott@rustbox:~/rust/postgres-queue$ 
@jakejscott
Copy link
Author

After digging around for a while I found out about rust features.
http://doc.crates.io/manifest.html#the-[features]-section

So I needed to change my Cargo.toml file to:

[package]
name = "postgres-queue"
version = "0.0.1"

[features]
default = ["postgres"]

[dependencies]
rustc-serialize = "*"

[dependencies.postgres]
version = "*"
features = ["uuid", "rustc-serialize", "time"]
optional = true

Leaving this here in case someone else get's tripped up by this. And yes I should RTFM!

@vayn
Copy link

vayn commented Apr 19, 2015

@superlogical This error is hard to solve. Thank you for saving my day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants