dpp/identity/identity_public_key/v0/conversion/
json.rs

1use crate::identity::identity_public_key::conversion::json::IdentityPublicKeyJsonConversionMethodsV0;
2use crate::identity::identity_public_key::conversion::platform_value::IdentityPublicKeyPlatformValueConversionMethodsV0;
3use crate::identity::identity_public_key::fields::BINARY_DATA_FIELDS;
4use crate::identity::identity_public_key::v0::IdentityPublicKeyV0;
5use crate::version::PlatformVersion;
6use crate::ProtocolError;
7use platform_value::{ReplacementType, Value};
8use serde_json::Value as JsonValue;
9use std::convert::{TryFrom, TryInto};
10
11impl IdentityPublicKeyJsonConversionMethodsV0 for IdentityPublicKeyV0 {
12    fn to_json_object(&self) -> Result<JsonValue, ProtocolError> {
13        self.to_cleaned_object()?
14            .try_into_validating_json()
15            .map_err(ProtocolError::ValueError)
16    }
17
18    fn to_json(&self) -> Result<JsonValue, ProtocolError> {
19        self.to_cleaned_object()?
20            .try_into()
21            .map_err(ProtocolError::ValueError)
22    }
23
24    fn from_json_object(
25        raw_object: JsonValue,
26        platform_version: &PlatformVersion,
27    ) -> Result<Self, ProtocolError> {
28        let mut value: Value = raw_object.into();
29        value.replace_at_paths(BINARY_DATA_FIELDS, ReplacementType::BinaryBytes)?;
30        Self::from_object(value, platform_version)
31    }
32}
33
34impl TryFrom<&str> for IdentityPublicKeyV0 {
35    type Error = ProtocolError;
36
37    fn try_from(value: &str) -> Result<Self, Self::Error> {
38        let mut platform_value: Value = serde_json::from_str::<JsonValue>(value)
39            .map_err(|e| ProtocolError::StringDecodeError(e.to_string()))?
40            .into();
41        platform_value.replace_at_paths(BINARY_DATA_FIELDS, ReplacementType::BinaryBytes)?;
42        platform_value.try_into().map_err(ProtocolError::ValueError)
43    }
44}