.github
    workflows
        tests.yml
README.md
as-pect.asconfig.json
as-pect.config.js
asconfig.json
borsh
    README.md
    asconfig.json
    assembly
        buffer.ts
        deserializer.ts
        index.ts
        serializer.ts
        tests
            as-pect.d.ts
            bencoded.spec.ts
    package.json
core
    asconfig.json
    assembly
        as_types.d.ts
        deserializer.ts
        index.ts
        serializer.ts
        utils.ts
    package.json
jest.config.js
json
    README.md
    asconfig.json
    assembly
        deserializer.ts
        index.ts
        serializer.ts
        tests
            as-pect.d.ts
            jvalue.spec.ts
            utils.ts
    package.json
lerna.json
package.json
tests
    README.md
    assembly
        index.ts
    package.json
transform
    dist
        index.d.ts
        index.js
        methodInjector.d.ts
        methodInjector.js
        utils.d.ts
        utils.js
    package.json
    src
        index.ts
        methodInjector.ts
        utils.ts
    tsconfig.json
tsconfig.json

# Tests for serial-as

This folder contains multiple objects covering a wide spectrum of cases in order to test (de)Serializers created with the **serial-as** package.
# Borsh-as

**borsh-as** is an assemblyscript implementation of the [borsh](https://borsh.io) serializer.

## How to use it

Install the package via `yarn add @serial-as/borsh` and add the `--transform @serial-as/transform` flag to your `asc` command.

```ts
import { BorshSerializer, BorshDeserializer } from '@serial-as/borsh'

@serializable
class Pair{
  x: i32 = 0,
  y: i32 = 0
}

let pair: Pair = {x:1, y:2}

// `serialized` is the u8 buffer [0, 0, 0, 1, 0, 0, 0, 2]
let serialized: ArrayBuffer = BorshSerializer.encode(object)  

// `decoded` is the Pair = {x:1, y:2}
let decoded: Pair = BorshDeserializer.decode<Pair>(serialized)  
```

## Limitations

Assemblyscript does not have Enums, nor allows to predefine the length of an array. Because of this, Borsh-as has the following limitations:

- Does **not** deserialize Enums.
- Does **not** deserialize fixed-size arrays.
# Serial-as

**serial-as** simplifies creating serialized encoders/decoders for the assemblyscript language. Furthermore, it readily implements a [JSON](./json), and a [Borsh](./borsh) (de)serializer.

## Installing and Testing
```bash
yarn install
yarn test
```

## What is a Serializer
A (de)Serialized is an object that encodes/decodes `objects` into a predefined type. For example, the JSON serializer encodes `objects` into `strings`, and decodes JSON-encoded `strings` into `objects`.

## How to create a Serializer
Extend the classes `Serializer` and `Deserealizer` exported by `serial-as` and implement their methods. Then, use your Serializer and Deserealizer to instantiate a `Serial` object. For example, to create a serializer that encodes/decodes objects into encoded `strings` you will write the following code.

```ts
import { Serializer, Deserializer } from "@serial-as/core";

class MySerializer extends Serializer<string>{
  /* implement methods to convert types into string */
}

class MyDeserializer extends Deserializer<string>{
  /* implement methods to parse a string into an object */
}

export class MySerial<string, MySerializer, MyDeserializer> { }
```

In order to implement `MyDeserializer` you will need to implement the abstract methods of the `Deserializer` class. Among others, you must implement 
- `decode_bool():bool`
- `abstract decode_map<K, V>():Map<K, V>`
- `decode_string():string`
- `decode_i32():i32`
- etc.

You can use as examples the [Borsh](./borsh) and [JSON](./json) serializers included in this repository. Simply copy one of the folders, and change the methods implemented on them.

## How to use your (De)Serializer
In order for an object to be serializable you need to decorate it with the `@serializable` tag. After this, the object can be used with any Serial object.

```ts
@serializable
class Pair{
  x: i32 = 0,
  y: i32 = 0
}

let pair:Pair = {x:1, y:2}

// Pair can also be used with your implemented serializer
const myserial:MySerial = new MySerial()

// serialize the object into a string
let serialized:string = myserial.serialize(object)  

// decoded must be the Pair = {x:1, y:2}
let decoded:Pair = myserial.deserialize<Pair>(serialized)
```

Once more, you can use the [Borsh](./borsh) and [JSON](./json) implementation included in this repository as a guide.

## Under the Hood
Under the hood, **serial-as** implements a [transform](https://www.assemblyscript.org/transforms.html) to visit `@serializable` objects and encode/decode their fields. To do so, the transform uses the methods implemented in the Serializer/Deserializer classes.
# JSON-as

**json-as** is an assemblyscript implementation of the [JSON](https://json.org) serializer.

## How to use it

Install the package via `yarn add @serial-as/json` and add the `--transform @serial-as/transform` flag to your `asc` command.

```ts
import { stringify, parse } from '@serial-as/json'

@serializable
class Pair{
  x: i32 = 0
  y: i32 = 0
}

let pair:Pair = {x:1, y:2}

// `serialized` is the string "{"x":1,"y":2}"
let serialized: string = stringify(object)  

// `decoded` is the Pair = {x:1, y:2}
let decoded: Pair = parse<Pair>(serialized)  
```
