Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TLS-20 Add serializer functions to JS/TS library #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,63 @@ npm install @timeleap/sia
```python
import { Sia } from "@timeleap/sia";

type Person = {
name?: string;
age?: number;
};

const sia = new Sia();
const person: Person = { name: "Pouya", age: 33 };

const payload = sia
.seek(0)
.addAscii(person.name ?? "")
.addUInt8(person.age ?? 0)
.toUint8ArrayReference();

console.log(payload); // Uint8Array(7) [5, 80, 111, 117, 121, 97, 33]

const desia = new Sia(payload);
const deserialized: Person = {
name: desia.readAscii(),
age: desia.readUInt8(),
};

console.log(deserialized); // { name: 'Pouya', age: 33 }
```

## Serializers

Sia provides a set of serializers for each primitive type. They are most useful for cases where you're adding an array of values. Instead of writing the function yourself, just use the exported utility functions.

```typescript
import { Sia, serializeString8ArrayItem } from "@timeleap/sia";

const sia = new Sia();

sia
.addString8("Hello")
.addUint8(25)
.addAscii("World");
sia.addArray8(["Hello", "World"], serializeString8ArrayItem);
```

The `serializeString8ArrayItem` runs for each item in the array and adds the item to the Sia content.

## Deserializers

For the opposite scenario, where you want to read an array of values from the Sia content, you can use the `readArray8` method in combination with the deserializers.

```typescript
import {
Sia,
serializeString8ArrayItem,
readString8ArrayItem,
} from "@timeleap/sia";

const sia = new Sia();

sia.addArray8(["Hello", "World"], serializeString8ArrayItem);

const desia = new Sia().setContent(sia.content);

const deserialized = desia.readArray8(readString8ArrayItem);

console.log(sia.content);
console.log(deserialized); // ["Hello", "World"]
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@timeleap/sia",
"version": "2.1.0",
"version": "2.2.0",
"description": "",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Buffer } from "./buffer.js";
import { pack, unpack } from "utfz-lib";
import { asciiToUint8Array, uint8ArrayToAscii } from "./ascii.js";
import { Buffer } from "./buffer.js";

const GLOBAL_SHARED_UNSAFE_BUFFER = new Uint8Array(32 * 1024 * 1024);

Expand Down Expand Up @@ -403,3 +403,5 @@ export class Sia extends Buffer {
return this.readArray(length, fn);
}
}

export * from "./serializers.js";
147 changes: 147 additions & 0 deletions src/serializers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { Sia } from "./index.js";

// Serializers
export function serializeInt8ArrayItem(sia: Sia, value: number) {
return sia.addInt8(value);
}

export function serializeInt16ArrayItem(sia: Sia, value: number) {
return sia.addInt16(value);
}

export function serializeInt32ArrayItem(sia: Sia, value: number) {
return sia.addInt32(value);
}

export function serializeInt64ArrayItem(sia: Sia, value: number) {
return sia.addInt64(value);
}

export function serializeUInt8ArrayItem(sia: Sia, value: number) {
return sia.addUInt8(value);
}

export function serializeUInt16ArrayItem(sia: Sia, value: number) {
return sia.addUInt16(value);
}

export function serializeUInt32ArrayItem(sia: Sia, value: number) {
return sia.addUInt32(value);
}

export function serializeUInt64ArrayItem(sia: Sia, value: number) {
return sia.addUInt64(value);
}

export function serializeString8ArrayItem(sia: Sia, value: string) {
return sia.addString8(value);
}

export function serializeString16ArrayItem(sia: Sia, value: string) {
return sia.addString16(value);
}

export function serializeString32ArrayItem(sia: Sia, value: string) {
return sia.addString32(value);
}

export function serializeString64ArrayItem(sia: Sia, value: string) {
return sia.addString64(value);
}

export function serializeByteArray8ArrayItem(sia: Sia, value: Uint8Array) {
return sia.addByteArray8(value);
}

export function serializeByteArray16ArrayItem(sia: Sia, value: Uint8Array) {
return sia.addByteArray16(value);
}

export function serializeByteArray32ArrayItem(sia: Sia, value: Uint8Array) {
return sia.addByteArray32(value);
}

export function serializeByteArray64ArrayItem(sia: Sia, value: Uint8Array) {
return sia.addByteArray64(value);
}

export function serializeBoolArrayItem(sia: Sia, value: boolean) {
return sia.addBool(value);
}

export function serializeBigIntArrayItem(sia: Sia, value: bigint) {
return sia.addBigInt(value);
}

// Deserializers
export function deserializeInt8ArrayItem(sia: Sia) {
return sia.readInt8();
}

export function deserializeInt16ArrayItem(sia: Sia) {
return sia.readInt16();
}

export function deserializeInt32ArrayItem(sia: Sia) {
return sia.readInt32();
}

export function deserializeInt64ArrayItem(sia: Sia) {
return sia.readInt64();
}

export function deserializeUInt8ArrayItem(sia: Sia) {
return sia.readUInt8();
}

export function deserializeUInt16ArrayItem(sia: Sia) {
return sia.readUInt16();
}

export function deserializeUInt32ArrayItem(sia: Sia) {
return sia.readUInt32();
}

export function deserializeUInt64ArrayItem(sia: Sia) {
return sia.readUInt64();
}

export function deserializeString8ArrayItem(sia: Sia) {
return sia.readString8();
}

export function deserializeString16ArrayItem(sia: Sia) {
return sia.readString16();
}

export function deserializeString32ArrayItem(sia: Sia) {
return sia.readString32();
}

export function deserializeString64ArrayItem(sia: Sia) {
return sia.readString64();
}

export function deserializeByteArray8ArrayItem(sia: Sia) {
return sia.readByteArray8();
}

export function deserializeByteArray16ArrayItem(sia: Sia) {
return sia.readByteArray16();
}

export function deserializeByteArray32ArrayItem(sia: Sia) {
return sia.readByteArray32();
}

export function deserializeByteArray64ArrayItem(sia: Sia) {
return sia.readByteArray64();
}

export function deserializeBoolArrayItem(sia: Sia) {
return sia.readBool();
}

export function deserializeBigIntArrayItem(sia: Sia) {
return sia.readBigInt();
}
Loading