2025-08-09 17:22:28 +03:00
2025-08-06 16:22:19 +03:00
2025-08-06 16:22:19 +03:00
2025-08-08 12:33:05 +03:00
2025-08-09 17:22:28 +03:00
2025-08-06 16:22:19 +03:00

serialize-js

import * as s from '@sek1ro/serialize'

const User = s.Struct({
  id: s.Int(32, 'unsigned'),
  name: String,
  age: Number,
  array: [Array, Boolean],
  raw_data: DataView,
})

const user = {
  id: 1,
  name: 'john',
  age: 20,
  array: [true, false],
  raw_data: s.filledDataView([0x00, 0x00, 0x00, 0xff]),
}

const dv = s.sizedDataView(sizeof(user, User))
s.serialize(dv, user, User)

const user_copy = s.parse(dv, User)

Contents

Types

ES types

  • Boolean
  • Number
  • String (headless)
  • Array (headless)
  • DataView (headless)

Built-in types

  • Int(bits: 8 | 16 | 32, sign: 'signed' | 'unsigned')
  • ConstString(size: number)
  • ConstArray(size: number)
  • ConstDataView(byte_size: number)
  • Struct(type_obj: Record<string, SerializableType | SerializableType[]>)
  • Sequence(types: (SerializableType | SerializableType[])[])

Headless

If type has variable size in bytes, if sizeof type different for specific object, it's called headless.

const array = [true, false]

sizeof(ConstArray(2), Boolean) // 2
sizeof(array, ConstArray(2), Boolean) // 2

sizeof(Array, Boolean) // Error: unknown sizeof Array, Boolean

sizeof(array, Array, Boolean) // 2

Library has function isHeadless(...types: SerializableType[]) to check this. It's used in Structure, if type is headless, in head of structure it's stored as uint32 offset to beginning of type's body, if type isn't headless, it's stored directly in head of structure

Description
No description provided
Readme 406 KiB
Languages
JavaScript 100%