API Reference

fast-json-stringify

Schema-based JSON serializer that compiles a fast stringifier from a JSON schema and enforces the declared shape.

Official Documentation ↗
CompatibilityThis documentation covers version v7.2.1. Check the official docs for the latest API surface.

Installation

npmnpm install fast-json-stringify
pnpmpnpm add fast-json-stringify

Methods

Overview

fast-json-stringify compiles a fast JSON serializer from a schema, emitting only the declared fields and enabling specialized code paths. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • build — Compiles a serializer function specialized to the given JSON schema.
  • serializer — Returns JSON text containing only the schema-declared fields for the input.
  • build options — Adjusts number rounding, JSON schema validation, and large array handling.

Example

const build = require('fast-json-stringify');

const stringify = build({
  type: 'object',
  properties: { id: { type: 'integer' } },
});
console.log(stringify({ id: 1, extra: 'dropped' }));

Notes

The serializer trusts the schema by default, so data can be silently dropped. Enable ajv validation when inputs are not already trusted.

Verification

Serialize an object with an extra undeclared field and confirm the output contains only the declared properties.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/fastify/fast-json-stringify@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:49:35.665Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://cdn.jsdelivr.net/gh/fastify/fast-json-stringify@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:49:35.665Z.

FAQ

Does it validate the input?
Add ajv options to enable schema validation; without them the serializer assumes the input already matches the schema.
How are undefined or missing fields handled?
Only declared properties are emitted; fields absent from the input are omitted from the output, matching the schema shape.
Why is it faster than JSON.stringify?
The schema lets it generate specialized code paths and skip property enumeration for fields that are not part of the output.