-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathjson.js
More file actions
161 lines (155 loc) · 4.15 KB
/
Copy pathjson.js
File metadata and controls
161 lines (155 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import fastJson from 'fast-json-stringify';
import { pkgVersion, baseSupports, bufToUtf8 } from './common.js';
let simdjson = null;
try {
simdjson = await import('simdjson');
} catch {
/* optional native addon */
}
/** JSON Schema shapes for official V2 types (fast-json-stringify). */
const v2JsonSchemas = {
message: {
title: 'Message',
type: 'object',
properties: {
f_bool: { type: 'boolean' },
f_int32: { type: 'integer' },
f_int64: { type: 'integer' },
f_float64: { type: 'number' },
f_string: { type: 'string' },
f_bool_2: { type: 'boolean' },
f_int32_2: { type: 'integer' },
f_string_2: { type: 'string' },
},
},
document: {
title: 'Document',
type: 'object',
properties: {
id: { type: 'string' },
status: { type: 'integer' },
meta: {
type: 'object',
properties: {
region: { type: 'string' },
version: { type: 'integer' },
},
},
items: {
type: 'array',
items: {
type: 'object',
properties: {
sku: { type: 'string' },
qty: { type: 'integer' },
price_minor: { type: 'integer' },
},
},
},
},
},
telemetry: {
title: 'Telemetry',
type: 'object',
properties: {
source: { type: 'string' },
ts: { type: 'integer' },
tags: { type: 'array', items: { type: 'string' } },
values: { type: 'array', items: { type: 'number' } },
},
},
strings: {
title: 'Strings',
type: 'object',
properties: {
items: { type: 'array', items: { type: 'string' } },
},
},
event: {
title: 'Event',
type: 'object',
properties: {
event_id: { type: 'string' },
event_type: { type: 'string' },
occurred_at: { type: 'integer' },
producer: { type: 'string' },
attrs: {
type: 'array',
items: {
type: 'object',
properties: {
key: { type: 'string' },
value: { type: 'string' },
},
},
},
},
},
};
export const jsonBuiltin = {
name: 'JSON.stringify',
version: `node-${process.versions.node}`,
category: 'json',
supports: baseSupports,
prepare() {},
serialize(value) {
// Optimal: single stringify; Buffer.from(string) is the Node wire representation.
return Buffer.from(JSON.stringify(value), 'utf8');
},
deserialize(buf) {
// Optimal: avoid double Buffer wrap when already a Buffer.
return JSON.parse(bufToUtf8(buf));
},
};
let fjsStringify = null;
export const fastJsonSer = {
name: 'fast-json-stringify',
version: pkgVersion('fast-json-stringify'),
category: 'json',
supports: baseSupports,
prepare(dataName, value) {
// Compile once per fixture outside the timed path (package contract).
if (Array.isArray(value)) {
const itemSchema = v2JsonSchemas[dataName] || {
type: 'object',
additionalProperties: true,
};
fjsStringify = fastJson({ type: 'array', items: itemSchema });
} else if (v2JsonSchemas[dataName]) {
fjsStringify = fastJson(v2JsonSchemas[dataName]);
} else {
fjsStringify = fastJson({
type: typeof value === 'object' && value !== null ? 'object' : 'integer',
additionalProperties: true,
});
}
},
serialize(value) {
return Buffer.from(fjsStringify(value), 'utf8');
},
deserialize(buf) {
return JSON.parse(bufToUtf8(buf));
},
};
/** Honest name: serialize is std JSON.stringify; only deserialize uses SIMD. */
export const simdjsonSer = {
name: 'simdjson-parse+JSON.stringify',
version: pkgVersion('simdjson') || 'optional-missing',
category: 'json',
supports: (n) => baseSupports(n) && !!simdjson,
prepare() {},
serialize(value) {
return Buffer.from(JSON.stringify(value), 'utf8');
},
deserialize(buf) {
if (!simdjson) throw new Error('simdjson not installed');
const mod = simdjson.default || simdjson;
// simdjson parse expects a string
return mod.parse(bufToUtf8(buf));
},
};
export function jsonSerializers() {
const list = [jsonBuiltin, fastJsonSer];
if (simdjson) list.push(simdjsonSer);
return list;
}