Skip to main content

encode_json_data_to_string

Function encode_json_data_to_string 

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

Encodes a Rust struct into a JSON string.

Serializes the provided data into a String 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(String): A JSON string containing the serialized data.
  • Err(String): An error string if serialization fails.

§Example

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

let json = encode_json_data_to_string(&my_data)?;