#wasmflow #json #codec #messagepack #deserialize-json #serde #serde-json

wasmflow-codec

JSON, Messagepack, & Raw serde value encoding/decoding library

3 releases

0.10.0 Jul 29, 2022
0.10.0-beta.4 Jun 30, 2022

#918 in WebAssembly

Download history 91/week @ 2024-03-31 22/week @ 2024-04-07 25/week @ 2024-04-14 51/week @ 2024-04-21 38/week @ 2024-04-28 22/week @ 2024-05-05 28/week @ 2024-05-12 18/week @ 2024-05-19 32/week @ 2024-05-26 23/week @ 2024-06-02 14/week @ 2024-06-09 17/week @ 2024-06-16 31/week @ 2024-06-23 6/week @ 2024-06-30 8/week @ 2024-07-07 34/week @ 2024-07-14

82 downloads per month
Used in 10 crates (6 directly)

Apache-2.0

13KB
187 lines

Maintenance

The Wasmflow Codec crate contains the serialization and deserialization functions and structures for communicating in and out of Wasmflow Components.

JSON

Serializes to a serde_json::Value which can be printed as a JSON string.

json::serialize

use wasmflow_codec::{json, Error};
use serde::{Serialize, Deserialize};

pub fn main() -> Result<(), Error> {

  #[derive(Serialize, Deserialize)]
  struct Point {
    x: i32,
    y: i32,
  }

  let point = Point { x: 200, y: 193 };

  let value = json::serialize(&point)?;
  println!("{:?}", value);

  assert_eq!(value, r#"{"x":200,"y":193}"#);
  Ok(())
}

json::deserialize

use wasmflow_codec::{json, Error};
use serde::{Serialize, Deserialize};

pub fn main() -> Result<(), Error> {
  #[derive(Serialize, Deserialize, Debug, PartialEq)]
  struct Point {
    x: i32,
    y: i32,
  }

  let json = r#"{"x":200,"y":193}"#;

  let instance: Point = json::deserialize(&json)?;

  assert_eq!(instance, Point { x: 200, y: 193 });
  Ok(())
}

MessagePack

Serializes to a MessagePack [Vec].

messagepack::serialize

use wasmflow_codec::{messagepack, Error};
use serde::{Serialize, Deserialize};

pub fn main() -> Result<(), Error> {
  #[derive(Serialize, Deserialize)]
  struct Point {
    x: i32,
    y: i32,
  }

  let point = Point { x: 200, y: 193 };

  let value = messagepack::serialize(&point)?;
  println!("{:?}", value);

  let expected: Vec<u8> = vec![130, 161, 120, 204, 200, 161, 121, 204, 193];
  assert_eq!(value, expected);
  Ok(())
}

messagepack::deserialize

use wasmflow_codec::{messagepack, Error};
use serde::{Serialize, Deserialize};

pub fn main() -> Result<(), Error> {
  #[derive(Serialize, Deserialize, Debug, PartialEq)]
  struct Point {
    x: i32,
    y: i32,
  }

  let slice = vec![146, 204, 200, 204, 193];

  let instance: Point = messagepack::deserialize(&slice)?;

  assert_eq!(instance, Point { x: 200, y: 193 });
  Ok(())
}

Raw

The [raw] module uses [serde_value] as an intermediary format to pass around.

use wasmflow_codec::{raw, Error};
use serde::{Serialize, Deserialize};

pub fn main() -> Result<(), Error> {
  #[derive(Serialize, Deserialize, Debug, PartialEq)]
  struct Point {
    x: i32,
    y: i32,
  }

  let point = Point { x: 200, y: 193 };

  let value = raw::serialize(&point)?;
  let instance: Point = raw::deserialize(value)?;

  assert_eq!(instance, Point { x: 200, y: 193 });
  Ok(())
}

Dependencies

~1.2–2MB
~44K SLoC