flex-json 0.0.5 → 0.0.6

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.
@@ -0,0 +1,308 @@
1
+ /**
2
+ * Custom error class for FlexJson errors
3
+ * Thrown when throwOnError is true (default) and an error occurs
4
+ */
5
+ declare class FlexJsonError extends Error {
6
+ /** Error status code (negative number) */
7
+ status: number;
8
+
9
+ /** Error message */
10
+ message: string;
11
+
12
+ constructor(status: number, message: string);
13
+ }
14
+
15
+ /**
16
+ * FlexJson - Flexible JSON manipulation library for JavaScript
17
+ * Supports comments, single/double/unquoted strings, and preserves formatting
18
+ */
19
+ declare class FlexJson {
20
+ /**
21
+ * Creates a new FlexJson instance
22
+ * @param initialJSON - Optional JSON string to parse
23
+ * @param useFlexJsonFlag - If true, enables FlexJson mode (comments, unquoted strings, etc.)
24
+ * @param throwOnError - If true (default), throw errors; if false, use silent mode
25
+ */
26
+ constructor(initialJSON?: string, useFlexJsonFlag?: boolean, throwOnError?: boolean);
27
+
28
+ // ============================================================================
29
+ // Properties
30
+ // ============================================================================
31
+
32
+ /** Status code: 0 = OK, negative = error */
33
+ readonly Status: number;
34
+
35
+ /** Error/status message from last operation */
36
+ readonly statusMsg: string;
37
+
38
+ /** JSON type: "object", "array", "string", "number", "boolean", or "null" */
39
+ readonly jsonType: string;
40
+
41
+ /** Serialized JSON string representation */
42
+ jsonString: string;
43
+
44
+ /** Number of items (for object/array) or 1 (for primitives) */
45
+ readonly length: number;
46
+
47
+ /** Key name (for items within an object) */
48
+ key: string;
49
+
50
+ /** Enable FlexJson mode (comments, unquoted strings, single quotes) */
51
+ UseFlexJson: boolean;
52
+
53
+ /**
54
+ * If true (default), throws FlexJsonError on errors.
55
+ * If false, errors are silent and only set Status/statusMsg.
56
+ */
57
+ throwOnError: boolean;
58
+
59
+ /** Preserve whitespace during deserialization */
60
+ keepSpacing: boolean;
61
+
62
+ /** Preserve comments during deserialization */
63
+ keepComments: boolean;
64
+
65
+ /** Allow single-quoted strings (default: true) */
66
+ ALLOW_SINGLE_QUOTE_STRINGS: boolean;
67
+
68
+ /** Encode single quotes when serializing (default: false) */
69
+ ENCODE_SINGLE_QUOTES: boolean;
70
+
71
+ /** Parent FlexJson object (for nested items) */
72
+ Parent: FlexJson | null;
73
+
74
+ /** Get/set the primitive value of this item */
75
+ thisValue: string | number | boolean | null;
76
+
77
+ // ============================================================================
78
+ // Deserialization Methods
79
+ // ============================================================================
80
+
81
+ /**
82
+ * Deserialize a JSON string (strict mode)
83
+ * @param jsonString - The JSON string to parse
84
+ * @param start - Starting position in the string (default: 0)
85
+ * @param okToClip - Allow extra content after JSON (default: false)
86
+ * @returns Status code (0 = success, negative = error)
87
+ */
88
+ Deserialize(jsonString: string, start?: number, okToClip?: boolean): number;
89
+
90
+ /**
91
+ * Deserialize a FlexJson string (flex mode with comments, unquoted strings, etc.)
92
+ * @param jsonString - The FlexJson string to parse
93
+ * @param start - Starting position in the string (default: 0)
94
+ * @param okToClip - Allow extra content after JSON (default: false)
95
+ * @returns Status code (0 = success, negative = error)
96
+ */
97
+ DeserializeFlex(jsonString: string, start?: number, okToClip?: boolean): number;
98
+
99
+ /**
100
+ * Deserialize JSON from a file (strict mode)
101
+ * @param filePath - Path to the JSON file
102
+ * @param okToClip - Allow extra content after JSON (default: false)
103
+ * @returns Status code (0 = success, negative = error)
104
+ */
105
+ DeserializeFile(filePath: string, okToClip?: boolean): number;
106
+
107
+ /**
108
+ * Deserialize FlexJson from a file (flex mode)
109
+ * @param filePath - Path to the FlexJson file
110
+ * @param okToClip - Allow extra content after JSON (default: false)
111
+ * @param spacingFlag - Keep spacing: -1=ignore, 0=false, 1=true (default: 1)
112
+ * @param commentsFlag - Keep comments: -1=ignore, 0=false, 1=true (default: 1)
113
+ * @returns Status code (0 = success, negative = error)
114
+ */
115
+ DeserializeFlexFile(
116
+ filePath: string,
117
+ okToClip?: boolean,
118
+ spacingFlag?: number,
119
+ commentsFlag?: number
120
+ ): number;
121
+
122
+ // ============================================================================
123
+ // Serialization Methods
124
+ // ============================================================================
125
+
126
+ /**
127
+ * Serialize the FlexJson object to a JSON string
128
+ * Updates the jsonString property
129
+ * @returns Status code (0 = success, negative = error)
130
+ */
131
+ SerializeMe(): number;
132
+
133
+ /**
134
+ * Write the FlexJson object to a file
135
+ * @param filePath - Path to write the file
136
+ * @returns Status code (0 = success, -1 = write error, -2 = invalid object)
137
+ */
138
+ WriteToFile(filePath: string): number;
139
+
140
+ // ============================================================================
141
+ // Item Access Methods
142
+ // ============================================================================
143
+
144
+ /**
145
+ * Get a child item by key or index
146
+ * @param idx - Key name (string) or index (number), supports dot notation for nested access
147
+ * @param dotNotation - If true, split idx by "." for nested access (default: true)
148
+ * @returns The child FlexJson item, or a null FlexJson if not found
149
+ */
150
+ item(idx: string | number, dotNotation?: boolean): FlexJson;
151
+
152
+ /**
153
+ * Alias for item()
154
+ * @param idx - Key name (string) or index (number)
155
+ * @param dotNotation - If true, split idx by "." for nested access (default: true)
156
+ * @returns The child FlexJson item, or a null FlexJson if not found
157
+ */
158
+ i(idx: string | number, dotNotation?: boolean): FlexJson;
159
+
160
+ /**
161
+ * Get the primitive value of this item or a child item
162
+ * @param idx - Optional key/index to access child first
163
+ * @param dotNotation - If true, split idx by "." for nested access (default: true)
164
+ * @returns The primitive value, or null for objects/arrays
165
+ */
166
+ v(idx?: string | number, dotNotation?: boolean): string | number | boolean | null;
167
+
168
+ /**
169
+ * Alias for v()
170
+ * @param idx - Optional key/index to access child first
171
+ * @param dotNotation - If true, split idx by "." for nested access (default: true)
172
+ * @returns The primitive value, or null for objects/arrays
173
+ */
174
+ value(idx?: string | number, dotNotation?: boolean): string | number | boolean | null;
175
+
176
+ // ============================================================================
177
+ // Type-Safe Getters
178
+ // ============================================================================
179
+
180
+ /**
181
+ * Get a string value by key with a default fallback
182
+ * @param idx - Key name, supports dot notation
183
+ * @param defaultValue - Value to return if key not found (default: "")
184
+ * @param dotNotation - If true, split idx by "." for nested access (default: true)
185
+ * @returns The string value or default
186
+ */
187
+ getStr(idx: string, defaultValue?: string, dotNotation?: boolean): string;
188
+
189
+ /**
190
+ * Get a numeric value by key with a default fallback
191
+ * @param idx - Key name, supports dot notation
192
+ * @param defaultValue - Value to return if key not found (default: 0)
193
+ * @param dotNotation - If true, split idx by "." for nested access (default: true)
194
+ * @returns The numeric value or default
195
+ */
196
+ getNum(idx: string, defaultValue?: number, dotNotation?: boolean): number;
197
+
198
+ /**
199
+ * Get a boolean value by key with a default fallback
200
+ * @param idx - Key name, supports dot notation
201
+ * @param defaultValue - Value to return if key not found (default: false)
202
+ * @param dotNotation - If true, split idx by "." for nested access (default: true)
203
+ * @returns The boolean value or default
204
+ */
205
+ getBool(idx: string, defaultValue?: boolean, dotNotation?: boolean): boolean;
206
+
207
+ // ============================================================================
208
+ // Type Conversion Methods
209
+ // ============================================================================
210
+
211
+ /**
212
+ * Convert this item's value to a string
213
+ * @param defaultValue - Value to return if conversion fails (default: "")
214
+ * @returns The string value or default
215
+ */
216
+ toStr(defaultValue?: string): string;
217
+
218
+ /**
219
+ * Convert this item's value to a number
220
+ * @param defaultValue - Value to return if conversion fails (default: 0)
221
+ * @returns The numeric value or default
222
+ */
223
+ toNum(defaultValue?: number): number;
224
+
225
+ /**
226
+ * Convert this item's value to a boolean
227
+ * @param defaultValue - Value to return if conversion fails (default: false)
228
+ * @returns The boolean value or default
229
+ */
230
+ toBool(defaultValue?: boolean): boolean;
231
+
232
+ // ============================================================================
233
+ // Mutation Methods
234
+ // ============================================================================
235
+
236
+ /**
237
+ * Add or update a value in the object/array
238
+ * @param value - The value to add (primitive or FlexJson)
239
+ * @param idx - Key name for objects (ignored for arrays)
240
+ * @param dotNotation - If true, split idx by "." for nested access (default: true)
241
+ */
242
+ add(value: string | number | boolean | null | FlexJson, idx?: string, dotNotation?: boolean): void;
243
+
244
+ /**
245
+ * Clear the FlexJson object
246
+ * @param clearParent - Also clear the Parent reference (default: true)
247
+ */
248
+ Clear(clearParent?: boolean): void;
249
+
250
+ /**
251
+ * Convert this FlexJson from an object to an array
252
+ * Keys are discarded, only values are kept
253
+ */
254
+ ConvertToArray(): void;
255
+
256
+ // ============================================================================
257
+ // Utility Methods
258
+ // ============================================================================
259
+
260
+ /**
261
+ * Check if a key exists in the object
262
+ * @param idx - Key name to check, supports dot notation
263
+ * @param dotNotation - If true, split idx by "." for nested access (default: true)
264
+ * @returns True if the key exists
265
+ */
266
+ contains(idx: string, dotNotation?: boolean): boolean;
267
+
268
+ /**
269
+ * Get the index of a key in the object
270
+ * @param search - Key name to find (case-insensitive)
271
+ * @returns The index, or -1 if not found
272
+ */
273
+ indexOfKey(search: string): number;
274
+
275
+ /**
276
+ * Iterate over each item in the object/array
277
+ * @param callback - Function called for each item
278
+ */
279
+ forEach(callback: (item: FlexJson) => void): void;
280
+
281
+ /**
282
+ * Get the internal array of child FlexJson items (for objects/arrays)
283
+ * @param idx - Optional key/index to access child first
284
+ * @param dotNotation - If true, split idx by "." for nested access (default: true)
285
+ * @returns Array of FlexJson items, or null
286
+ */
287
+ toJsonArray(idx?: string | number, dotNotation?: boolean): FlexJson[] | null;
288
+
289
+ /**
290
+ * Set both keepSpacing and keepComments flags
291
+ * @param spacingFlag - -1=ignore, 0=false, 1=true, or boolean
292
+ * @param commentsFlag - -1=ignore, 0=false, 1=true, or boolean
293
+ */
294
+ keepSpacingAndComments(spacingFlag?: number | boolean, commentsFlag?: number | boolean): void;
295
+
296
+ // ============================================================================
297
+ // Static Methods
298
+ // ============================================================================
299
+
300
+ /**
301
+ * Create a FlexJson object representing null
302
+ * @returns A new FlexJson with type "null"
303
+ */
304
+ static CreateNull(): FlexJson;
305
+ }
306
+
307
+ export = FlexJson;
308
+ export { FlexJsonError };