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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use serde_json::{Value};
use object_builder;

/// Provides functionality to create custom JSON presenters for your structs.
///
/// ## Example
///
/// ```
/// use jsonway::{self, Serializer};
///
/// struct Jedi {
///     name: String
/// }
///
/// struct JediSerializer<'a> {
///     jedi: &'a Jedi
/// }
///
/// impl<'a> jsonway::Serializer for JediSerializer<'a> {
///     fn root(&self) -> Option<&str> { Some("jedi") }
///     fn build(&self, json: &mut jsonway::ObjectBuilder) {
///         json.set("name", &self.jedi.name);
///     }
/// }
///
/// let jedi = Jedi { name: "Saes Rrogon".to_string() };
/// let json = JediSerializer{jedi: &jedi}.serialize(true);
///
/// assert_eq!(
///     json.pointer("/jedi/name").unwrap().as_str().unwrap(),
///     "Saes Rrogon"
/// );
/// ```
pub trait Serializer {

    fn build(&self, &mut object_builder::ObjectBuilder);

    #[inline]
    fn root(&self) -> Option<&str> {
        None
    }

    fn serialize(&mut self, include_root: bool) -> Value {
        let mut bldr = object_builder::ObjectBuilder::new();
        let root = self.root();
        if include_root && root.is_some() {
            bldr.root(root.unwrap())
        }
        self.build(&mut bldr);

        bldr.unwrap()
    }
}

/// Provides functionality to create custom JSON presenters for your structs.
///
/// ## Example
///
/// ```
/// use jsonway::{self, ObjectSerializer};
///
/// struct Jedi {
///     name: String
/// }
///
/// struct JediSerializer;
///
/// impl jsonway::ObjectSerializer<Jedi> for JediSerializer {
///     fn root(&self) -> Option<&str> { Some("jedi") }
///     fn build(&self, jedi: &Jedi, json: &mut jsonway::ObjectBuilder) {
///         json.set("name", &jedi.name);
///     }
/// }
///
/// let jedi = Jedi { name: "Saes Rrogon".to_string() };
/// let json = JediSerializer.serialize(&jedi, true);
///
/// assert_eq!(
///     json.pointer("/jedi/name").unwrap().as_str().unwrap(),
///     "Saes Rrogon"
/// );
/// ```
pub trait ObjectSerializer<T> {

    fn build(&self, &T, &mut object_builder::ObjectBuilder);

    #[inline]
    fn root(&self) -> Option<&str> {
        None
    }

    fn serialize(&mut self, obj: &T, include_root: bool) -> Value {
        let mut bldr = object_builder::ObjectBuilder::new();
        let root = self.root();
        if include_root && root.is_some() {
            bldr.root(root.unwrap())
        }
        self.build(obj, &mut bldr);
        bldr.unwrap()
    }
}

/// Provides functionality to create custom JSON presenters for your structs.
///
/// ## Example
///
/// ```rust
/// use jsonway::{self, ObjectScopeSerializer};
///
/// struct User {
///     id: u64,
///     is_admin: bool
/// }
///
/// struct Jedi {
///     name: String,
///     secret: String
/// }
///
/// struct JediSerializer;
///
/// impl jsonway::ObjectScopeSerializer<Jedi, User> for JediSerializer {
///     fn root(&self) -> Option<&str> { Some("jedi") }
///     fn build(&self, jedi: &Jedi, current_user: &User, json: &mut jsonway::ObjectBuilder) {
///         json.set("name", jedi.name.to_string());
///
///         if current_user.is_admin {
///             json.set("secret", jedi.secret.to_string());
///         }
///     }
/// }
///
/// let jedi = Jedi {
///     name: "Palpatine".to_string(),
///     secret: "Dark side".to_string()
/// };
///
/// let current_user = User { id: 1, is_admin: true };
/// let json = JediSerializer.serialize(&jedi, &current_user, true);
///
/// assert_eq!(
///     json.pointer("/jedi/name").unwrap().as_str().unwrap(),
///     "Palpatine"
/// );
///
/// assert_eq!(
///     json.pointer("/jedi/secret").unwrap().as_str().unwrap(),
///     "Dark side"
/// );
///
/// ```
pub trait ObjectScopeSerializer<T, S> {

    fn build(&self, &T, &S, &mut object_builder::ObjectBuilder);

    #[inline]
    fn root(&self) -> Option<&str> {
        None
    }

    fn serialize(&mut self, obj: &T, scope: &S, include_root: bool) -> Value {
        let mut bldr = object_builder::ObjectBuilder::new();
        let root = self.root();
        if include_root && root.is_some() {
            bldr.root(root.unwrap())
        }
        self.build(obj, scope, &mut bldr);
        bldr.unwrap()
    }
}