Skip to content
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

NaiveDateTime - Json deserialize error: input contains invalid characters #531

Closed
subhojit777 opened this issue Apr 2, 2019 · 3 comments

Comments

@subhojit777
Copy link

cargo.toml

actix-web = { version = "^0.7", features = ["ssl"] }
diesel = { version = "^1.4", features = ["mysql", "chrono", "r2d2"] }
dotenv = "0.13.0"
serde = "1.0.80"
serde_derive = "1.0.80"
serde_json = "1.0.32"
chrono = { version = "0.4.6", features = ["serde"] }
futures = "^0.1"
env_logger = "^0.6"
failure = "^0.1.5"

Model:

#[derive(Deserialize, Serialize, Insertable, Debug)]
#[table_name = "answers"]
pub struct AnswerForm {
    pub question_id: i32,
    pub title: String,
    pub user_id: i32,
    pub created: NaiveDateTime,
}

Schema:

CREATE TABLE answers (
  id INT PRIMARY KEY AUTO_INCREMENT,
  question_id INT NOT NULL,
  title VARCHAR(255) NOT NULL,
  user_id INT NOT NULL,
  created TIMESTAMP NOT NULL
)

For a json input like this:

{
    "question_id": 1,
    "title": "something",
    "user_id": 1,
    "created": "2007-04-05T14:30Z"
}

I am getting error:

Error occured during request handling: Json deserialize error: input contains invalid characters at line 5 column 34

This could be a problem in chrono I am not sure.

@dtolnay
Copy link
Member

dtolnay commented Apr 2, 2019

This error is not from serde_json. I would recommend minimizing a failing test case to narrow down the problem.

@dtolnay dtolnay closed this as completed Apr 2, 2019
@subhojit777
Copy link
Author

Ah. Got the problem. NaiveDateTime expects ISO 8601 combined date and time without timezone. and I was passing the date in incorrect format. The timezone was mentioned and second was not mentioned. The correct format is 2007-04-05T14:30:30. Thanks.

@dtolnay
Copy link
Member

dtolnay commented Apr 4, 2019

Nice! Glad you figured it out.

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

No branches or pull requests

2 participants