dzgt_utils/
json_object.rs

1use std::fmt::Debug;
2use std::marker::PhantomData;
3use cyfs_base::*;
4use serde::{Serialize, Deserialize};
5use std::ops::{Deref, DerefMut};
6use crate::into_cyfs_err;
7
8pub trait JSONObjType: 'static + RawEncode + Clone + Debug + Send + Sync {
9    fn obj_type() -> u16;
10}
11
12#[derive(Clone, Debug, RawEncode, RawDecode)]
13pub struct JSONDescContent<T: JSONObjType> {
14    obj_type: u16,
15    content_hash: HashValue,
16    #[cyfs(skip)]
17    _p: PhantomData<T>,
18}
19
20impl<T: JSONObjType> DescContent for JSONDescContent<T> {
21    fn obj_type() -> u16 {
22        T::obj_type()
23    }
24
25    type OwnerType = Option<ObjectId>;
26    type AreaType = SubDescNone;
27    type AuthorType = SubDescNone;
28    type PublicKeyType = SubDescNone;
29}
30
31#[derive(Clone, Debug, RawEncode, RawDecode)]
32pub struct JSONBodyContent(pub Vec<u8>);
33
34impl Deref for JSONBodyContent {
35    type Target = Vec<u8>;
36
37    fn deref(&self) -> &Self::Target {
38        &self.0
39    }
40}
41
42impl DerefMut for JSONBodyContent {
43    fn deref_mut(&mut self) -> &mut Self::Target {
44        &mut self.0
45    }
46}
47
48impl BodyContent for JSONBodyContent {
49
50    fn version(&self) -> u8 {
51        0
52    }
53
54    fn format(&self) -> u8 {
55        OBJECT_CONTENT_CODEC_FORMAT_RAW
56    }
57}
58
59pub type JSONObjectType<T> = NamedObjType<JSONDescContent<T>, JSONBodyContent>;
60pub type JSONObjectBuilder<T> = NamedObjectBuilder<JSONDescContent<T>, JSONBodyContent>;
61pub type JSONObject<T> = NamedObjectBase<JSONObjectType<T>>;
62
63pub trait DSGJSON<T: Serialize + for<'a> Deserialize<'a>, A: JSONObjType> {
64    fn new(dec_id: ObjectId, owner_id: ObjectId, obj_type: u16, obj: &T) -> BuckyResult<JSONObject<A>>;
65    fn get(&self) -> BuckyResult<T>;
66}
67
68pub trait DSGJSONType {
69    fn get_json_obj_type(&self) -> u16;
70}
71
72impl<T: JSONObjType> DSGJSONType for NamedObjectBase<JSONObjectType<T>> {
73    fn get_json_obj_type(&self) -> u16 {
74        self.desc().content().obj_type
75    }
76}
77
78pub trait Verifier {
79    fn verify_body(&self) -> bool;
80}
81
82impl <T: Serialize + for<'a> Deserialize<'a>, A: JSONObjType> DSGJSON<T, A> for NamedObjectBase<JSONObjectType<A>> {
83    fn new(dec_id: ObjectId, owner_id: ObjectId, obj_type: u16, obj: &T) -> BuckyResult<JSONObject<A>> {
84        let body = JSONBodyContent(serde_json::to_vec(obj).map_err(into_cyfs_err!(BuckyErrorCode::Failed, "encode failed"))?);
85
86        let desc = JSONDescContent { obj_type, content_hash: hash_data(body.as_slice()), _p: Default::default() };
87
88        Ok(JSONObjectBuilder::new(desc, body).owner(owner_id).dec_id(dec_id).build())
89    }
90
91    fn get(&self) -> BuckyResult<T> {
92        let body = self.body().as_ref().unwrap().content();
93        serde_json::from_slice(body.as_ref()).map_err(into_cyfs_err!(BuckyErrorCode::Failed, "parse {}", String::from_utf8_lossy(body.as_slice()).to_string()))
94    }
95
96}
97
98impl<A: JSONObjType> Verifier for NamedObjectBase<JSONObjectType<A>> {
99    fn verify_body(&self) -> bool {
100        if self.body().is_none() {
101            return false;
102        }
103
104        let body = self.body().as_ref().unwrap().content();
105        if hash_data(body.as_slice()) == self.desc().content().content_hash {
106            true
107        } else {
108            false
109        }
110    }
111}