jsonway/
serializer.rs

1use serde_json::{Value};
2use object_builder;
3
4/// Provides functionality to create custom JSON presenters for your structs.
5///
6/// ## Example
7///
8/// ```
9/// use jsonway::{self, Serializer};
10///
11/// struct Jedi {
12///     name: String
13/// }
14///
15/// struct JediSerializer<'a> {
16///     jedi: &'a Jedi
17/// }
18///
19/// impl<'a> jsonway::Serializer for JediSerializer<'a> {
20///     fn root(&self) -> Option<&str> { Some("jedi") }
21///     fn build(&self, json: &mut jsonway::ObjectBuilder) {
22///         json.set("name", &self.jedi.name);
23///     }
24/// }
25///
26/// let jedi = Jedi { name: "Saes Rrogon".to_string() };
27/// let json = JediSerializer{jedi: &jedi}.serialize(true);
28///
29/// assert_eq!(
30///     json.pointer("/jedi/name").unwrap().as_str().unwrap(),
31///     "Saes Rrogon"
32/// );
33/// ```
34pub trait Serializer {
35
36    fn build(&self, &mut object_builder::ObjectBuilder);
37
38    #[inline]
39    fn root(&self) -> Option<&str> {
40        None
41    }
42
43    fn serialize(&mut self, include_root: bool) -> Value {
44        let mut bldr = object_builder::ObjectBuilder::new();
45        let root = self.root();
46        if include_root && root.is_some() {
47            bldr.root(root.unwrap())
48        }
49        self.build(&mut bldr);
50
51        bldr.unwrap()
52    }
53}
54
55/// Provides functionality to create custom JSON presenters for your structs.
56///
57/// ## Example
58///
59/// ```
60/// use jsonway::{self, ObjectSerializer};
61///
62/// struct Jedi {
63///     name: String
64/// }
65///
66/// struct JediSerializer;
67///
68/// impl jsonway::ObjectSerializer<Jedi> for JediSerializer {
69///     fn root(&self) -> Option<&str> { Some("jedi") }
70///     fn build(&self, jedi: &Jedi, json: &mut jsonway::ObjectBuilder) {
71///         json.set("name", &jedi.name);
72///     }
73/// }
74///
75/// let jedi = Jedi { name: "Saes Rrogon".to_string() };
76/// let json = JediSerializer.serialize(&jedi, true);
77///
78/// assert_eq!(
79///     json.pointer("/jedi/name").unwrap().as_str().unwrap(),
80///     "Saes Rrogon"
81/// );
82/// ```
83pub trait ObjectSerializer<T> {
84
85    fn build(&self, &T, &mut object_builder::ObjectBuilder);
86
87    #[inline]
88    fn root(&self) -> Option<&str> {
89        None
90    }
91
92    fn serialize(&mut self, obj: &T, include_root: bool) -> Value {
93        let mut bldr = object_builder::ObjectBuilder::new();
94        let root = self.root();
95        if include_root && root.is_some() {
96            bldr.root(root.unwrap())
97        }
98        self.build(obj, &mut bldr);
99        bldr.unwrap()
100    }
101}
102
103/// Provides functionality to create custom JSON presenters for your structs.
104///
105/// ## Example
106///
107/// ```rust
108/// use jsonway::{self, ObjectScopeSerializer};
109///
110/// struct User {
111///     id: u64,
112///     is_admin: bool
113/// }
114///
115/// struct Jedi {
116///     name: String,
117///     secret: String
118/// }
119///
120/// struct JediSerializer;
121///
122/// impl jsonway::ObjectScopeSerializer<Jedi, User> for JediSerializer {
123///     fn root(&self) -> Option<&str> { Some("jedi") }
124///     fn build(&self, jedi: &Jedi, current_user: &User, json: &mut jsonway::ObjectBuilder) {
125///         json.set("name", jedi.name.to_string());
126///
127///         if current_user.is_admin {
128///             json.set("secret", jedi.secret.to_string());
129///         }
130///     }
131/// }
132///
133/// let jedi = Jedi {
134///     name: "Palpatine".to_string(),
135///     secret: "Dark side".to_string()
136/// };
137///
138/// let current_user = User { id: 1, is_admin: true };
139/// let json = JediSerializer.serialize(&jedi, &current_user, true);
140///
141/// assert_eq!(
142///     json.pointer("/jedi/name").unwrap().as_str().unwrap(),
143///     "Palpatine"
144/// );
145///
146/// assert_eq!(
147///     json.pointer("/jedi/secret").unwrap().as_str().unwrap(),
148///     "Dark side"
149/// );
150///
151/// ```
152pub trait ObjectScopeSerializer<T, S> {
153
154    fn build(&self, &T, &S, &mut object_builder::ObjectBuilder);
155
156    #[inline]
157    fn root(&self) -> Option<&str> {
158        None
159    }
160
161    fn serialize(&mut self, obj: &T, scope: &S, include_root: bool) -> Value {
162        let mut bldr = object_builder::ObjectBuilder::new();
163        let root = self.root();
164        if include_root && root.is_some() {
165            bldr.root(root.unwrap())
166        }
167        self.build(obj, scope, &mut bldr);
168        bldr.unwrap()
169    }
170}