Stuhl
Stuhl

Reputation: 39

serde_json to json prints addtitonal String \r and \n in diesel db object

Playground link

use serde_json::json; // 1.0.57
fn main() {

let users = vec![Users {
  id : 10,
  username : "test".to_string(),
  password : "pass".to_string()
  }];
 for user in &users {
     println!("I print  id:{},password:{},username:{} ",user.id, user.password, user.username);
 }
 println!("json_serde prints {}",json!(&users));
 
 let serialized = serde_json::to_string(&users).unwrap();
 println!("Different serde: {}",serialized);
}
#[derive(Serialize, Deserialize)]
pub struct Users {
 pub id: i32,
 pub username: String,
 pub password: String,
}

It works perfect but in my server i get this

I print  id:4,password:test, username:test
json_serde prints [{"id":4,"password":"test\r\n","username":"test\r"}]

The only difference is I get my data from the db

Diesel query

    let users = users
    .filter(id.eq(p_id))
    .limit(10)
    .load::<Users>(&connection)
    .expect("Error loading posts"),

Actual Users in model.rs

    #[derive(Queryable)]
    #[derive(Serialize, Deserialize)]
    pub struct Users {
        pub id: i32,
        pub username: String,
        pub password: String,
    }

shema.rs

 table! {
        users (id) {
            id -> Int4,
            username -> Varchar,
            password -> Varchar,
        }
    }

Upvotes: 1

Views: 440

Answers (1)

Stuhl
Stuhl

Reputation: 39

I forget to trim my input so serde_json was correct

Upvotes: 1

Related Questions