Node.js Platform
JSON Packer provides native-performance JSON compression functionality for Node.js environments.
Installation
bash
npm install json-packer-napi
# or
yarn add json-packer-napi
# or
pnpm add json-packer-napi
API
Core Functions
typescript
// Compress to byte array
export function compressToBytes(jsonString: string, options?: CompressOptions): Buffer;
// Compress to Base64 string
export function compressToBase64(jsonString: string, options?: CompressOptions): string;
// Decompress from byte array
export function decompressFromBytes(bytes: Buffer): string;
// Decompress from Base64 string
export function decompressFromBase64(base64: string): string;
Configuration Options
typescript
interface CompressOptions {
enable_value_pool?: boolean; // Enable string value pool (default false)
pool_min_repeats?: number; // Minimum repetitions for value pool (default 3)
pool_min_string_len?: number; // Minimum string length for value pool (default 8)
}
Examples
Basic Usage
javascript
const { compressToBase64, decompressFromBase64 } = require('json-packer-napi');
const data = { name: "Alice", age: 30, active: true };
const jsonStr = JSON.stringify(data);
// Compress
const compressed = compressToBase64(jsonStr);
console.log('Compressed:', compressed);
// Decompress
const decompressed = decompressFromBase64(compressed);
const restored = JSON.parse(decompressed);
console.log('Restored:', restored);
ESM Import
javascript
import { compressToBase64, decompressFromBase64 } from 'json-packer-napi';
const data = { items: ["apple", "banana", "cherry"] };
const compressed = compressToBase64(JSON.stringify(data));
const restored = JSON.parse(decompressFromBase64(compressed));
Enable String Value Pool
javascript
const { compressToBytes, decompressFromBytes } = require('json-packer-napi');
const data = {
users: [
{ status: "connected", message: "connected to server" },
{ status: "connected", message: "connected to server" },
{ status: "connected", message: "connected to server" }
]
};
const options = {
enable_value_pool: true,
pool_min_repeats: 2, // Strings repeated 2+ times enter value pool
pool_min_string_len: 5 // Only consider strings with length >= 5 for pool
};
const compressed = compressToBytes(JSON.stringify(data), options);
const restored = JSON.parse(decompressFromBytes(compressed));
console.log('Original size:', Buffer.byteLength(JSON.stringify(data), 'utf8'));
console.log('Compressed size:', compressed.length);