pub fn decode_plutus_datum_to_json_value(
datum: &PlutusData,
schema: PlutusDatumSchema
) -> Result<Value, JsError>
Examples found in repository?
src/plutus.rs (line 1198)
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
pub fn decode_plutus_datum_to_json_str(
datum: &PlutusData,
schema: PlutusDatumSchema,
) -> Result<String, JsError> {
let value = decode_plutus_datum_to_json_value(datum, schema)?;
serde_json::to_string(&value).map_err(|e| JsError::from_str(&e.to_string()))
}
//TODO: move it to deserialize impl
pub fn decode_plutus_datum_to_json_value(
datum: &PlutusData,
schema: PlutusDatumSchema,
) -> Result<serde_json::Value, JsError> {
use serde_json::Value;
let (type_tag, json_value) = match &datum.datum {
PlutusDataEnum::ConstrPlutusData(constr) => {
let mut obj = serde_json::map::Map::with_capacity(2);
obj.insert(
String::from("constructor"),
Value::from(from_bignum(&constr.alternative))
);
let mut fields = Vec::new();
for field in constr.data.elems.iter() {
fields.push(decode_plutus_datum_to_json_value(field, schema)?);
}
obj.insert(
String::from("fields"),
Value::from(fields)
);
(None, Value::from(obj))
},
PlutusDataEnum::Map(map) => match schema {
PlutusDatumSchema::BasicConversions => (None, Value::from(map.0.iter().map(|(key, value)| {
let json_key: String = match &key.datum {
PlutusDataEnum::ConstrPlutusData(_) => Err(JsError::from_str("plutus data constructors are not allowed as keys in this schema. Use DetailedSchema.")),
PlutusDataEnum::Map(_) => Err(JsError::from_str("plutus maps are not allowed as keys in this schema. Use DetailedSchema.")),
PlutusDataEnum::List(_) => Err(JsError::from_str("plutus lists are not allowed as keys in this schema. Use DetailedSchema.")),
PlutusDataEnum::Integer(x) => Ok(x.to_str()),
PlutusDataEnum::Bytes(bytes) => String::from_utf8(bytes.clone()).or_else(|_err| Ok(format!("0x{}", hex::encode(bytes))))
}?;
let json_value = decode_plutus_datum_to_json_value(value, schema)?;
Ok((json_key, Value::from(json_value)))
}).collect::<Result<serde_json::map::Map<String, Value>, JsError>>()?)),
PlutusDatumSchema::DetailedSchema => (Some("map"), Value::from(map.0.iter().map(|(key, value)| {
let k = decode_plutus_datum_to_json_value(key, schema)?;
let v = decode_plutus_datum_to_json_value(value, schema)?;
let mut kv_obj = serde_json::map::Map::with_capacity(2);
kv_obj.insert(String::from("k"), k);
kv_obj.insert(String::from("v"), v);
Ok(Value::from(kv_obj))
}).collect::<Result<Vec<_>, JsError>>()?)),
},
PlutusDataEnum::List(list) => {
let mut elems = Vec::new();
for elem in list.elems.iter() {
elems.push(decode_plutus_datum_to_json_value(elem, schema)?);
}
(Some("list"), Value::from(elems))
},
PlutusDataEnum::Integer(bigint) => (
Some("int"),
bigint
.as_int()
.as_ref()
.map(|int| if int.0 >= 0 { Value::from(int.0 as u64) } else { Value::from(int.0 as i64) })
.ok_or_else(|| JsError::from_str(&format!("Integer {} too big for our JSON support", bigint.to_str())))?
),
PlutusDataEnum::Bytes(bytes) => (Some("bytes"), Value::from(match schema {
PlutusDatumSchema::BasicConversions => {
// cardano-cli converts to a string only if bytes are utf8 and all characters are printable
String::from_utf8(bytes.clone())
.ok()
.filter(|utf8| utf8.chars().all(|c| !c.is_control()))
// otherwise we hex-encode the bytes with a 0x prefix
.unwrap_or_else(|| format!("0x{}", hex::encode(bytes)))
},
PlutusDatumSchema::DetailedSchema => hex::encode(bytes),
})),
};
if type_tag.is_none() || schema != PlutusDatumSchema::DetailedSchema {
Ok(json_value)
} else {
let mut wrapper = serde_json::map::Map::with_capacity(1);
wrapper.insert(String::from(type_tag.unwrap()), json_value);
Ok(Value::from(wrapper))
}
}