1extern crate std;
2
3use nanojson::{Serialize, Deserialize};
4
5#[derive(Serialize, Deserialize, Debug, PartialEq)]
10struct Comment {
11 user: String,
12 message: String,
13}
14
15#[derive(Serialize, Deserialize, Debug, PartialEq)]
16struct Post {
17 id: i64,
18 title: String,
19 body: String,
20 comments: Vec<Comment>,
21}
22
23#[derive(Serialize, Deserialize, Debug, PartialEq)]
24struct Thread {
25 name: String,
26 posts: Vec<Post>,
27}
28
29#[derive(Serialize, Deserialize, Debug, PartialEq)]
34struct Node {
35 value: String,
36 children: Vec<Box<Node>>, }
38
39#[derive(Serialize, Deserialize, Debug, PartialEq)]
44enum Reaction {
45 Like,
46 Dislike,
47 Laugh,
48}
49
50#[derive(Serialize, Deserialize, Debug, PartialEq)]
51enum Activity {
52 View { post_id: i64 },
53 React { post_id: i64, reaction: Reaction },
54 Comment { post_id: i64, message: String },
55}
56
57#[derive(Serialize, Deserialize, Debug, PartialEq)]
62struct Forum {
63 threads: Vec<Thread>,
64 activity_feed: Vec<Activity>,
65 category_tree: Box<Node>,
66 tags: Vec<Vec<String>>, }
68
69fn main() {
74 let forum = Forum {
75 threads: vec![
76 Thread {
77 name: "General".to_string(),
78 posts: vec![
79 Post {
80 id: 1,
81 title: "Welcome".to_string(),
82 body: "Welcome to the forum!".to_string(),
83 comments: vec![
84 Comment {
85 user: "alice".to_string(),
86 message: "Nice to be here!".to_string(),
87 },
88 Comment {
89 user: "bob".to_string(),
90 message: "Hello everyone!".to_string(),
91 },
92 ],
93 },
94 Post {
95 id: 2,
96 title: "Rules".to_string(),
97 body: "Be nice.".to_string(),
98 comments: vec![],
99 },
100 ],
101 },
102 Thread {
103 name: "Rust".to_string(),
104 posts: vec![
105 Post {
106 id: 3,
107 title: "Ownership".to_string(),
108 body: "Let's discuss ownership.".to_string(),
109 comments: vec![
110 Comment {
111 user: "charlie".to_string(),
112 message: "It's tricky at first.".to_string(),
113 }
114 ],
115 }
116 ],
117 }
118 ],
119
120 activity_feed: vec![
121 Activity::View { post_id: 1 },
122 Activity::React { post_id: 1, reaction: Reaction::Like },
123 Activity::Comment { post_id: 2, message: "Important!".to_string() },
124 ],
125
126 category_tree: Box::new(Node {
127 value: "root".to_string(),
128 children: vec![
129 Box::new(Node {
130 value: "programming".to_string(),
131 children: vec![
132 Box::new(Node {
133 value: "rust".to_string(),
134 children: vec![],
135 }),
136 Box::new(Node {
137 value: "c++".to_string(),
138 children: vec![],
139 }),
140 ],
141 }),
142 Box::new(Node {
143 value: "offtopic".to_string(),
144 children: vec![],
145 }),
146 ],
147 }),
148
149 tags: vec![
150 vec!["rust".to_string(), "systems".to_string()],
151 vec!["fun".to_string()],
152 vec![],
153 ],
154 };
155
156 let json = nanojson::stringify_pretty(2, &forum).unwrap();
161 std::println!("Forum JSON:\n{json}");
162
163 let decoded: Forum = nanojson::parse(&json).unwrap();
168 std::println!("\nDecoded:\n{:?}", decoded);
169
170 assert_eq!(forum, decoded);
171
172 let raw = r#"
177 {
178 "threads": [
179 {
180 "name": "Announcements",
181 "posts": [
182 {
183 "id": 10,
184 "title": "Update",
185 "body": "New features added",
186 "comments": [
187 { "user": "admin", "message": "Enjoy!" }
188 ]
189 }
190 ]
191 }
192 ],
193 "activity_feed": [
194 { "View": { "post_id": 10 } },
195 { "React": { "post_id": 10, "reaction": "Laugh" } }
196 ],
197 "category_tree": {
198 "value": "root",
199 "children": []
200 },
201 "tags": [
202 ["news"],
203 []
204 ]
205 }
206 "#;
207
208 println!("");
209 match nanojson::parse::<Forum>(raw) {
210 Ok(parsed) => {
211 std::println!("\nParsed from raw JSON:\n{:?}", parsed);
212 }
213 Err(e) => e.print(raw),
214 }
215}
216
217#[cfg(test)] #[test] fn test_main() { main() }