dpp/document/v0/
json_conversion.rs1use crate::document::fields::property_names;
2use crate::document::serialization_traits::{
3 DocumentJsonMethodsV0, DocumentPlatformValueMethodsV0,
4};
5use crate::document::DocumentV0;
6use crate::util::json_value::JsonValueExt;
7use crate::ProtocolError;
8use platform_value::{Identifier, Value};
9use platform_version::version::PlatformVersion;
10use serde::Deserialize;
11use serde_json::{json, Value as JsonValue};
12use std::convert::TryInto;
13
14impl DocumentJsonMethodsV0<'_> for DocumentV0 {
15 fn to_json_with_identifiers_using_bytes(
16 &self,
17 _platform_version: &PlatformVersion,
18 ) -> Result<JsonValue, ProtocolError> {
19 let mut value = json!({
20 property_names::ID: self.id,
21 property_names::OWNER_ID: self.owner_id,
22 });
23 let value_mut = value.as_object_mut().unwrap();
24 if let Some(created_at) = self.created_at {
25 value_mut.insert(
26 property_names::CREATED_AT.to_string(),
27 JsonValue::Number(created_at.into()),
28 );
29 }
30 if let Some(updated_at) = self.updated_at {
31 value_mut.insert(
32 property_names::UPDATED_AT.to_string(),
33 JsonValue::Number(updated_at.into()),
34 );
35 }
36 if let Some(created_at_block_height) = self.created_at_block_height {
37 value_mut.insert(
38 property_names::CREATED_AT_BLOCK_HEIGHT.to_string(),
39 JsonValue::Number(created_at_block_height.into()),
40 );
41 }
42
43 if let Some(updated_at_block_height) = self.updated_at_block_height {
44 value_mut.insert(
45 property_names::UPDATED_AT_BLOCK_HEIGHT.to_string(),
46 JsonValue::Number(updated_at_block_height.into()),
47 );
48 }
49
50 if let Some(created_at_core_block_height) = self.created_at_core_block_height {
51 value_mut.insert(
52 property_names::CREATED_AT_CORE_BLOCK_HEIGHT.to_string(),
53 JsonValue::Number(created_at_core_block_height.into()),
54 );
55 }
56
57 if let Some(updated_at_core_block_height) = self.updated_at_core_block_height {
58 value_mut.insert(
59 property_names::UPDATED_AT_CORE_BLOCK_HEIGHT.to_string(),
60 JsonValue::Number(updated_at_core_block_height.into()),
61 );
62 }
63 if let Some(transferred_at) = self.transferred_at {
64 value_mut.insert(
65 property_names::TRANSFERRED_AT.to_string(),
66 JsonValue::Number(transferred_at.into()),
67 );
68 }
69 if let Some(transferred_at_block_height) = self.transferred_at_block_height {
70 value_mut.insert(
71 property_names::TRANSFERRED_AT_BLOCK_HEIGHT.to_string(),
72 JsonValue::Number(transferred_at_block_height.into()),
73 );
74 }
75 if let Some(transferred_at_core_block_height) = self.transferred_at_core_block_height {
76 value_mut.insert(
77 property_names::TRANSFERRED_AT_CORE_BLOCK_HEIGHT.to_string(),
78 JsonValue::Number(transferred_at_core_block_height.into()),
79 );
80 }
81 if let Some(creator_id) = self.creator_id {
82 value_mut.insert(property_names::CREATOR_ID.to_string(), json!(creator_id));
83 }
84 if let Some(revision) = self.revision {
85 value_mut.insert(
86 property_names::REVISION.to_string(),
87 JsonValue::Number(revision.into()),
88 );
89 }
90
91 self.properties
92 .iter()
93 .try_for_each(|(key, property_value)| {
94 let serde_value: JsonValue = property_value.try_to_validating_json()?;
95 value_mut.insert(key.to_string(), serde_value);
96 Ok::<(), ProtocolError>(())
97 })?;
98
99 Ok(value)
100 }
101
102 fn to_json(&self, _platform_version: &PlatformVersion) -> Result<JsonValue, ProtocolError> {
103 self.to_object()
104 .map(|v| v.try_into().map_err(ProtocolError::ValueError))?
105 }
106
107 fn from_json_value<S, E>(
108 mut document_value: JsonValue,
109 _platform_version: &PlatformVersion,
110 ) -> Result<Self, ProtocolError>
111 where
112 for<'de> S: Deserialize<'de> + TryInto<Identifier, Error = E>,
113 E: Into<ProtocolError>,
114 {
115 let mut document = Self {
116 ..Default::default()
117 };
118
119 if let Ok(value) = document_value.remove(property_names::ID) {
120 if !value.is_null() {
121 let data: S = serde_json::from_value(value)?;
122 document.id = data.try_into().map_err(Into::into)?;
123 }
124 }
125 if let Ok(value) = document_value.remove(property_names::OWNER_ID) {
126 if !value.is_null() {
127 let data: S = serde_json::from_value(value)?;
128 document.owner_id = data.try_into().map_err(Into::into)?;
129 }
130 }
131 if let Ok(value) = document_value.remove(property_names::REVISION) {
132 document.revision = serde_json::from_value(value)?
133 }
134 if let Ok(value) = document_value.remove(property_names::CREATED_AT) {
135 document.created_at = serde_json::from_value(value)?
136 }
137 if let Ok(value) = document_value.remove(property_names::UPDATED_AT) {
138 document.updated_at = serde_json::from_value(value)?
139 }
140 if let Ok(value) = document_value.remove(property_names::CREATED_AT_BLOCK_HEIGHT) {
141 document.created_at_block_height = serde_json::from_value(value)?;
142 }
143 if let Ok(value) = document_value.remove(property_names::UPDATED_AT_BLOCK_HEIGHT) {
144 document.updated_at_block_height = serde_json::from_value(value)?;
145 }
146 if let Ok(value) = document_value.remove(property_names::CREATED_AT_CORE_BLOCK_HEIGHT) {
147 document.created_at_core_block_height = serde_json::from_value(value)?;
148 }
149 if let Ok(value) = document_value.remove(property_names::UPDATED_AT_CORE_BLOCK_HEIGHT) {
150 document.updated_at_core_block_height = serde_json::from_value(value)?;
151 }
152 if let Ok(value) = document_value.remove(property_names::TRANSFERRED_AT) {
153 document.transferred_at = serde_json::from_value(value)?;
154 }
155 if let Ok(value) = document_value.remove(property_names::TRANSFERRED_AT_BLOCK_HEIGHT) {
156 document.transferred_at_block_height = serde_json::from_value(value)?;
157 }
158 if let Ok(value) = document_value.remove(property_names::TRANSFERRED_AT_CORE_BLOCK_HEIGHT) {
159 document.transferred_at_core_block_height = serde_json::from_value(value)?;
160 }
161 if let Ok(value) = document_value.remove(property_names::CREATOR_ID) {
162 if !value.is_null() {
163 let data: S = serde_json::from_value(value)?;
164 document.creator_id = Some(data.try_into().map_err(Into::into)?);
165 }
166 }
167
168 let platform_value: Value = document_value.into();
169
170 document.properties = platform_value
171 .into_btree_string_map()
172 .map_err(ProtocolError::ValueError)?;
173 Ok(document)
174 }
175}