How it works...
Adding JSON handling to an actix-web web service is easy—thanks to the deep integration of the popular serde crate (https://crates.io/crates/serde). After some imports in step 1, we declare a Bookmark struct as Serialize and Deserialize in step 2, which enables serde to generate and parse JSON for this data type.
The change in the handler functions is also minimal since returning and ingesting JSON is a very common task. The required function to return a JSON payload with the response is attached to the HttpResponse factory method that does everything, including setting the appropriate content type. On the ingest part, there is a web::Json<T> type to take care of deserializing and checking whatever is forwarded into the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access