快速字符串
快速字符串 明显快于 JSON.stringify() 小有效载荷.
它的性能优势随着有效载荷的增长而收缩.
和他一对 平板电脑,它触发一个 V8 优化,在最终将字符串转换成一个时提高性能 Buffer。 。 。 .
如何运作
快速 json- 字符串需要一个 JSON Schema 草案7 输入生成快 stringify 函数.
基准
- 机器 :
EX41S-SSD, Intel Core i7, 4Ghz, 64GB RAM, 4C/8T, SSD。 。 。 . - 节点.js
v18.12.1
FJS creation x 4,129 ops/sec ±0.82% (92 runs sampled)
CJS creation x 184,196 ops/sec ±0.12% (97 runs sampled)
AJV Serialize creation x 61,130,591 ops/sec ±0.40% (92 runs sampled)
JSON.stringify array x 5,057 ops/sec ±0.10% (100 runs sampled)
fast-json-stringify array default x 6,243 ops/sec ±0.14% (98 runs sampled)
fast-json-stringify array json-stringify x 6,261 ops/sec ±0.30% (99 runs sampled)
compile-json-stringify array x 6,842 ops/sec ±0.18% (96 runs sampled)
AJV Serialize array x 6,964 ops/sec ±0.11% (95 runs sampled)
JSON.stringify large array x 248 ops/sec ±0.07% (90 runs sampled)
fast-json-stringify large array default x 99.96 ops/sec ±0.22% (74 runs sampled)
fast-json-stringify large array json-stringify x 248 ops/sec ±0.07% (90 runs sampled)
compile-json-stringify large array x 317 ops/sec ±0.09% (89 runs sampled)
AJV Serialize large array x 111 ops/sec ±0.07% (33 runs sampled)
JSON.stringify long string x 16,002 ops/sec ±0.09% (98 runs sampled)
fast-json-stringify long string x 15,979 ops/sec ±0.09% (96 runs sampled)
compile-json-stringify long string x 15,952 ops/sec ±0.31% (97 runs sampled)
AJV Serialize long string x 21,416 ops/sec ±0.08% (98 runs sampled)
JSON.stringify short string x 12,944,272 ops/sec ±0.09% (96 runs sampled)
fast-json-stringify short string x 30,585,790 ops/sec ±0.27% (97 runs sampled)
compile-json-stringify short string x 30,656,406 ops/sec ±0.12% (96 runs sampled)
AJV Serialize short string x 30,406,785 ops/sec ±0.37% (96 runs sampled)
JSON.stringify obj x 3,153,043 ops/sec ±0.33% (99 runs sampled)
fast-json-stringify obj x 6,866,434 ops/sec ±0.11% (100 runs sampled)
compile-json-stringify obj x 15,886,723 ops/sec ±0.15% (98 runs sampled)
AJV Serialize obj x 8,969,043 ops/sec ±0.36% (97 runs sampled)
JSON stringify date x 1,126,547 ops/sec ±0.09% (97 runs sampled)
fast-json-stringify date format x 1,836,188 ops/sec ±0.12% (99 runs sampled)
compile-json-stringify date format x 1,125,735 ops/sec ±0.19% (98 runs sampled)
目录 :
ExampleOptionsAPIfastJsonStringifySpecific use casesRequiredMissing fieldsPattern PropertiesAdditional PropertiesAnyOf财务报告和审定财务报表OneOfReuse - $refLong integersIntegersNullableLarge ArraysSecurity NoticeDebug ModeAcknowledgementsLicense
在 RunKit 上试试: https://runkit.com/npm/快活-json-stringify (中文(简体) )
示例
const fastJson = require('fast-json-stringify')
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
age: {
description: 'Age in years',
type: 'integer'
},
reg: {
type: 'string'
}
}
})
console.log(stringify({
firstName: 'Matteo',
lastName: 'Collina',
age: 32,
reg: /"([^"]|\\")*"/
}))
选项
你可以选择提供 fast-json-stringify 作为第二个参数的选项对象:
const fastJson = require('fast-json-stringify')
const stringify = fastJson(mySchema, {
schema: { ... },
ajv: { ... },
rounding: 'ceil'
})
schema: 按美元属性分类的外部参考计划. 更多细节ajv数字 : ajv v8 实例设置 对于需要ajv。 。 。 . 更多细节rounding: 如何设置integer类型将在非整数时四舍五入. 更多细节largeArrayMechanism: 设置用于处理大型 (默认)20000或更多项)数组. 更多细节
API 密码
快速 JsonStringify (schema) (英语)
构建一个 stringify() 基于 jsonschema 草稿 7 spec。 。 。 .
支持类型 :
'string''integer''number''array''object''boolean''null'
并入巢者也.
具体使用案例
| 实例 | 序列为 |
|---|---|
Date | string 通过 toISOString() |
RegExp | string |
BigInt | integer 通过 toString |
JSON Schema 内置格式 日期将按下列顺序排列:
| 格式 | 序列化格式示例 |
|---|---|
date-time | 2020-04-03T09:11:08.615Z |
date | 2020-04-03 |
time | 09:11:08 |
说明:如果字符串格式化了日期而不是日期对象,则不会对其进行操纵. 报告的格式应适当.
带有日期对象的示例:
const stringify = fastJson({
title: 'Example Schema with string date-time field',
type: 'string',
format: 'date-time'
})
const date = new Date()
console.log(stringify(date)) // '"YYYY-MM-DDTHH:mm:ss.sssZ"'
需求
您可以通过在图中添加字段名称来设置对象的特定字段 required 阵列在您的计划中.
示例
const schema = {
title: 'Example Schema with required field',
type: 'object',
properties: {
nickname: {
type: 'string'
},
mail: {
type: 'string'
}
},
required: ['mail']
}
如果字符串对象缺少所需的字段, fast-json-stringify 将投出一个错误.
缺少字段
如果一个字段 当前 在计谋中 不存在 在要字符串的对象中, fast-json-stringify 将不会将其写入最后字符串.
示例
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
nickname: {
type: 'string'
},
mail: {
type: 'string'
}
}
})
const obj = {
mail: '[email protected]'
}
console.log(stringify(obj)) // '{"mail":"[email protected]"}'
默认
fast-json-stringify 支持 default 用于序列化值的 jsonschema 密钥
如果是的话 undefined 或者不在场.
示例
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
nickname: {
type: 'string',
default: 'the default string'
}
}
})
console.log(stringify({})) // '{"nickname":"the default string"}'
console.log(stringify({nickname: 'my-nickname'})) // '{"nickname":"my-nickname"}'
图案属性
fast-json-stringify 支持JSON schema定义的图案属性.
特性 必须是对象,如果密钥是有效的正则克并值是对象,则以这种方式声明: { type: 'type' }。 。 。 .
特性 将只对属性对象中未明确列出的属性工作.
示例
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
nickname: {
type: 'string'
}
},
patternProperties: {
'num': {
type: 'number'
},
'.*foo$': {
type: 'string'
}
}
})
const obj = {
nickname: 'nick',
matchfoo: 42,
otherfoo: 'str',
matchnum: 3
}
console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nickname":"nick"}'
其他属性
fast-json-stringify 支持 JSON schema 定义的其他属性.
额外收益 必须是物体或布尔,以这种方式声明: { type: 'type' }。 。 。 .
额外收益 将只为未明确列出的属性工作 属性 财务报告和审定财务报表 特性 对象.
若为 额外收益 不存在或设置为 false,其中未明确列出的所有财产 属性 财务报告和审定财务报表 特性 如 缺少字段。 。 。 .
缺失的字段被忽略,以避免在序列化前需要重写对象. 然而,其他规则也会在类似情况下适用.
若为 额外收益 设置为 true,它将被使用 JSON.stringify 来字符串附加属性。 如果你想达到最大性能 我们强烈鼓励你尽可能使用固定的策略.
附加属性总是会被序列化于对象的末尾.
示例
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
nickname: {
type: 'string'
}
},
patternProperties: {
'num': {
type: 'number'
},
'.*foo$': {
type: 'string'
}
},
additionalProperties: {
type: 'string'
}
})
const obj = {
nickname: 'nick',
matchfoo: 42,
otherfoo: 'str',
matchnum: 3,
nomatchstr: 'valar morghulis',
nomatchint: 313
}
console.log(stringify(obj)) // '{"nickname":"nick","matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313"}'
任意和唯一
fast-json-stringify 支持 任何一个 财务报告和审定财务报表 其中一个 jSON schema定义的关键词. 两者必须是一系列有效的JSON计划. 不同的计划会按指定的顺序进行测试. 计划越多 stringify 在找到火柴之前必须先试试 速度越慢.
任何一个 财务报告和审定财务报表 其中一个 使用情况 页:1 作为 JSON 的 schema 验证器,用于找到匹配数据的 schema 。 这对业绩有影响——仅作为最后手段使用.
示例
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
'undecidedType': {
'anyOf': [{
type: 'string'
}, {
type: 'boolean'
}]
}
}
})
指定对象 JSON 计划时 任何一个,添加 所需经费 验证关键字,只匹配对象与您想要的属性.
示例
const stringify = fastJson({
title: 'Example Schema',
type: 'array',
items: {
anyOf: [
{
type: 'object',
properties: {
savedId: { type: 'string' }
},
// without "required" validation any object will match
required: ['savedId']
},
{
type: 'object',
properties: {
error: { type: 'string' }
},
required: ['error']
}
]
}
})
如果/当时/过去
fast-json-stringify 支持 if/then/else jsonschema 特性。 见 ajv 文档。 。 。 .
示例
const stringify = fastJson({
'type': 'object',
'properties': {
},
'if': {
'properties': {
'kind': { 'type': 'string', 'enum': ['foobar'] }
}
},
'then': {
'properties': {
'kind': { 'type': 'string', 'enum': ['foobar'] },
'foo': { 'type': 'string' },
'bar': { 'type': 'number' }
}
},
'else': {
'properties': {
'kind': { 'type': 'string', 'enum': ['greeting'] },
'hi': { 'type': 'string' },
'hello': { 'type': 'number' }
}
}
})
console.log(stringify({
kind: 'greeting',
foo: 'FOO',
bar: 42,
hi: 'HI',
hello: 45
})) // {"kind":"greeting","hi":"HI","hello":45}
console.log(stringify({
kind: 'foobar',
foo: 'FOO',
bar: 42,
hi: 'HI',
hello: 45
})) // {"kind":"foobar","foo":"FOO","bar":42}
编号 不要声明属性两次 否则你会打印两次!
重新使用 - 美元调整
如果您想要重用一个值的定义, 您可以使用属性 $ref。 。 。 .
价值 $ref 必须是字符串在 JSON 指针 格式.
示例
const schema = {
title: 'Example Schema',
definitions: {
num: {
type: 'object',
properties: {
int: {
type: 'integer'
}
}
},
str: {
type: 'string'
}
},
type: 'object',
properties: {
nickname: {
$ref: '#/definitions/str'
}
},
patternProperties: {
'num': {
$ref: '#/definitions/num'
}
},
additionalProperties: {
$ref: '#/definitions/def'
}
}
const stringify = fastJson(schema)
如果需要使用外部定义,可以把它作为选择 fast-json-stringify。 。 。 .
示例
const schema = {
title: 'Example Schema',
type: 'object',
properties: {
nickname: {
$ref: 'strings#/definitions/str'
}
},
patternProperties: {
'num': {
$ref: 'numbers#/definitions/num'
}
},
additionalProperties: {
$ref: 'strings#/definitions/def'
}
}
const externalSchema = {
numbers: {
definitions: {
num: {
type: 'object',
properties: {
int: {
type: 'integer'
}
}
}
}
},
strings: require('./string-def.json')
}
const stringify = fastJson(schema, { schema: externalSchema })
外部定义也可以相互参照. 示例
const schema = {
title: 'Example Schema',
type: 'object',
properties: {
foo: {
$ref: 'strings#/definitions/foo'
}
}
}
const externalSchema = {
strings: {
definitions: {
foo: {
$ref: 'things#/definitions/foo'
}
}
},
things: {
definitions: {
foo: {
type: 'string'
}
}
}
}
const stringify = fastJson(schema, { schema: externalSchema })
长整数
默认情况下, 库会自动处理 大点。 。 。 .
整数
这个 type: integer 如果提供了浮动点,则属性将被截断.
您可以自定义此行为 rounding 选项 round, (中文(简体) ), ceil, (中文(简体) ), floor 或者说 trunc默认为 trunc数字 :
const stringify = fastJson(schema, { rounding: 'ceil' })
无效
根据 打开 API 3.0 规格,必须宣布无效值 nullable。 。 。 .
可忽略对象
const stringify = fastJson({
'title': 'Nullable schema',
'type': 'object',
'nullable': true,
'properties': {
'product': {
'nullable': true,
'type': 'object',
'properties': {
'name': {
'type': 'string'
}
}
}
}
})
console.log(stringify({product: {name: "hello"}})) // "{"product":{"name":"hello"}}"
console.log(stringify({product: null})) // "{"product":null}"
console.log(stringify(null)) // null
否则,无效值将不但没有引起错误,反而被胁迫如下:
integer- > (中文)0number- > (中文)0string- > (中文)""boolean- > (中文)false
大阵列
大型阵列在本文档范围内被定义为包含:,
默认, 20000 元素或更多元素。 该值可通过选项调整
参数 largeArraySize。 。 。 .
在某个时候,由于使用默认机制造成的间接费用
fast-json-stringify 来处理数组开始指数化增长,引导
以减缓总体处决.
设置
为了改进用户设置 largeArrayMechanism 财务报告和审定财务报表
largeArraySize 选项.
largeArrayMechanism默认值为 default有效的数值是:
default- 这个选项是业绩和功能之间的折衷办法,由 仍然提供预期的功能 可能的性能收益。 有了这个选项, 大阵列 这将是 通过加入其使用字符串的元素而字符串化Array.join改为 更好性能的字符串连接json-stringify- 此选项将删除对计划验证的支持 在内部 大阵列 完全没有。 这样做之前的间接费用 提及无效,大大改善了执行时间。 注意没有变化 在未考虑的数组的行为中 大块
largeArraySize默认值为 20000有效数值
等整数:
200002e4'20000''2e4'- 说吧 注意此选项将转换为2没有200001.5- 说吧 注意此选项将转换为1
基准
供参考的是,现提出一些基准,用于与这三个基准进行比较 机制。 以旧机器为基准.
- 机器 :
ST1000LM024 HN-M 1TB HDD, Intel Core i7-3610QM @ 2.3GHz, 12GB RAM, 4C/8T。 。 。 . - 节点.js
v16.13.1
JSON.stringify large array x 157 ops/sec ±0.73% (86 runs sampled)
fast-json-stringify large array default x 48.72 ops/sec ±4.92% (48 runs sampled)
fast-json-stringify large array json-stringify x 157 ops/sec ±0.76% (86 runs sampled)
compile-json-stringify large array x 175 ops/sec ±4.47% (79 runs sampled)
AJV Serialize large array x 58.76 ops/sec ±4.59% (60 runs sampled)
担保通知
将计划定义作为应用程序代码, 它 无法使用用户提供的策略.
实现低成本和高性能的校正 fast-json-stringify
创建和编译函数(使用 Function (原始内容存档于2019-03-28). resources. instructionor) on signation.
虽然 schema 正在验证任何开发者错误,
无法保证无法提供用户生成的计划
让你的应用程序暴露在远程攻击中.
用户负责发送可信赖的数据. fast-json-stringify 保证你将得到
有效的输出,但输入必须符合计划或者可以被强制用于计划。 如果输入
不符合计划,你会得到未定义的行为.
调试模式
调试模式可以在您的开发过程中被激活, 以了解当事物不发生时会发生什么 工作如所期望的那样.
const debugCompiled = fastJson({
title: 'default string',
type: 'object',
properties: {
firstName: {
type: 'string'
}
}
}, { mode: 'debug' })
console.log(debugCompiled) // it is a object contain code, ajv instance
const rawString = debugCompiled.code // it is the generated code
console.log(rawString)
const stringify = fastJson.restore(debugCompiled) // use the generated string to get back the `stringify` function
console.log(stringify({ firstName: 'Foo', surname: 'bar' })) // '{"firstName":"Foo"}'
鸣谢
这个项目由下列各方慷慨赞助: 临近格式。 。 。 .
许可证
麻省理工学院