fatline

Quick Start

Get up and running with fatline in 2 minutes

Installation

bun add fatline
pnpm add fatline
npm install fatline
yarn add fatline

Basic Usage

import { serialize, deserialize } from 'fatline'

const data = {
  user: {
    id: '1',
    createdAt: new Date(),
    roles: new Set(['admin', 'user']),
    metadata: new Map([['theme', 'dark']]),
  }
}

// Serialize to JSON string
const json = serialize(data)

// Deserialize back to original types
const back = deserialize(json)

back.user.createdAt instanceof Date    // true
back.user.roles instanceof Set         // true
back.user.metadata instanceof Map      // true

Built-in Types

fatline automatically handles these types:

TypeEncoded As
Datemilliseconds timestamp
BigIntstring
Maparray of [key, value] entries
Setarray of values
RegExp[source, flags]
URLstring
Uint8Arraybase64 string
ArrayBufferbase64 string
undefinedpreserved (unlike JSON)
NaN, Infinity, -Infinitypreserved

Serialization Options

serialize(data, {
  // Paths to exclude from serialization
  exclude: ['user.password', 'internal'],

  // Transform values before serializing
  transform: {
    'user.secretKey': () => '[REDACTED]',
  },

  // How to handle circular references: 'error' | 'null' | 'ref'
  circular: 'error',

  // How to handle unknown types: 'error' | 'skip' | 'null'
  onUnknown: 'error',

  // Human-readable output for debugging
  debug: false,
})

Custom Types

Register your own types for automatic serialization:

import { registerType } from 'fatline'
import Decimal from 'decimal.js'

registerType({
  name: 'Decimal',
  test: (v): v is Decimal => v instanceof Decimal,
  encode: (v) => v.toString(),
  decode: (v) => new Decimal(v),
})

// Now Decimals serialize/deserialize automatically
serialize({ price: new Decimal('19.99') })

See Custom Types for more patterns.

Next Steps

On this page