use neo_types::{NeoByteString, NeoStruct, NeoValue};
use serde::{Deserialize, Serialize};
use serde_json::{self, Value as JsonValue};
pub fn bytes_to_json<T: for<'de> Deserialize<'de>>(bytes: &NeoByteString) -> Option<T> {
serde_json::from_slice(bytes.as_slice()).ok()
}
pub fn json_to_bytes<T: Serialize>(value: &T) -> NeoByteString {
match serde_json::to_vec(value) {
Ok(data) => NeoByteString::from_slice(&data),
Err(_) => NeoByteString::new(Vec::new()),
}
}
pub fn storage_struct(key: &NeoByteString, value: &NeoByteString) -> NeoValue {
let mut entry = NeoStruct::new();
entry.set_field("key", NeoValue::from(key.clone()));
entry.set_field("value", NeoValue::from(value.clone()));
NeoValue::from(entry)
}
pub fn json_from_value(value: &NeoValue) -> Option<JsonValue> {
value
.as_byte_string()
.and_then(|bytes| serde_json::from_slice(bytes.as_slice()).ok())
}