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

Created on 2 Apr 2019  路  3Comments  路  Source: serde-rs/json

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.

Most helpful comment

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.

All 3 comments

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

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.

Nice! Glad you figured it out.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mnacamura picture mnacamura  路  6Comments

AerialX picture AerialX  路  5Comments

VictorKoenders picture VictorKoenders  路  6Comments

harindaka picture harindaka  路  4Comments

mfarrugi picture mfarrugi  路  4Comments