Function res_to_json

Source
pub fn res_to_json<T: Serialize>(res: T) -> Value
Expand description

Serializes a value into a JSON Value for AppSync responses

§Arguments

  • res - Value to serialize that implements Serialize

§Returns

JSON Value representation of the input

§Panics

Panics if the value cannot be serialized to JSON. This should never happen for valid AppSync schema objects as generated by the appsync_lambda_main proc macro.

§Examples

#[derive(Serialize)]
struct User {
    id: String,
    name: String
}

let user = User {
    id: "123".to_string(),
    name: "John".to_string()
};

let json = res_to_json(user);
assert_eq!(json, json!({
    "id": "123",
    "name": "John"
}));

// Simple types also work
let num = res_to_json(42);
assert_eq!(num, json!(42));