@vertesia/json 0.80.0-dev.20251121 → 0.80.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +180 -0
- package/package.json +10 -1
package/README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# @vertesia/json
|
|
2
|
+
|
|
3
|
+
JSON utilities for TypeScript, including type definitions and object traversal with the visitor pattern.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **JSON Type Definitions**: Strict TypeScript types for JSON values
|
|
8
|
+
- **Object Walker**: Traverse nested objects and arrays using the visitor pattern
|
|
9
|
+
- **Async Support**: Async object walker for asynchronous operations during traversal
|
|
10
|
+
- **Map Function**: Transform object values while preserving structure
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @vertesia/json
|
|
16
|
+
# or
|
|
17
|
+
pnpm add @vertesia/json
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### JSON Types
|
|
23
|
+
|
|
24
|
+
Strict TypeScript types for JSON values:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import type {
|
|
28
|
+
JSONPrimitive, // string | number | boolean | null
|
|
29
|
+
JSONArray, // JSONValue[]
|
|
30
|
+
JSONObject, // { [key: string]: JSONValue }
|
|
31
|
+
JSONComposite, // JSONArray | JSONObject
|
|
32
|
+
JSONValue // JSONPrimitive | JSONComposite
|
|
33
|
+
} from '@vertesia/json';
|
|
34
|
+
|
|
35
|
+
function processJson(data: JSONValue) {
|
|
36
|
+
// Type-safe JSON handling
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Object Walker
|
|
41
|
+
|
|
42
|
+
Traverse objects using the visitor pattern:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { ObjectWalker } from '@vertesia/json';
|
|
46
|
+
|
|
47
|
+
const data = {
|
|
48
|
+
name: 'John',
|
|
49
|
+
tags: ['admin', 'user'],
|
|
50
|
+
metadata: {
|
|
51
|
+
created: '2024-01-01',
|
|
52
|
+
count: 42
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const walker = new ObjectWalker();
|
|
57
|
+
|
|
58
|
+
walker.walk(data, {
|
|
59
|
+
onStartObject: (key, value) => {
|
|
60
|
+
console.log(`Start object: ${key}`);
|
|
61
|
+
},
|
|
62
|
+
onEndObject: (key, value) => {
|
|
63
|
+
console.log(`End object: ${key}`);
|
|
64
|
+
},
|
|
65
|
+
onStartIteration: (key, value) => {
|
|
66
|
+
console.log(`Start array: ${key}`);
|
|
67
|
+
},
|
|
68
|
+
onEndIteration: (key, value) => {
|
|
69
|
+
console.log(`End array: ${key}`);
|
|
70
|
+
},
|
|
71
|
+
onValue: (key, value) => {
|
|
72
|
+
console.log(`Value: ${key} = ${value}`);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Map Values
|
|
78
|
+
|
|
79
|
+
Transform values while preserving structure:
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { ObjectWalker } from '@vertesia/json';
|
|
83
|
+
|
|
84
|
+
const data = {
|
|
85
|
+
name: 'John',
|
|
86
|
+
age: 30,
|
|
87
|
+
scores: [85, 90, 78]
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const walker = new ObjectWalker();
|
|
91
|
+
const result = walker.map(data, (key, value) => {
|
|
92
|
+
if (typeof value === 'number') {
|
|
93
|
+
return value * 2;
|
|
94
|
+
}
|
|
95
|
+
return value;
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// result: { name: 'John', age: 60, scores: [170, 180, 156] }
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Async Object Walker
|
|
102
|
+
|
|
103
|
+
For asynchronous operations during traversal:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { AsyncObjectWalker } from '@vertesia/json';
|
|
107
|
+
|
|
108
|
+
const walker = new AsyncObjectWalker();
|
|
109
|
+
|
|
110
|
+
await walker.walk(data, {
|
|
111
|
+
onValue: async (key, value) => {
|
|
112
|
+
if (typeof value === 'string') {
|
|
113
|
+
await processString(value);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// Async map
|
|
119
|
+
const result = await walker.map(data, async (key, value) => {
|
|
120
|
+
if (typeof value === 'string') {
|
|
121
|
+
return await translateText(value);
|
|
122
|
+
}
|
|
123
|
+
return value;
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Iterator Support
|
|
128
|
+
|
|
129
|
+
Enable support for iterables beyond arrays:
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
const walker = new ObjectWalker(true); // Enable iterator support
|
|
133
|
+
|
|
134
|
+
const data = {
|
|
135
|
+
items: new Set([1, 2, 3])
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
walker.walk(data, {
|
|
139
|
+
onValue: (key, value) => {
|
|
140
|
+
console.log(key, value);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## API Reference
|
|
146
|
+
|
|
147
|
+
### Types
|
|
148
|
+
|
|
149
|
+
| Type | Description |
|
|
150
|
+
|------|-------------|
|
|
151
|
+
| `JSONPrimitive` | `string \| number \| boolean \| null` |
|
|
152
|
+
| `JSONArray` | `JSONValue[]` |
|
|
153
|
+
| `JSONObject` | `{ [key: string]: JSONValue }` |
|
|
154
|
+
| `JSONComposite` | `JSONArray \| JSONObject` |
|
|
155
|
+
| `JSONValue` | `JSONPrimitive \| JSONComposite` |
|
|
156
|
+
|
|
157
|
+
### ObjectWalker
|
|
158
|
+
|
|
159
|
+
| Method | Description |
|
|
160
|
+
|--------|-------------|
|
|
161
|
+
| `walk(obj, visitor)` | Traverse object with visitor callbacks |
|
|
162
|
+
| `map(obj, mapFn)` | Transform values while preserving structure |
|
|
163
|
+
|
|
164
|
+
### ObjectVisitor Callbacks
|
|
165
|
+
|
|
166
|
+
| Callback | Description |
|
|
167
|
+
|----------|-------------|
|
|
168
|
+
| `onStartObject(key, value)` | Called when entering an object |
|
|
169
|
+
| `onEndObject(key, value)` | Called when leaving an object |
|
|
170
|
+
| `onStartIteration(key, value)` | Called when entering an array/iterable |
|
|
171
|
+
| `onEndIteration(key, value)` | Called when leaving an array/iterable |
|
|
172
|
+
| `onValue(key, value)` | Called for primitive values |
|
|
173
|
+
|
|
174
|
+
### AsyncObjectWalker
|
|
175
|
+
|
|
176
|
+
Same API as `ObjectWalker` but all visitor callbacks and map functions return Promises.
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
Apache-2.0
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vertesia/json",
|
|
3
|
-
"version": "0.80.0
|
|
3
|
+
"version": "0.80.0",
|
|
4
4
|
"description": "JSON utlities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./lib/types/index.d.ts",
|
|
@@ -32,6 +32,15 @@
|
|
|
32
32
|
"url": "https://github.com/vertesia/composableai.git",
|
|
33
33
|
"directory": "packages/json"
|
|
34
34
|
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"vertesia",
|
|
37
|
+
"json",
|
|
38
|
+
"utilities",
|
|
39
|
+
"walker",
|
|
40
|
+
"visitor",
|
|
41
|
+
"traverse",
|
|
42
|
+
"typescript"
|
|
43
|
+
],
|
|
35
44
|
"scripts": {
|
|
36
45
|
"test": "vitest run",
|
|
37
46
|
"build": "pnpm exec tsmod build && pnpm exec rollup -c",
|