Serializing responses

The service returns random numbers represented as the f64 type. We want to return it packed to a JSON object, because we may need to add more fields to the object. It's simple to use objects in JavaScript. Declare the RngResponse struct and add the #[derive(Serailize)] attribute. This makes this struct serializable, as follows:

#[derive(Serialize)]
struct RngResponse {
value: f64,
}

To make this struct deserializable, we should derive the Deserialize trait. Deserialization may be useful it you want to use the same type for requests and responses. It's also important to derive both Serialize and Deserialize if you want to use the same type in a server and a client.

The serialized object will be represented as a string, as follows:

{ "value": 0.123456 }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.0.85