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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#![doc = include_str!("./.crate-docs.md")]
#![forbid(unsafe_code)]
#![warn(
    clippy::cargo,
    missing_docs,
    // clippy::missing_docs_in_private_items,
    clippy::pedantic,
    future_incompatible,
    rust_2018_idioms,
)]
#![allow(
    clippy::missing_errors_doc, // TODO clippy::missing_errors_doc
    clippy::option_if_let_else,
)]

use std::io::{Read, Write};

use serde::{de::DeserializeOwned, Deserialize, Serialize};
pub use serde_json;
pub use transmog;
use transmog::{BorrowedDeserializer, Format, OwnedDeserializer};

/// Json implementor of [`Format`].
#[derive(Clone, Default)]
#[must_use]
pub struct Json {
    pretty: bool,
}

impl Json {
    /// Returns an instance configured to serialize in a "pretty" format.
    pub fn pretty(mut self) -> Self {
        self.pretty = true;
        self
    }
}

impl<'a, T> Format<'a, T> for Json
where
    T: Serialize,
{
    type Error = Error;

    fn serialize(&self, value: &T) -> Result<Vec<u8>, Self::Error> {
        if self.pretty {
            serde_json::to_vec_pretty(value).map_err(Error::from)
        } else {
            serde_json::to_vec(value).map_err(Error::from)
        }
    }

    fn serialize_into<W: Write>(&self, value: &T, writer: W) -> Result<(), Self::Error> {
        if self.pretty {
            serde_json::to_writer_pretty(writer, value).map_err(Error::from)
        } else {
            serde_json::to_writer(writer, value).map_err(Error::from)
        }
    }
}

impl<'a, T> BorrowedDeserializer<'a, T> for Json
where
    T: Serialize + Deserialize<'a>,
{
    fn deserialize_borrowed(&self, data: &'a [u8]) -> Result<T, Self::Error> {
        serde_json::from_slice(data).map_err(Error::from)
    }
}

impl<T> OwnedDeserializer<T> for Json
where
    T: Serialize + DeserializeOwned,
{
    fn deserialize_owned(&self, data: &[u8]) -> Result<T, Self::Error> {
        serde_json::from_slice(data).map_err(Error::from)
    }
    fn deserialize_from<R: Read>(&self, reader: R) -> Result<T, Self::Error> {
        serde_json::from_reader(reader).map_err(Error::from)
    }
}

#[test]
fn format_tests() {
    transmog::test_util::test_format(&Json::default());
    transmog::test_util::test_format(&Json::default().pretty());
}

/// Errors from [`Json`].
#[derive(thiserror::Error, Debug)]
pub enum Error {
    /// An error occurred from parsing `Json`.
    #[error("json error: {0}")]
    Json(#[from] serde_json::Error),
    /// An Io error occurred outside of parsing `Json`.
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),
}