Skip to main content

decode_json_data

Function decode_json_data 

Source
pub fn decode_json_data<T: for<'a> Deserialize<'a>>(
    data: &[u8],
) -> Result<T, String>
Expand description

Decodes JSON data into a Rust struct.

Deserializes a byte slice into the specified type 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 byte slice (&[u8]) containing the JSON-encoded data.

§Returns

  • Ok(T): Successfully deserialized data of type T.
  • Err(String): An error string if deserialization fails.

§Example

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

let data: MyData = decode_json_data(bytes)?;