-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
json.ts
173 lines (155 loc) Β· 5.78 KB
/
json.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
import jsonpointer from "jsonpointer";
import { TextLoader } from "./text.js";
/**
* Class that extends the `TextLoader` class. It represents a document
* loader that loads documents from JSON files. It has a constructor that
* takes a `filePathOrBlob` parameter representing the path to the JSON
* file or a `Blob` object, and an optional `pointers` parameter that
* specifies the JSON pointers to extract.
*/
export class JSONLoader extends TextLoader {
public pointers: string[];
constructor(filePathOrBlob: string | Blob, pointers: string | string[] = []) {
super(filePathOrBlob);
this.pointers = Array.isArray(pointers) ? pointers : [pointers];
}
/**
* Method that takes a `raw` string as a parameter and returns a promise
* that resolves to an array of strings. It parses the raw JSON string and
* extracts the values based on the specified JSON pointers. If no JSON
* pointers are specified, it extracts all the strings from the JSON
* object.
* @param raw The raw JSON string to parse.
* @returns A promise that resolves to an array of strings.
*/
protected async parse(raw: string): Promise<string[]> {
const json = JSON.parse(raw.trim());
// If there is no pointers specified we extract all strings we found
const extractAllStrings = !(this.pointers.length > 0);
const compiledPointers = this.pointers.map((pointer) =>
jsonpointer.compile(pointer)
);
return this.extractArrayStringsFromObject(
json,
compiledPointers,
extractAllStrings
);
}
/**
* If JSON pointers are specified, return all strings below any of them
* and exclude all other nodes expect if they match a JSON pointer (to allow to extract strings from different levels)
*
* If no JSON pointer is specified then return all string in the object
*/
private extractArrayStringsFromObject(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
json: any,
pointers: jsonpointer[],
extractAllStrings = false,
keyHasBeenFound = false
): string[] {
if (!json) {
return [];
}
if (typeof json === "string" && extractAllStrings) {
return [json];
}
if (Array.isArray(json) && extractAllStrings) {
let extractedString: string[] = [];
for (const element of json) {
extractedString = extractedString.concat(
this.extractArrayStringsFromObject(element, pointers, true)
);
}
return extractedString;
}
if (typeof json === "object") {
if (extractAllStrings) {
return this.extractArrayStringsFromObject(
Object.values(json),
pointers,
true
);
}
const targetedEntries = this.getTargetedEntries(json, pointers);
const thisLevelEntries = Object.values(json) as object[];
const notTargetedEntries = thisLevelEntries.filter(
(entry: object) => !targetedEntries.includes(entry)
);
let extractedStrings: string[] = [];
// If we found a targeted entry, we extract all strings from it
if (targetedEntries.length > 0) {
for (const oneEntry of targetedEntries) {
extractedStrings = extractedStrings.concat(
this.extractArrayStringsFromObject(oneEntry, pointers, true, true)
);
}
for (const oneEntry of notTargetedEntries) {
extractedStrings = extractedStrings.concat(
this.extractArrayStringsFromObject(oneEntry, pointers, false, true)
);
}
} else if (extractAllStrings || !keyHasBeenFound) {
for (const oneEntry of notTargetedEntries) {
extractedStrings = extractedStrings.concat(
this.extractArrayStringsFromObject(
oneEntry,
pointers,
extractAllStrings
)
);
}
}
return extractedStrings;
}
return [];
}
/**
* Method that takes a `json` object and an array of `pointers` as
* parameters and returns an array of targeted entries. It iterates over
* the JSON pointers and uses the `jsonpointer.get()` function to get the
* targeted entries from the JSON object.
* @param json The JSON object to get targeted entries from.
* @param pointers The JSON pointers to get targeted entries.
* @returns An array of targeted entries.
*/
private getTargetedEntries(json: object, pointers: jsonpointer[]): object[] {
const targetEntries = [];
for (const pointer of pointers) {
const targetedEntry = pointer.get(json);
if (targetedEntry) {
targetEntries.push(targetedEntry);
}
}
return targetEntries;
}
}
/**
* Class that extends the `TextLoader` class. It represents a document
* loader that loads documents from JSON Lines files. It has a constructor
* that takes a `filePathOrBlob` parameter representing the path to the
* JSON Lines file or a `Blob` object, and a `pointer` parameter that
* specifies the JSON pointer to extract.
*/
export class JSONLinesLoader extends TextLoader {
constructor(filePathOrBlob: string | Blob, public pointer: string) {
super(filePathOrBlob);
}
/**
* Method that takes a `raw` string as a parameter and returns a promise
* that resolves to an array of strings. It parses the raw JSON Lines
* string, splits it into lines, parses each line as JSON, and extracts
* the values based on the specified JSON pointer.
* @param raw The raw JSON Lines string to parse.
* @returns A promise that resolves to an array of strings.
*/
protected async parse(raw: string): Promise<string[]> {
const lines = raw.split("\n");
const jsons = lines
.map((line) => line.trim())
.filter(Boolean)
.map((line) => JSON.parse(line));
const pointer = jsonpointer.compile(this.pointer);
return jsons.map((json) => pointer.get(json));
}
}