serialize the Typst objects into bytes and deserialize the bytes into Typst objects
Reason
I want to interactive between the wasm and typst. But I found that the input arguments and output argument are all bytes. It is not convenient for me to use WASM. So I designed the serialization protocol and implemented this serialization module for reference.
Although there have been some serialization APIs like cbor, yaml, json and others, this is a good learning material and a good example to show the abilities of Typst.
Example
Have a look at the example here.
Usage
Simply import 2 functions : serialize, deserialize.
And enjoy it
#import "@preview/yats:0.1.0": serialize, deserialize
#{
let obj = (
name : "0warning0error",
age : 100,
height : 1.8,
birthday : datetime(year : 1998,month : 7,day:8)
)
deserialize(serialize(obj))
}
Supported Types
nonebooltype: type is a typeintfloat: (implemented in string, for convenience)datetime: only supportyear,month,day;hour,minute,second; both combined.durationbytesstringregex: (dealing with it is a little tricky)array: the element in it can be anything listed.dictionary: the value in it can be anything listed.
Yats function
#let serialize(
data : any
) = { .. }
Arguments:
data: [any] — Any supported object .
Return
The serialized bytes.
#let deserialize(
data : array
) = { .. }
Arguments:
data: [bytes] — serialized objects represented by bytes .
Return
binary array. (the first one is the object deserialized, the second one is the valid length of the bytes)
Potential Problems and limitation
-
Some problem can be caused by changes of
repr. For example, the serialization ofregexrelies on thereprofregex. And there are no method to directly catch the innerstring. -
Because of lack of time, only basic types are supported. But more types can be supported in Typst.
License
This project is licensed under the Apache 2.0 License.