Hello, I'm working on an application which is basically a search API for some PHP software we're using. I'm trying to use the Rocket framework which is going well, and I'm able to execute a SQL query and output the data onto the page but I'd like to format it as JSON. How can I do this using something like serde_json? Currently I manually manufacture my data output like this:
Ok(rows) => {
let mut users_string = String::new();
let mut num_users = 0;
for row in rows.iter() {
let user_id: i32 = row.get("u");
let user_name: String = row.get("n");
users_string.push_str(&user_id.to_string());
users_string.push_str(": ");
users_string.push_str(&user_name);
users_string.push_str(", ");
num_users += 1;
}
if num_users > 0 {
let users_string_len = users_string.len();
users_string.truncate(users_string_len - 2)
}
users_string
},
Instead, I would like to have some JSON output like this:
[{u:'12', n:'cool user'}]
Ideas, tutorials and tips very welcome.
Thanks!