PitchHut logo
An extensible and fast JSON serializer for modern app development.
Pitch

CodableJSON is a high-performance, no-dependency serializer that transforms JavaScript types to and from JSON with ease. Offering a TypeScript-friendly interface, it enables declarative serialization, making it robust yet simple to extend. Leverage its unique features to handle common JavaScript data structures seamlessly.

Description

CodableJSON is an efficient, extensible, and declarative serializer designed to convert virtually any data structure to and from JSON format. By leveraging modern TypeScript features, it enables a high-performance experience while ensuring type safety and security. This library is free of external dependencies, with a compact size of only 7.3KB gzipped, making it an ideal choice for both small and large JavaScript/TypeScript projects.

Key Features

  • Declarative Serialization: Utilize modern decorators to specify what to serialize rather than how to serialize it, streamlining the coding process.
  • Extensible Type Handling: Out-of-the-box support for numerous JavaScript types alongside straightforward extensions for custom types.
  • High Performance: Achieve serialization speeds approximately 3x faster than SuperJSON, supported by detailed performance benchmarks.
  • Type-Safe: Designed with full TypeScript compatibility offering autocompletion and strong type inference.
  • Zero Dependencies: Operates entirely independently without requiring any external libraries.
  • Well-Tested: Comprehensive test coverage verifies the functionality across various edge cases.
  • Framework Agnostic: Compatible with any JavaScript or TypeScript application.
  • Enhanced Security: Integrates built-in security measures, including protection against prototype pollution.

Usage Example

JSON Serialization

Operate with JavaScript types that standard JSON does not natively support:

import { encode, decode } from "codablejson";

const data = {
  date: new Date("2025-01-01"),
  set: new Set(["a", "b", "c"]),
  map: new Map([["key", "value"]]),
  bigint: BigInt("1234567890123456789"),
  regex: /hello/gi,
  url: new URL("https://example.com"),
};

const encoded = encode(data);
const decoded = decode(encoded);
// All data types preserved during encoding and decoding.

Declarative Class Serialization

Easily define classes for serialization while preserving their structure:

import { codableClass, codable, Coder } from "codablejson";

@codableClass("Player")
class Player {
  @codable() name: string;
  @codable() score: number;

  constructor(data: Pick<Player, "name" | "score">) {
    this.name = data.name;
    this.score = data.score;
  }
}

const coder = new Coder([Player]);
const playerInstance = new Player({ name: "Alice", score: 100 });
const encoded = coder.encode(playerInstance);
const decoded = coder.decode<Player>(encoded);
// Structure and types remain intact.

Supported JavaScript Types

CodableJSON automatically handles multiple JavaScript data types that standard JSON serialization does not manage:

JavaScript TypeExample Output
Date{ $$Date: "2025-01-01T00:00:00.000Z" }
BigInt{ $$BigInt: "1234567890123456789" }
Set{ $$Set: ["a", "b", "c"] }
Map{ $$Map: [["key", "value"]] }

Performance Insights

CodableJSON focuses on speed and efficiency, showcasing:

  • Encoding performance is around 3 to 3.5 times faster than comparable libraries like SuperJSON.

Security Features

CodableJSON incorporates built-in security provisions to ensure safe serialization processes, such as filtering out dangerous properties and preventing prototype pollution.

For extensive documentation, including usage instructions, comparisons, and security best practices, please refer to the official documentation here.

0 comments

No comments yet.

Sign in to be the first to comment.