Making custom adapters

Making custom adapter is very easy. Unless it conforms to SCALE codec principles.

This might be very useful if you want to step out of SCALE's incremental way of serialization.

So let's assume we have a next type:

struct MyCustomType {
    let string: String
    let int: Int32

All custom adapters are subclasses of general ScaleCodecAdapter class.

open class ScaleCodecAdapter<T>: ScaleCodecAdaptable {}

Subclasses need to implement two methods for reading and writing. For example, here is an adapter for MyCustomType.

public final class MyCustomTypeAdapter: ScaleCodecAdapter<MyCustomType> {
    public override func read(_ type: MyCustomType.Type?, from reader: DataReader) throws -> MyCustomType {
    public override func write(value: MyCustomType) throws -> Data {

And then add this to provider like that:

let provider: ScaleCodecAdapterProvider
provider.setAdapter(MyCustomTypeAdapter(), for: MyCustomType.self)

Feel free to play with custom adapters as much as you want.

Last updated