fast-json-stringify
Schema-based JSON serializer that compiles a fast stringifier from a JSON schema and enforces the declared shape.
Official Documentation ↗v7.2.1. Check the official docs for the latest API surface.Installation
npm install fast-json-stringifypnpm add fast-json-stringifyMethods
No methods match this filter.
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.
- However, other schema rules would throw in similar situations. (https://cdn.jsdelivr.net/gh/fastify/fast-json-stringify@latest/README.md)
- Valid values for it are: - default - This option keeps the full feature set of this lib: every element is still coerced to the type declared in items, and type errors are still thrown. (https://cdn.jsdelivr.net/gh/fastify/fast-json-stringify@latest/README.md)
- While the schema is currently validated for any developer errors, there is no guarantee that supplying user-generated schema could not expose your application to remote attacks. (https://cdn.jsdelivr.net/gh/fastify/fast-json-stringify@latest/README.md)
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.
- You can use it only if you are sure that your data doesn’t need escaping. (https://cdn.jsdelivr.net/gh/fastify/fast-json-stringify@latest/README.md)
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.