# @fastify/fast-json-stringify-compiler 中文文档

规范地址：https://npmcn.com/package/%40fastify/fast-json-stringify-compiler/

版本：5\.2\.0
npm 元数据更新时间：2026-11-12T12:48:27.227Z
中文译文更新时间：2026-11-14T11:05:11.351Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40fastify/fast-json-stringify-compiler
原文对照：https://npmcn.com/package/%40fastify/fast-json-stringify-compiler/?lang=original
项目主页：https://github.com/fastify/fast-json-stringify-compiler#readme
源码仓库：https://github.com/fastify/fast-json-stringify-compiler

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：29.4 kB，17 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：1 个
- 首次发布：2022-01-04T16:40:42.530Z
- 最新版本发布：2026-11-12T12:48:26.551Z
- 近 30 天下载量：79312158（2026-10-15 至 2026-11-13，npm 官方下载统计）
- 已发布版本数：16（近 12 个月 3 个）

## 中文简介

构建和管理用于加固框架的快速加固实例

## 安装

```sh
npm install @fastify/fast-json-stringify-compiler
```

也可以使用：`pnpm add @fastify/fast-json-stringify-compiler`、`yarn add @fastify/fast-json-stringify-compiler`、`bun add @fastify/fast-json-stringify-compiler`

## README

# @ fastify/ fast-json-stringify-compilier (中文(简体) )

