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
use super::Format;

use serde::{de::DeserializeSeed, Serialize};

use crate::kind::Fallible;

/// A format implementing JavaScript Object Notation.
///
/// JSON is a human readable object
/// serialization format used in a diverse range of applications.
/// This wraps functionality provided by the `serde_json` crate.
///
/// For this format to be used the `json` feature must be enabled.
pub struct Json;

impl Format for Json {
    type Representation = String;
    type Error = serde_json::Error;

    fn serialize<T: Serialize>(item: T) -> Self::Representation {
        serde_json::to_string(&item).unwrap()
    }

    fn deserialize<'de, T: DeserializeSeed<'de>>(
        item: Self::Representation,
        context: T,
    ) -> Fallible<T::Value, (Self::Error, Self::Representation)>
    where
        T: Sync + Send + 'static,
    {
        Box::pin(async move {
            let mut deserializer = serde_json::Deserializer::from_reader(item.as_bytes());
            context
                .deserialize(&mut deserializer)
                .map_err(|e| (e, item))
        })
    }
}