/// For serializing and deserializing Pubkey pub(crate) mod serde_pubkey { use borsh::BorshDeserialize; use serde::{Deserialize, Deserializer, Serializer}; use solana_sdk::pubkey::Pubkey; use std::str::FromStr; #[derive(Deserialize)] #[serde(untagged)] enum RawPubkey { String(String), Bytes(Vec), } pub fn serialize(k: &Pubkey, ser: S) -> Result { ser.serialize_str(&k.to_string()) } pub fn deserialize<'de, D: Deserializer<'de>>(de: D) -> Result { match RawPubkey::deserialize(de)? { RawPubkey::String(s) => Pubkey::from_str(&s).map_err(serde::de::Error::custom), RawPubkey::Bytes(b) => Pubkey::try_from_slice(&b).map_err(serde::de::Error::custom), } } } /// For serializing and deserializing `Option` pub(crate) mod serde_option_pubkey { use borsh::BorshDeserialize; use serde::{Deserialize, Deserializer, Serializer}; use solana_sdk::pubkey::Pubkey; use std::str::FromStr; #[derive(Deserialize)] #[serde(untagged)] enum RawPubkey { String(String), Bytes(Vec), } pub fn serialize(k: &Option, ser: S) -> Result { ser.serialize_str(&k.map(|k| k.to_string()).unwrap_or_default()) } pub fn deserialize<'de, D: Deserializer<'de>>(de: D) -> Result, D::Error> { match Option::::deserialize(de)? { Some(RawPubkey::String(s)) => { if s.is_empty() { Ok(None) } else { Pubkey::from_str(&s) .map_err(serde::de::Error::custom) .map(Some) } } Some(RawPubkey::Bytes(b)) => { if b.is_empty() { Ok(None) } else { Pubkey::try_from_slice(&b) .map_err(serde::de::Error::custom) .map(Some) } } None => Ok(None), } } }