### Install json-accelerator Source: https://github.com/elysiajs/json-accelerator/blob/main/README.md Install the json-accelerator package using various package managers like npm, yarn, pnpm, or bun. ```bash npm install json-accelerator yarn add json-accelerator pnpm add json-accelerator bun add json-accelerator ``` -------------------------------- ### Configuring Accelerator Options Source: https://github.com/elysiajs/json-accelerator/blob/main/README.md Illustrates how to pass configuration options, such as `unsafe`, to the `createAccelerator` function to control the behavior when encountering unsafe characters during string serialization. ```typescript import { Type as t } from '@sinclair/typebox' import { createAccelerator } from 'json-accelerator' const shape = t.Object({ name: t.String(), id: t.Number() }) createAccelerator(shape, { unsafe: 'throw' }) ``` -------------------------------- ### Basic Usage with TypeBox Source: https://github.com/elysiajs/json-accelerator/blob/main/README.md Demonstrates how to use `createAccelerator` with a TypeBox object schema to serialize a value into a JSON string. The generated encoder function takes a value conforming to the schema and returns its JSON string representation. ```typescript import { Type as t } from '@sinclair/typebox' import { createAccelerator } from 'json-accelerator' const shape = t.Object({ name: t.String(), id: t.Number() }) const value = { id: 0, name: 'saltyaom' } satisfies typeof shape.static const encode = createAccelerator(shape) console.log(encode(value)) // {"id":0,"name":"saltyaom"} ``` -------------------------------- ### Type Validation with TypeBox Compiler Source: https://github.com/elysiajs/json-accelerator/blob/main/README.md Shows how to integrate TypeBox's `TypeCompiler` for runtime validation before passing data to the accelerator. This ensures data integrity and prevents unexpected errors if the input does not match the schema. ```typescript import { Type as t } from '@sinclair/typebox' import { TypeCompiler } from '@sinclair/typebox/compiler' import { createAccelerator } from 'json-accelerator' const shape = t.Object({ name: t.String(), id: t.Number() }) const value = { id: 0, name: 'saltyaom' } const guard = TypeCompiler.Compile(shape) const encode = createAccelerator(shape) if (guard.Check(value)) encode(value) ``` -------------------------------- ### Manual Sanitization with `sanitize: true` Source: https://github.com/elysiajs/json-accelerator/blob/main/README.md Demonstrates how to mark specific string fields within a schema with `sanitize: true` to ensure they are always sanitized, even when the global `unsafe` option is set to 'manual'. This is useful for handling potentially unsafe user-provided input. ```typescript import { Type as t } from '@sinclair/typebox' import { createAccelerator } from 'json-accelerator' const shape = t.Object({ name: t.String(), id: t.Number(), unknown: t.String({ sanitize: true }) }) const value = { id: 0, name: 'saltyaom', unknown: `hello\nworld` } satisfies typeof shape.static const encode = createAccelerator(shape, { sanitize: 'manual' }) console.log(encode(value)) // {"id":0,"name":"saltyaom","unknown":"hello\\nworld"} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.