[![CI](https://github.com/fastify/fast-json-stringify-compiler/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fast-json-stringify-compiler/actions/workflows/ci.yml) [![NPM version](https://img.shields.io/npm/v/@fastify/fast-json-stringify-compiler.svg?style=flat)](https://www.npmjs.com/package/@fastify/fast-json-stringify-compiler) [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

建立和管理 [`fast-json-stringify`](https://www.npmjs.com/package/fast-json-stringify) 快速化框架实例. 这个软件包负责编译应用程序的 `response` JSON schemas进入优化功能,以加快响应时间.

## 版本数

| `@fastify/fast-json-stringify-compiler` | `fast-json-stringify` | 支助 `fastify` |
| --: | --: | --: |
| 页:1 | 页:1 | ^ 5.x (韩语) |
| v4.x (英语) | 页:1 | ^ 5.x (韩语) |
| ưμ㼯a | v4.x (英语) | ^ 4.x 键 |
| v2.x (英语) | ưμ㼯a | ^ 4.x 键 |
| v1.x (韩语) | ưμ㼯a | ^3.x |

### 快速字符串配置

这个 `fast-json-stringify` 配置是默认配置 。 您可以检查默认设置 [`fast-json-stringify` 选项](https://github.com/fastify/fast-json-stringify/#options) 文档.

您也可以通过传出 [`serializerOpts`](https://fastify.dev/docs/latest/Reference/Server/#serializeropts) 配置到快捷实例.

#### `objectMechanism: 'native'`

`fast-json-stringify@7.3.0` 添加 [`objectMechanism`](https://github.com/fastify/fast-json-stringify/#object-mechanism) 选项. 与 `'native'`,序列器将有效载荷投射到有计划形状的物体和代表 `JSON.stringify`,因此在Node.js 25+ V8上继续走快路,而反应方案仍在应用(未宣布属性剥离,胁迫和格式如常). 输出与默认机制相同,选项是Node.js < 25.

```js
const app = fastify({
  serializerOpts: {
    objectMechanism: 'native'
  }
})
```

## 使用量

此模块已被 Fastify 用作默认值 . 如果您需要向您的服务器实例提供不同的版本,请参考 [官方医生](https://fastify.dev/docs/latest/Reference/Server/#schemacontroller)。 。 。 .

### 快速 Json 字符串独立

`fast-json-stringify@v4.1.0` 介绍 [独立特性](https://github.com/fastify/fast-json-stringify#standalone-mode) 让你预先编译你的计划 并用在你的应用程序中 以更快的启动.

要使用此特性,您必须了解以下内容:

1.  您必须生成并保存应用程序的编译计划 .
2.  从文件中读取所编译的策略, 并将其反馈到您的 Fastify 应用程序 .

#### 生成并保存编译的策略

Fastify 帮助您生成序列化的 schemas 函数, 您选择将其保存到您想要的地方 . 要完成此任务, 您必须使用新编译器 : `@fastify/fast-json-stringify-compiler/standalone`。 。 。 .

您必须为此编译器提供 2 个参数 :

-   `readMode: false`: 一个布尔来表示您想要生成策略函数字符串.
-   `storeFunction`" 一个同步函数,必须存储 schemas 函数的源代码. 您也可以提供同步函数, 但是您必须管理错误 .

何时 `readMode: false`, (中文(简体) ), **仅用于开发的编译器**。 。 。 .

```js
const { StandaloneSerializer } = require('@fastify/fast-json-stringify-compiler')

const factory = StandaloneSerializer({
  readMode: false,
  storeFunction (routeOpts, schemaSerializationCode) {
    // routeOpts is like: { schema, method, url, httpStatus }
    // schemaSerializationCode is a string source code that is the compiled schema function
    const fileName = generateFileName(routeOpts)
    fs.writeFileSync(path.join(__dirname, fileName), schemaSerializationCode)
  }
})

const app = fastify({
  jsonShorthand: false,
  schemaController: {
    compilersFactory: {
      buildSerializer: factory
    }
  }
})

// ... add all your routes with schemas ...

app.ready().then(() => {
  // at this stage all your schemas are compiled and stored in the file system
  // now it is important to turn off the readMode
})
```

#### 读取汇编的策略函数

在这个阶段,你应该有一个文件 每个路线的策略. 要使用它们,必须使用 `@fastify/fast-json-stringify-compiler/standalone` 参数:

-   `readMode: true`: 一个布尔来表示您想要读取和使用策略函数字符串.
-   `restoreFunction`" 一个同步函数,必须返回一个函数来序列化路由的有效载荷.

在继续阅读文件之前

-   当你使用 `readMode: true`,应用程序计划没有编译(它们被忽略). 所以,如果你改变你的计划, 你必须重新编译它们!
-   如您所见,您必须使用 `routeOpts` 对象。 你可以用这个 `routeOpts.schema.$id` 字段,由您来定义一个独特的计划标识符.

```js
const { StandaloneSerializer } = require('@fastify/fast-json-stringify-compiler')

const factory = StandaloneSerializer({
  readMode: true,
  restoreFunction (routeOpts) {
    // routeOpts is like: { schema, method, url, httpStatus }
    const fileName = generateFileName(routeOpts)
    return require(path.join(__dirname, fileName))
  }
})

const app = fastify({
  jsonShorthand: false,
  schemaController: {
    compilersFactory: {
      buildSerializer: factory
    }
  }
})

// ... add all your routes with schemas as before...

app.listen({ port: 3000 })
```

### 如何运作

这个模块提供工厂的功能来生产 [序列器编译器](https://fastify.dev/docs/latest/Reference/Server/#serializercompiler) 函数.

## 许可证

许可证根据 [麻省理工学院](https://npmcn.com/package/%40fastify/fast-json-stringify-compiler/LICENSE)。 。 。 .

## 常见问题

### @fastify/fast-json-stringify-compiler 是什么？

构建和管理用于加固框架的快速加固实例

### 如何安装 @fastify/fast-json-stringify-compiler？

在项目目录运行 npm install @fastify/fast-json-stringify-compiler，或使用 pnpm add / yarn add / bun add。

### @fastify/fast-json-stringify-compiler 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @fastify/fast-json-stringify-compiler 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @fastify/fast-json-stringify-compiler 可以商用吗？

@fastify/fast-json-stringify-compiler 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @fastify/fast-json-stringify-compiler 的下载量有多少？

根据 npm 官方下载统计，@fastify/fast-json-stringify-compiler 在 2026/10/15 – 2026/11/13 这 30 天内被下载 79,312,158 次，日均约 2,643,739 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @fastify/fast-json-stringify-compiler 还在维护吗？

最新版本 5\.2\.0 发布于 2026/11/12（1 天前）；至今共发布 16 个版本，其中过去 12 个月发布了 3 个；首次发布于 2022/01/05。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

