1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use ;
use DeserializeOwned;
/// Json struct is a wrapper to handle the return types from the RedisJSON commands.
///
/// RedisJSON usually returns values in square brackets, which you usually had to handle manually:
///
/// ```rust,no_run
/// # use redis::{Client, JsonCommands, RedisResult};
/// # use redis_macros::{FromRedisValue, ToRedisArgs, Json};
/// # use serde::{Deserialize, Serialize};
/// # #[derive(Serialize, Deserialize)]
/// # struct User { id: u32 }
/// #
/// # fn main () -> redis::RedisResult<()> {
/// # let client = redis::Client::open("redis://localhost:6379/")?;
/// # let mut con = client.get_connection()?;
/// # let _: () = con.json_set("user", "$", &r#"{ "id": 1 }"#)?;
/// // You have to manually deserialize this and pull from the Vec
/// let user_id: String = con.json_get("user", "$.id")?; // => "[1]"
/// # Ok(())
/// # }
/// ```
///
/// Instead, `Json` implements the `FromRedisValue` trait, removes the square brackets and deserializes from JSON.
/// For this your type don't even have to implement `FromRedisValue`, it only requires to be serde `Deserialize`-able.
///
/// ```rust,no_run
/// # use redis::{Client, JsonCommands, RedisResult};
/// # use redis_macros::Json;
/// # use serde::{Deserialize, Serialize};
/// #[derive(Serialize, Deserialize)]
/// struct User { id: u32 }
///
/// # fn main () -> redis::RedisResult<()> {
/// # let client = redis::Client::open("redis://localhost:6379/")?;
/// # let mut con = client.get_connection()?;
/// # let _: () = con.json_set("user", "$", &r#"{ "id": 1 }"#)?;
/// let Json(user_id): Json<u32> = con.json_get("user", "$.id")?; // => 1
/// let Json(user): Json<User> = con.json_get("user", "$")?; // => User { id: 1 }
/// # Ok(())
/// # }
/// ```
///
/// This command is designed to use RedisJSON commands. You could probably use this type
/// to parse normal command outputs, but it removes the first and last character
/// so it is not recommended.
///
;