# Functions
Deserialize deserializes a single item from bytes.
DeserializeOption deserializes an optional value
# Under the hood, this is represented as a 0 or 1 length array
Here's an example for handling an optional value:
// For a Some(10) value bytes == []byte{0x01, 0x0A} des := NewDeserializer(bytes) output := DeserializeOption(des, nil, func(des *Deserializer, out *uint8) { out = des.U8() }) // output == &10
// For a None value bytes2 == []byte{0x00} des2 := NewDeserializer(bytes2) output := DeserializeOption(des2, nil, func(des *Deserializer, out *uint8) { out = des.U8() }) // output == nil.
DeserializeSequence deserializes an Unmarshaler implementation array
This lets you deserialize a whole sequence of [Unmarshaler], and will fail if any member fails.
DeserializeSequenceWithFunction deserializes any array with the given function
This lets you deserialize a whole sequence of any type, and will fail if any member fails.
NewDeserializer creates a new Deserializer from a byte array.
Serialize serializes a single item
type MyStruct struct { num uint64 }
func (str *MyStruct) MarshalBCS(ser *Serialize) { ser.U64(num) }
struct := &MyStruct{ num: 100 } bytes, _ := Serialize(struct).
SerializeBool Serializes a single boolean
bytes, _ := SerializeBool(true).
SerializeBytes Serializes a single byte array
input := []byte{0x1, 0x2} bytes, _ := SerializeBytes(input).
SerializeOption serializes an optional value
# Under the hood, this is represented as a 0 or 1 length array
Here's an example for handling an optional value:
// For a Some(10) value input := uint8(10) ser := &Serializer{} bytes, _ := SerializeOption(ser, &input, func(ser *Serializer, item uint8) { ser.U8(item) }) // bytes == []byte{0x01,0x0A}
// For a None value ser2 := &Serializer{} bytes2, _ := SerializeOption(ser2, nil, func(ser *Serializer, item uint8) { ser.U8(item) }) // bytes2 == []byte{0x00}.
SerializeSequence serializes a sequence of [Marshaler] implemented types.
SerializeSequenceOnly serializes a sequence into a single value using [SerializeSequence]
type MyStruct struct { num uint64 }
func (str *MyStruct) MarshalBCS(ser *Serialize) { ser.U64(num) }
myArray := []MyStruct{ MyStruct{num: 0}, MyStruct{num: 1}, MyStruct{num: 2}, }
bytes, err := SerializeSequenceOnly(myArray).
SerializeSequenceWithFunction allows custom serialization of a sequence, which can be useful for non-bcs.Struct types
array := []string{"hello", "blockchain"}
SerializeSequenceWithFunction(array, func(ser *Serializer, item string) { ser.WriteString(item) }.
SerializeSingle is a convenience function, to not have to create a serializer to serialize one value
Here's an example for handling a nested byte array
input := [][]byte{[]byte{0x1}, []byte{0x2}} bytes, _ := SerializeSingle(func(ser *Serializer) { ser.Uleb128(len(input)) for _, list := range input { ser.WriteBytes(list) } }).
SerializeU128 Serializes a single uint128
u128 := big.NewInt(1) bytes, _ := SerializeU128(u128).
SerializeU16 Serializes a single uint16
bytes, _ := SerializeU16(uint16(50000)).
SerializeU256 Serializes a single uint256
u256 := big.NewInt(1) bytes, _ := SerializeU256(u256).
SerializeU32 Serializes a single uint32
bytes, _ := SerializeU32(uint32(50000)).
SerializeU64 Serializes a single uint64
bytes, _ := SerializeU64(uint64(20)).
SerializeU8 Serializes a single uint8
bytes, _ := SerializeU8(uint8(200)).
# Structs
Deserializer is a type to deserialize a known set of bytes.
Serializer is a holding type to serialize a set of items into one shared buffer
serializer := &Serializer{} serializer.U64(uint64(10)) serializedBytes := serializer.ToBytes().
# Interfaces
Marshaler is an interface for any type that can be serialized into BCS
It's highly suggested to implement on a pointer to a type, and not the type directly.
Struct is an interface for an on-chain type.
Unmarshaler is an interface for any type that can be deserialized from BCS
It's highly suggested to implement on a pointer to a type, and not the type directly.