1use crate::io::MySqlBufMutExt;
2use crate::types::{Decode, Encode};
3use crate::value::MySqlValue;
4use rbdc::json::Json;
5use rbdc::Error;
6
7impl Encode for Json {
8 fn encode(self, buf: &mut Vec<u8>) -> Result<usize, Error> {
9 let mut bytes = self.0.into_bytes();
10 if bytes.is_empty() {
11 bytes = "null".to_string().into_bytes();
12 }
13 let len = bytes.len();
14 buf.put_bytes_lenenc(bytes);
15 Ok(len)
16 }
17}
18
19impl Decode for Json {
20 fn decode(value: MySqlValue) -> Result<Self, Error> {
21 Ok(Self(value.as_str().unwrap_or("null").to_string()))
22 }
23}
24
25pub fn encode_json(arg: rbs::Value, buf: &mut Vec<u8>) -> Result<usize, Error> {
26 let bytes = arg.to_string().into_bytes();
27 let len = bytes.len();
28 buf.put_bytes_lenenc(bytes);
29 Ok(len)
30}
31
32pub fn decode_json(value: MySqlValue) -> Result<rbs::Value, Error> {
33 let v = value.as_str().unwrap_or("null").to_string();
34 Ok(serde_json::from_str(&v).map_err(|e| Error::from(e.to_string()))?)
35}