1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#![warn(missing_docs)]
#![warn(missing_copy_implementations, missing_debug_implementations)]
#![warn(unused_qualifications, unused_results)]
#![warn(future_incompatible)]
#![warn(unused)]
#![forbid(broken_intra_doc_links)]
use dialectic_tokio_serde::*;
use serde::{Deserialize, Serialize};
use serde_json as json;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_util::codec::LinesCodec;
#[derive(Debug, Clone, Copy)]
pub struct Json {
pretty: bool,
}
impl Json {
pub fn new() -> Self {
Json::default()
}
pub fn pretty() -> Self {
Json { pretty: true }
}
}
impl Default for Json {
fn default() -> Self {
Json { pretty: false }
}
}
impl Serializer for Json {
type Error = json::Error;
type Output = String;
fn serialize<T: Serialize>(&mut self, item: &T) -> Result<Self::Output, Self::Error> {
json::to_string(item)
}
}
impl<Input: AsRef<str>> Deserializer<Input> for Json {
type Error = json::Error;
fn deserialize<T: for<'a> Deserialize<'a>>(&mut self, src: &Input) -> Result<T, Self::Error> {
json::from_str(src.as_ref())
}
}
pub fn lines<W: AsyncWrite, R: AsyncRead>(
writer: W,
reader: R,
max_length: usize,
) -> (Sender<Json, LinesCodec, W>, Receiver<Json, LinesCodec, R>) {
symmetrical(
Json::default(),
LinesCodec::new_with_max_length(max_length),
writer,
reader,
)
}