1extern crate std;
2
3use std::collections::HashMap;
4use nanojson::{Serialize, Deserialize};
5
6#[derive(Serialize, Deserialize, Debug, PartialEq)]
11struct Address {
12 street: String,
13 city: String,
14 zip: String,
15}
16
17#[derive(Serialize, Deserialize, Debug, PartialEq)]
18struct Profile {
19 username: String,
20 email: String,
21 address: Address,
22}
23
24#[derive(Serialize, Deserialize, Debug, PartialEq)]
25struct Item {
26 id: i64,
27 name: String,
28 price: i64,
29}
30
31#[derive(Serialize, Deserialize, Debug, PartialEq)]
32struct Inventory {
33 items: Vec<Item>,
34 metadata: HashMap<String, String>,
35}
36
37#[derive(Serialize, Deserialize, Debug, PartialEq)]
42enum Role {
43 Admin,
44 User,
45 Guest,
46}
47
48#[derive(Serialize, Deserialize, Debug, PartialEq)]
49enum Status {
50 Active,
51 Disabled,
52 Pending,
53}
54
55#[derive(Serialize, Deserialize, Debug, PartialEq)]
56enum Event {
57 Login { user_id: i64 },
58 Purchase { item_id: i64, quantity: i64 },
59 Logout,
60}
61
62#[derive(Serialize, Deserialize, Debug, PartialEq)]
67struct User {
68 id: i64,
69 profile: Box<Profile>, roles: Vec<Role>, status: Status,
72 tags: Vec<String>, preferences: HashMap<String, String>,
74}
75
76#[derive(Serialize, Deserialize, Debug, PartialEq)]
77struct AppState {
78 users: Vec<User>,
79 inventory: Inventory,
80 sessions: HashMap<String, i32>,
81 events: Vec<Event>,
82}
83
84fn main() {
89 let mut prefs = HashMap::new();
90 prefs.insert("theme".to_string(), "dark".to_string());
91 prefs.insert("language".to_string(), "en".to_string());
92
93 let mut metadata = HashMap::new();
94 metadata.insert("warehouse".to_string(), "A1".to_string());
95 metadata.insert("currency".to_string(), "USD".to_string());
96
97 let mut sessions = HashMap::new();
98 sessions.insert("session_abc".to_string(), 1);
99 sessions.insert("session_xyz".to_string(), 2);
100
101 let state = AppState {
102 users: vec![
103 User {
104 id: 1,
105 profile: Box::new(Profile {
106 username: "alice".to_string(),
107 email: "alice@example.com".to_string(),
108 address: Address {
109 street: "Main St".to_string(),
110 city: "Wonderland".to_string(),
111 zip: "12345".to_string(),
112 },
113 }),
114 roles: vec![Role::Admin, Role::User],
115 status: Status::Active,
116 tags: vec!["premium".to_string(), "beta".to_string()],
117 preferences: prefs.clone(),
118 },
119 User {
120 id: 2,
121 profile: Box::new(Profile {
122 username: "bob".to_string(),
123 email: "bob@example.com".to_string(),
124 address: Address {
125 street: "Second St".to_string(),
126 city: "Builderland".to_string(),
127 zip: "67890".to_string(),
128 },
129 }),
130 roles: vec![Role::User],
131 status: Status::Pending,
132 tags: vec![],
133 preferences: prefs.clone(),
134 },
135 ],
136 inventory: Inventory {
137 items: vec![
138 Item { id: 1, name: "Sword".to_string(), price: 100 },
139 Item { id: 2, name: "Shield".to_string(), price: 150 },
140 ],
141 metadata,
142 },
143 sessions,
144 events: vec![
145 Event::Login { user_id: 1 },
146 Event::Purchase { item_id: 2, quantity: 1 },
147 Event::Logout,
148 ],
149 };
150
151 let json = nanojson::stringify_pretty(2, &state).unwrap();
156 std::println!("AppState JSON:\n{json}");
157
158 let decoded: AppState = nanojson::parse(&json).unwrap();
163 std::println!("\nDecoded:\n{:?}", decoded);
164
165 assert_eq!(state, decoded);
166
167 let raw = r#"
172 {
173 "users": [
174 {
175 "id": 3,
176 "profile": {
177 "username": "charlie",
178 "email": "charlie@example.com",
179 "address": {
180 "street": "Third St",
181 "city": "ChocolateFactory",
182 "zip": "99999"
183 }
184 },
185 "roles": ["Guest"],
186 "status": "Disabled",
187 "tags": ["new"],
188 "preferences": {
189 "theme": "light"
190 }
191 }
192 ],
193 "inventory": {
194 "items": [
195 { "id": 10, "name": "Potion", "price": 50 }
196 ],
197 "metadata": {
198 "warehouse": "B2"
199 }
200 },
201 "sessions": {
202 "session_foo": 3
203 },
204 "events": [
205 { "Login": { "user_id": 3 } },
206 { "Logout": null }
207 ]
208 }
209 "#;
210
211 println!("");
212 match nanojson::parse::<AppState>(raw) {
213 Ok(parsed) => {
214 std::println!("\nParsed from raw JSON:\n{:?}", parsed);
215 }
216 Err(e) => e.print(raw),
217 }
218}
219
220#[cfg(test)] #[test] fn test_main() { main() }