質問編集履歴
4
JSONキーをダブルクオートでかこみました。JSON内dateプロパティ末尾の不要なカンマを削除しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,6 +6,112 @@
|
|
6
6
|
|
7
7
|
```rust
|
8
8
|
|
9
|
+
let json_str = r#"
|
10
|
+
|
11
|
+
{
|
12
|
+
|
13
|
+
"id": 1,
|
14
|
+
|
15
|
+
"date": "2021-03-15"
|
16
|
+
|
17
|
+
}"#;
|
18
|
+
|
19
|
+
```
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
↓
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
```rust
|
28
|
+
|
29
|
+
#[derive(Serialize, Deserialize, Debug)]
|
30
|
+
|
31
|
+
struct Record {
|
32
|
+
|
33
|
+
id: i32,
|
34
|
+
|
35
|
+
#[serde(with="yyyy_mm_dd_fmt")]
|
36
|
+
|
37
|
+
date: NaiveDate,
|
38
|
+
|
39
|
+
}
|
40
|
+
|
41
|
+
```
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
## 現状のコード全体
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
```rust
|
50
|
+
|
51
|
+
mod yyyy_mm_dd_fmt {
|
52
|
+
|
53
|
+
use chrono::{NaiveDate};
|
54
|
+
|
55
|
+
use serde::{self, Deserialize, Serializer, Deserializer};
|
56
|
+
|
57
|
+
const FORMAT: &'static str = "%Y-%m-%d";
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
pub fn serialize<S>(date: &NaiveDate, serializer: S) -> Result<S::Ok, S::Error>
|
62
|
+
|
63
|
+
where S: Serializer,
|
64
|
+
|
65
|
+
{
|
66
|
+
|
67
|
+
let s = format!("{}", date.format(FORMAT));
|
68
|
+
|
69
|
+
serializer.serialize_str(&s)
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
pub fn deserialize<'de, D>(deserializer: D) -> Result<NaiveDate, D::Error>
|
76
|
+
|
77
|
+
where D: Deserializer<'de>,
|
78
|
+
|
79
|
+
{
|
80
|
+
|
81
|
+
let s = String::deserialize(deserializer)?;
|
82
|
+
|
83
|
+
let nd = NaiveDate::parse_from_str(&s, FORMAT);
|
84
|
+
|
85
|
+
nd
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
use serde::{Serialize, Deserialize};
|
94
|
+
|
95
|
+
use chrono::NaiveDate;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
#[derive(Serialize, Deserialize, Debug)]
|
100
|
+
|
101
|
+
struct Record {
|
102
|
+
|
103
|
+
id: i32,
|
104
|
+
|
105
|
+
#[serde(with="yyyy_mm_dd_fmt")]
|
106
|
+
|
107
|
+
date: NaiveDate,
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
fn main() {
|
114
|
+
|
9
115
|
let json_str = r#"
|
10
116
|
|
11
117
|
{
|
@@ -16,142 +122,38 @@
|
|
16
122
|
|
17
123
|
}"#;
|
18
124
|
|
125
|
+
let record: Record = serde_json::from_str(json_str).unwrap();
|
126
|
+
|
127
|
+
println!("{:?}", record)
|
128
|
+
|
129
|
+
}
|
130
|
+
|
19
|
-
```
|
131
|
+
```
|
20
|
-
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
21
|
-
|
137
|
+
### きになっているところ
|
22
|
-
|
23
|
-
|
24
|
-
|
138
|
+
|
139
|
+
|
140
|
+
|
25
|
-
```rust
|
141
|
+
```rust
|
26
|
-
|
27
|
-
|
142
|
+
|
28
|
-
|
29
|
-
struct Record {
|
30
|
-
|
31
|
-
id: i32,
|
32
|
-
|
33
|
-
#[serde(with="yyyy_mm_dd_fmt")]
|
34
|
-
|
35
|
-
date: NaiveDate,
|
36
|
-
|
37
|
-
}
|
38
|
-
|
39
|
-
```
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
## 現状のコード全体
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
```rust
|
48
|
-
|
49
|
-
mod yyyy_mm_dd_fmt {
|
50
|
-
|
51
|
-
use chrono::{NaiveDate};
|
52
|
-
|
53
|
-
use serde::{self, Deserialize, Serializer, Deserializer};
|
54
|
-
|
55
|
-
const FORMAT: &'static str = "%Y-%m-%d";
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
pub fn serialize<
|
143
|
+
pub fn deserialize<'de, D>(deserializer: D) -> Result<NaiveDate, D::Error>
|
60
|
-
|
144
|
+
|
61
|
-
where
|
145
|
+
where D: Deserializer<'de>,
|
62
|
-
|
146
|
+
|
63
|
-
{
|
147
|
+
{
|
148
|
+
|
64
|
-
|
149
|
+
let s = String::deserialize(deserializer)?;
|
150
|
+
|
65
|
-
let
|
151
|
+
let nd = NaiveDate::parse_from_str(&s, FORMAT);
|
66
|
-
|
152
|
+
|
67
|
-
|
153
|
+
nd
|
68
154
|
|
69
155
|
}
|
70
156
|
|
71
|
-
|
72
|
-
|
73
|
-
pub fn deserialize<'de, D>(deserializer: D) -> Result<NaiveDate, D::Error>
|
74
|
-
|
75
|
-
where D: Deserializer<'de>,
|
76
|
-
|
77
|
-
{
|
78
|
-
|
79
|
-
let s = String::deserialize(deserializer)?;
|
80
|
-
|
81
|
-
let nd = NaiveDate::parse_from_str(&s, FORMAT);
|
82
|
-
|
83
|
-
nd
|
84
|
-
|
85
|
-
}
|
86
|
-
|
87
|
-
}
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
use serde::{Serialize, Deserialize};
|
92
|
-
|
93
|
-
use chrono::NaiveDate;
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
#[derive(Serialize, Deserialize, Debug)]
|
98
|
-
|
99
|
-
struct Record {
|
100
|
-
|
101
|
-
id: i32,
|
102
|
-
|
103
|
-
#[serde(with="yyyy_mm_dd_fmt")]
|
104
|
-
|
105
|
-
date: NaiveDate,
|
106
|
-
|
107
|
-
}
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
fn main() {
|
112
|
-
|
113
|
-
let json_str = r#"
|
114
|
-
|
115
|
-
{
|
116
|
-
|
117
|
-
id:1,
|
118
|
-
|
119
|
-
date:"2021-03-15",
|
120
|
-
|
121
|
-
}"#;
|
122
|
-
|
123
|
-
let record: Record = serde_json::from_str(json_str).unwrap();
|
124
|
-
|
125
|
-
println!("{:?}", record)
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
```
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
### きになっているところ
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
```rust
|
140
|
-
|
141
|
-
pub fn deserialize<'de, D>(deserializer: D) -> Result<NaiveDate, D::Error>
|
142
|
-
|
143
|
-
where D: Deserializer<'de>,
|
144
|
-
|
145
|
-
{
|
146
|
-
|
147
|
-
let s = String::deserialize(deserializer)?;
|
148
|
-
|
149
|
-
let nd = NaiveDate::parse_from_str(&s, FORMAT);
|
150
|
-
|
151
|
-
nd
|
152
|
-
|
153
|
-
}
|
154
|
-
|
155
157
|
```
|
156
158
|
|
157
159
|
|
3
Fix typo (D::Deserializer -> Deserializer)
test
CHANGED
File without changes
|
test
CHANGED
@@ -158,7 +158,7 @@
|
|
158
158
|
|
159
159
|
↑ 返り値の型が違うのでここでこのまま返せないのはわかっているんですが、matchなりmap_errを使ったあとに、
|
160
160
|
|
161
|
-
どうやって `chrono::ParseError` から `D
|
161
|
+
どうやって `chrono::ParseError` から `Deserializer<'de>` オブジェクトを作ればよいのでしょう?
|
162
162
|
|
163
163
|
|
164
164
|
|
2
TypoFix
test
CHANGED
File without changes
|
test
CHANGED
@@ -154,7 +154,13 @@
|
|
154
154
|
|
155
155
|
```
|
156
156
|
|
157
|
+
|
158
|
+
|
159
|
+
↑ 返り値の型が違うのでここでこのまま返せないのはわかっているんですが、matchなりmap_errを使ったあとに、
|
160
|
+
|
157
|
-
|
161
|
+
どうやって `chrono::ParseError` から `D::Deserializer<'de>` オブジェクトを作ればよいのでしょう?
|
162
|
+
|
163
|
+
|
158
164
|
|
159
165
|
|
160
166
|
|
1
不要なコメントアウト部分を削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,8 +30,6 @@
|
|
30
30
|
|
31
31
|
id: i32,
|
32
32
|
|
33
|
-
|
34
|
-
|
35
33
|
#[serde(with="yyyy_mm_dd_fmt")]
|
36
34
|
|
37
35
|
date: NaiveDate,
|
@@ -54,8 +52,6 @@
|
|
54
52
|
|
55
53
|
use serde::{self, Deserialize, Serializer, Deserializer};
|
56
54
|
|
57
|
-
|
58
|
-
|
59
55
|
const FORMAT: &'static str = "%Y-%m-%d";
|
60
56
|
|
61
57
|
|
@@ -71,8 +67,6 @@
|
|
71
67
|
serializer.serialize_str(&s)
|
72
68
|
|
73
69
|
}
|
74
|
-
|
75
|
-
|
76
70
|
|
77
71
|
|
78
72
|
|
@@ -98,10 +92,6 @@
|
|
98
92
|
|
99
93
|
use chrono::NaiveDate;
|
100
94
|
|
101
|
-
// use std::time::{Duration, Instant};
|
102
|
-
|
103
|
-
|
104
|
-
|
105
95
|
|
106
96
|
|
107
97
|
#[derive(Serialize, Deserialize, Debug)]
|
@@ -109,8 +99,6 @@
|
|
109
99
|
struct Record {
|
110
100
|
|
111
101
|
id: i32,
|
112
|
-
|
113
|
-
|
114
102
|
|
115
103
|
#[serde(with="yyyy_mm_dd_fmt")]
|
116
104
|
|