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
//! Integration with the `serde_json` crate providing JSON serialization.

extern crate serde_json;

use mime::{self, Mime};

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

use super::{Serialize, Deserialize};

use serialize;
use ::{Error, Result};

/// Serializer for JSON request bodies with compact output.
#[derive(Clone, Debug, Default)]
pub struct Serializer;

impl serialize::Serializer for Serializer {
    fn serialize<T: Serialize, W: Write>(&self, val: &T, write: &mut W) -> Result<()> {
        Error::map_serialize(self::serde_json::to_writer(write, val))
    }

    /// Returns `application/json`.
    fn content_type(&self) -> Option<Mime> {
        Some(mime::json())
    }
}

/// Serializer for JSON request bodies which pretty-prints its output.
#[derive(Clone, Debug, Default)]
pub struct PrettySerializer;

impl serialize::Serializer for PrettySerializer {
    fn serialize<T: Serialize, W: Write>(&self, val: &T, write: &mut W) -> Result<()> {
        Error::map_serialize(self::serde_json::to_writer_pretty(write, val))
    }

    fn content_type(&self) -> Option<Mime> {
        Some(mime::json())
    }
}

/// Deserializer for pulling values from JSON response bodies.
#[derive(Clone, Debug, Default)]
pub struct Deserializer;

impl serialize::Deserializer for Deserializer {
    fn deserialize<T: Deserialize, R: Read>(&self, read: &mut R) -> Result<T> {
        Error::map_deserialize(self::serde_json::from_reader(read))
    }
}