Skip to content

Commit f03f7ea

Browse files
committed
fix(last confusion): readme
1 parent 18c1915 commit f03f7ea

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ bun add @ublitzjs/niche-json-stringify
2929

3030
## Quick start
3131

32-
Generate a specialized serializer from a JSON Schema (or a TypeBox schema). The generated function **does not validate** its inputit assumes the data already matches the schema.
32+
Generate a specialized serializer from a JSON Schema (or a TypeBox schema). The generated function **does not validate** its input - it assumes the data already matches the schema.
3333

3434
```ts
35-
import { Type, Static } from "@sinclair/typebox";
35+
import { Type } from "@sinclair/typebox";
3636
import { createStringify } from "@ublitzjs/niche-json-stringify";
3737

3838
const User = Type.Object({
@@ -42,8 +42,6 @@ const User = Type.Object({
4242
tags: Type.Array(Type.String(), { default: [] }),
4343
});
4444

45-
type User = Static<typeof User>;
46-
4745
const stringify = createStringify(User);
4846

4947
const json = stringify({
@@ -92,12 +90,12 @@ const stringify = createStringify(LogEntry);
9290
### Additional properties
9391

9492
`additionalProperties` is supported. Unknown properties are serialized using `JSON.stringify`.
95-
93+
However, it does not work with `default` values or with and outer object - only properties.
9694
```ts
97-
const Payload = Type.Object(
98-
{ id: Type.Integer() },
99-
{ additionalProperties: true }
100-
);
95+
const Payload = Type.Object({
96+
id: Type.Integer(),
97+
extra: Type.Object({}, { additionalProperties: true })
98+
});
10199

102100
const stringify = createStringify(Payload);
103101

@@ -126,24 +124,31 @@ const stringify = createStringify(User, CJSescape);
126124
### Specialized array serializers
127125

128126
For hot paths, the package also exports highly optimized serializers for common array types.
127+
Internally they are used when array has certain size and replaced with `JSON.stringify` when fed a huge one.
129128

130129
```ts
131130
import {
132-
int_arr,
133-
bool_arr,
131+
num_arr_node,
132+
num_arr_bun,
133+
bool_arr_bun,
134+
bool_arr_node,
134135
str_arr_node,
135136
str_arr_bun,
136137
} from "@ublitzjs/niche-json-stringify";
137138

138-
int_arr([1, 2, 3]);
139+
int_arr_node([1, 2, 3]);
140+
int_arr_bun([1, 2, 3]);
139141
// "[1,2,3]"
140142

141-
bool_arr([true, false]);
143+
// look for use cases
144+
bool_arr_node([true, false]);
145+
bool_arr_bun([true, false]);
142146
// "[true,false]"
143147

144148
// optimised non-escaping serialisers
145149
str_arr_node(["a", "b"]);
146150
str_arr_bun(["a", "b"]);
151+
// "[\"a\",\"b\"]"
147152
```
148153

149154
> **Note:** Generated serializers prioritize speed over safety. They do **not** validate input, so invalid data may produce invalid JSON or incorrect output. Validate your data before serialization if necessary.

0 commit comments

Comments
 (0)