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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use serde::de::DeserializeOwned;
use crate::Error;
pub fn json_decode<T: ?Sized>(datas: Vec<serde_json::Value>) -> Result<T, crate::Error>
where T: DeserializeOwned {
let mut js = serde_json::Value::Null;
let type_name = std::any::type_name::<T>();
if is_array(type_name) {
js = serde_json::Value::Array(datas);
} else {
match type_name {
"core::option::Option<i8>" | "core::option::Option<i16>" | "core::option::Option<i32>" | "core::option::Option<i64>" |
"core::option::Option<u8>" | "core::option::Option<u16>" | "core::option::Option<u32>" | "core::option::Option<u64>" |
"core::option::Option<f32>" | "core::option::Option<f64>" |
"core::option::Option<serde_json::number::Number>" |
"core::option::Option<bigdecimal::BigDecimal>" |
"core::option::Option<bool>" |
"core::option::Option<alloc::string::String>" |
"i8" | "i16" | "i32" | "i64" |
"u8" | "u16" | "u32" | "u64" |
"f32" | "f64" |
"serde_json::number::Number" |
"bigdecimal::BigDecimal" |
"bool" |
"alloc::string::String" => {
if datas.len() > 1 {
return Result::Err(Error::from(format!("[rbatis] rows.rows_affected > 1,but decode one result({})!", type_name)));
}
for item in datas {
match item {
serde_json::Value::Object(arg) => {
for (_, r) in arg {
match type_name {
"alloc::string::String" | "bigdecimal::BigDecimal" => {
js = serde_json::Value::String(r.to_string());
}
_ => {
js = r;
}
}
break;
}
}
_ => {}
}
break;
}
}
"serde_json::value::Value" => {
js = serde_json::Value::Array(datas)
}
_ => {
if datas.len() > 1 {
return Result::Err(Error::from(format!("[rbatis] rows.rows_affected > 1,but decode one result({})!", type_name)));
}
for x in datas {
js = x;
break;
}
}
}
}
#[cfg(feature = "debug_mode")]
println!("[rbatis] [debug_mode] => {:#}", js);
let decode_result = serde_json::from_value(js);
if decode_result.is_ok() {
return Result::Ok(decode_result.unwrap());
} else {
let e = decode_result.err().unwrap().to_string();
return Result::Err(Error::from(format!("[rbatis] json_decode fail decode_type:\"{}\",serde_err:\"{}\"", type_name, e)));
}
}
fn is_array(type_name: &str) -> bool {
if type_name.starts_with("[")
|| type_name.starts_with("&[")
|| type_name.starts_with("alloc::collections::linked_list")
|| type_name.starts_with("alloc::collections::vec_deque")
|| type_name.starts_with("std::collections::hash::set")
|| type_name.starts_with("alloc::vec::Vec<")
|| type_name.starts_with("core::option::Option<[")
|| type_name.starts_with("core::option::Option<&[")
|| type_name.starts_with("core::option::Option<alloc::vec::Vec<")
|| type_name.starts_with("core::option::Option<alloc::collections::linked_list")
|| type_name.starts_with("core::option::Option<alloc::collections::vec_deque")
|| type_name.starts_with("core::option::Option<std::collections::hash::set")
{
return true;
}
return false;
}
#[cfg(test)]
mod test {
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
use serde_json::json;
use crate::decode::{is_array, json_decode};
#[test]
fn test_decode_hashmap() {
let m: HashMap<String, serde_json::Value> = json_decode(vec![json!(
{
"a":"1",
"b":2
}
)]).unwrap();
println!("{:#?}", m);
}
#[test]
fn test_decode_btree_map() {
let m: BTreeMap<String, serde_json::Value> = json_decode(vec![json!(
{
"a":"1",
"b":2
}
)]).unwrap();
println!("{:#?}", m);
}
#[test]
fn test_is_array() {
let dq_name = std::any::type_name::<VecDeque::<i32>>();
assert_eq!(is_array(dq_name), true);
}
}