Skip to main content

encode_json_data

Function encode_json_data 

Source
pub fn encode_json_data<T: Serialize>(data: &T) -> Result<Vec<u8>, String>
Expand description

Encodes a Rust struct into JSON bytes.

Serializes the provided data into a byte vector using Serde and serde_json. Supports Juno’s extended JSON conventions for IC types such as Principal, u64 (bigint), and Vec<u8> (Uint8Array), which are encoded using @dfinity/utils compatible markers.

§Parameters

  • data: A reference to the Rust data structure to be serialized.

§Returns

  • Ok(Vec<u8>): A byte vector containing the JSON-encoded data.
  • Err(String): An error string if serialization fails.

§Example

#[derive(Serialize)]
struct MyData {
    name: String,
}

let bytes = encode_json_data(&my_data)?;