wasmflow_codec/
json.rs

1use serde::{Deserialize, Serialize};
2
3use crate::error::CodecError;
4use crate::Result;
5
6#[doc(hidden)]
7pub fn to_value<T>(item: &T) -> std::result::Result<serde_json::Value, serde_json::Error>
8where
9  T: Serialize,
10{
11  serde_json::to_value(item)
12}
13
14#[doc(hidden)]
15pub fn json_serialize<T>(item: &T) -> std::result::Result<String, serde_json::Error>
16where
17  T: Serialize,
18{
19  serde_json::to_string(item)
20}
21
22/// The standard function for serializing codec structs into a format that can be.
23/// used for message exchange between actor and host. Use of any other function to.
24/// serialize could result in breaking incompatibilities.
25pub fn serialize<T>(item: &T) -> Result<String>
26where
27  T: Serialize,
28{
29  json_serialize(item).map_err(CodecError::JsonSerializationError)
30}
31
32#[doc(hidden)]
33pub fn json_deserialize<'de, T: Deserialize<'de>>(
34  json: &'de str,
35) -> std::result::Result<T, serde_json::Error> {
36  serde_json::from_str(json)
37}
38
39/// The standard function for de-serializing codec structs from a format suitable.
40/// for message exchange between actor and host. Use of any other function to.
41/// deserialize could result in breaking incompatibilities.
42pub fn deserialize<'de, T: Deserialize<'de>>(json: &'de str) -> Result<T> {
43  json_deserialize(json).map_err(CodecError::JsonDeserializationError)
44}