Debian Codegraph @

modules/lsp-runtime-lib/include/lsp-plug.in/fmt/json/Serializer.h

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 17 окт. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef LSP_PLUG_IN_FMT_JSON_SERIALIZER_H_
23 #define LSP_PLUG_IN_FMT_JSON_SERIALIZER_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26 #include <lsp-plug.in/common/types.h>
27 #include <lsp-plug.in/io/IOutStream.h>
28 #include <lsp-plug.in/io/IOutSequence.h>
29 #include <lsp-plug.in/io/Path.h>
30 #include <lsp-plug.in/fmt/json/token.h>
31 #include <lsp-plug.in/lltl/darray.h>
32
33 namespace lsp
34 {
35 namespace json
36 {
37
38 class Serializer
39 {
40 protected:
41 enum pmode_t
42 {
43 WRITE_ROOT,
44 WRITE_ARRAY,
45 WRITE_OBJECT
46 };
47
48 enum serialize_flags_t
49 {
50 SF_PROPERTY = 1 << 0,
51 SF_VALUE = 1 << 1,
52 SF_COMMA = 1 << 2,
53 SF_CONTENT = 1 << 3
54 };
55
56 typedef struct state_t
57 {
58 pmode_t mode;
59 size_t flags;
60 size_t ident;
61 } state_t;
62
63 protected:
64 io::IOutSequence *pOut;
65 size_t nWFlags;
66 state_t sState;
67 lltl::darray<state_t> sStack;
68 serial_flags_t sSettings;
69
70 protected:
71 inline status_t push_state(pmode_t state);
72 status_t pop_state();
73 inline void copy_settings(const serial_flags_t *flags);
74
75 inline char hex(int x);
76 status_t write_raw(const char *buf, int len);
77 status_t write_literal(const LSPString *value);
78 inline status_t emit_comma();
79 inline status_t emit_separator();
80
81 public:
82 explicit Serializer();
83 Serializer(const Serializer &) = delete;
84 Serializer(Serializer &&) = delete;
85 virtual ~Serializer();
86
87 Serializer & operator = (const Serializer &) = delete;
88 Serializer & operator = (Serializer &&) = delete;
89
90 public:
91 /**
92 * Open serializer
93 * @param path UTF-8 path to the file
94 * @param settings serialization flags
95 * @param charset character set
96 * @return status of operation
97 */
98 status_t open(const char *path, const serial_flags_t *settings, const char *charset = NULL);
99
100 /**
101 * Open serializer
102 * @param path string representation of path to the file
103 * @param settings serialization flags
104 * @param charset character set
105 * @return status of operation
106 */
107 status_t open(const LSPString *path, const serial_flags_t *settings, const char *charset = NULL);
108
109 /**
110 * Open serializer
111 * @param path path to the file
112 * @param settings serialization flags
113 * @param charset character set
114 * @return status of operation
115 */
116 status_t open(const io::Path *path, const serial_flags_t *settings, const char *charset = NULL);
117
118 /**
119 * Wrap string with serializer
120 * @param str string to wrap
121 * @param settings serialization flags
122 * @return status of operation
123 */
124 status_t wrap(LSPString *str, const serial_flags_t *settings);
125
126 /**
127 * Wrap input sequence with serializer
128 * @param seq sequence to use for reads
129 * @param settings serialization flags
130 * @param flags wrapping flags
131 * @return status of operation
132 */
133 status_t wrap(io::IOutSequence *seq, const serial_flags_t *settings, size_t flags = WRAP_NONE);
134
135 /**
136 * Wrap input stream with serializer
137 * @param os output stream
138 * @param settings serialization flags
139 * @param flags wrapping flags
140 * @param charset character set
141 * @return status of operation
142 */
143 status_t wrap(io::IOutStream *os, const serial_flags_t *settings, size_t flags = WRAP_NONE, const char *charset = NULL);
144
145 /**
146 * Close serializer
147 * @return status of operation
148 */
149 status_t close();
150
151 public:
152
153 /**
154 * Write JSON event to output
155 * @param event event to write
156 * @return status of operation
157 */
158 status_t write(const event_t *event);
159
160 /**
161 * Write comma
162 * @return status of operation
163 */
164 status_t write_comma();
165
166 /**
167 * Write new line
168 * @return status of operation
169 */
170 status_t writeln();
171
172 /**
173 * Write integer value
174 * @param value value to write
175 * @return status of operation
176 */
177 status_t write_int(int32_t value);
178
179 /**
180 * Write integer value
181 * @param value value to write
182 * @return status of operation
183 */
184 status_t write_int(uint32_t value);
185
186 /**
187 * Write integer value
188 * @param value value to write
189 * @return status of operation
190 */
191 status_t write_int(int64_t value);
192
193 /**
194 * Write integer value
195 * @param value value to write
196 * @return status of operation
197 */
198 status_t write_int(uint64_t value);
199
200 /**
201 * Write hexadecimal value
202 * @param value value to write
203 * @return status of operation
204 */
205 status_t write_hex(int32_t value);
206
207 /**
208 * Write hexadecimal value
209 * @param value value to write
210 * @return status of operation
211 */
212 status_t write_hex(uint32_t value);
213
214 /**
215 * Write hexadecimal value
216 * @param value value to write
217 * @return status of operation
218 */
219 status_t write_hex(int64_t value);
220
221 /**
222 * Write hexadecimal value
223 * @param value value to write
224 * @return status of operation
225 */
226 status_t write_hex(uint64_t value);
227
228 /**
229 * Write double
230 * @param value double value
231 * @return status of operation
232 */
233 status_t write_double(double value);
234
235 /**
236 * Write double
237 * @param value double value
238 * @param fmt format specifier (same as for sprintf)
239 * @return status of operation
240 */
241 status_t write_double(double value, const char *fmt);
242
243 /**
244 * Write boolean value
245 * @param value value to write
246 * @return status of operation
247 */
248 status_t write_bool(bool value);
249
250 /**
251 * Write string value
252 * @param value value to write, NULL will be interpreted as NULL value
253 * @param charset character set
254 * @return status of operation
255 */
256 status_t write_string(const char *value, const char *charset);
257
258 /**
259 * Write string in UTF-8
260 * @param value value to write, NULL will be interpreted as NULL value
261 * @return status of operation
262 */
263 status_t write_string(const char *value);
264
265 /**
266 * Write string value
267 * @param value value to write, NULL will be interpreted as NULL value
268 * @return status of operation
269 */
270 status_t write_string(const LSPString *value);
271
272 /**
273 * Write null value
274 * @return status of operation
275 */
276 status_t write_null();
277
278 /**
279 * Write comment
280 * @param value comment to write
281 * @return status of operation
282 */
283 status_t write_comment(const char *value, const char *charset);
284
285 /**
286 * Write comment in UTF-8 encoding
287 * @param value comment to write
288 * @return status of operation
289 */
290 status_t write_comment(const char *value);
291
292 /**
293 * Write comment
294 * @param value comment to write
295 * @return status of operation
296 */
297 status_t write_comment(const LSPString *value);
298
299 /**
300 * Write property
301 * @param name property name
302 * @param charset character set
303 * @return status of operation
304 */
305 status_t write_property(const char *name, const char *charset);
306
307 /**
308 * Write property in UTF-8 encoding
309 * @param name property name
310 * @return status of operation
311 */
312 status_t write_property(const char *name);
313
314 /**
315 * Write property
316 * @param name property name
317 * @return status of operation
318 */
319 status_t write_property(const LSPString *name);
320
321 /**
322 * Write hexadecimal property
323 * @param key UTF-8 property name
324 * @param value value to write
325 * @return status of operation
326 */
327 status_t prop_int(const char *key, int32_t value);
328
329
330 /**
331 * Write integer property
332 * @param key property name
333 * @param value value to write
334 * @return status of operation
335 */
336 status_t prop_int(const LSPString *key, int32_t value);
337
338 /**
339 * Write hexadecimal property
340 * @param key UTF-8 property name
341 * @param value value to write
342 * @return status of operation
343 */
344 status_t prop_int(const char *key, uint32_t value);
345
346
347 /**
348 * Write integer property
349 * @param key property name
350 * @param value value to write
351 * @return status of operation
352 */
353 status_t prop_int(const LSPString *key, uint32_t value);
354
355 /**
356 * Write integer property
357 * @param key UTF-8 property name
358 * @param value value to write
359 * @return status of operation
360 */
361 status_t prop_int(const char *key, int64_t value);
362
363 /**
364 * Write integer property
365 * @param key property name
366 * @param value value to write
367 * @return status of operation
368 */
369 status_t prop_int(const LSPString *key, int64_t value);
370
371 /**
372 * Write integer property
373 * @param key UTF-8 property name
374 * @param value value to write
375 * @return status of operation
376 */
377 status_t prop_int(const char *key, uint64_t value);
378
379 /**
380 * Write integer property
381 * @param key property name
382 * @param value value to write
383 * @return status of operation
384 */
385 status_t prop_int(const LSPString *key, uint64_t value);
386
387 /**
388 * Write hexadecimal property
389 * @param key UTF-8 property name
390 * @param value value to write
391 * @return status of operation
392 */
393 status_t prop_hex(const char *key, int32_t value);
394
395 /**
396 * Write hexadecimal property
397 * @param key property name
398 * @param value value to write
399 * @return status of operation
400 */
401 status_t prop_hex(const LSPString *key, int32_t value);
402
403 /**
404 * Write hexadecimal property
405 * @param key UTF-8 property name
406 * @param value value to write
407 * @return status of operation
408 */
409 status_t prop_hex(const char *key, uint32_t value);
410
411 /**
412 * Write hexadecimal property
413 * @param key property name
414 * @param value value to write
415 * @return status of operation
416 */
417 status_t prop_hex(const LSPString *key, uint32_t value);
418
419 /**
420 * Write hexadecimal property
421 * @param key UTF-8 property name
422 * @param value value to write
423 * @return status of operation
424 */
425 status_t prop_hex(const char *key, int64_t value);
426
427 /**
428 * Write hexadecimal property
429 * @param key property name
430 * @param value value to write
431 * @return status of operation
432 */
433 status_t prop_hex(const LSPString *key, int64_t value);
434
435 /**
436 * Write hexadecimal property
437 * @param key UTF-8 property name
438 * @param value value to write
439 * @return status of operation
440 */
441 status_t prop_hex(const char *key, uint64_t value);
442
443 /**
444 * Write hexadecimal property
445 * @param key property name
446 * @param value value to write
447 * @return status of operation
448 */
449 status_t prop_hex(const LSPString *key, uint64_t value);
450
451 /**
452 * Write double property
453 * @param key UTF-8 property name
454 * @param value double value
455 * @return status of operation
456 */
457 status_t prop_double(const char *key, double value);
458
459 /**
460 * Write double property
461 * @param key property name
462 * @param value double value
463 * @return status of operation
464 */
465 status_t prop_double(const LSPString *key, double value);
466
467 /**
468 * Write double property
469 * @param key UTF-8 property name
470 * @param value double value
471 * @param fmt format specifier (same as for sprintf)
472 * @return status of operation
473 */
474 status_t prop_double(const char *key, double value, const char *fmt);
475
476 /**
477 * Write double property
478 * @param key property name
479 * @param value double value
480 * @param fmt format specifier (same as for sprintf)
481 * @return status of operation
482 */
483 status_t prop_double(const LSPString *key, double value, const char *fmt);
484
485 /**
486 * Write boolean property
487 * @param key UTF-8 property name
488 * @param value value to write
489 * @return status of operation
490 */
491 status_t prop_bool(const char *key, bool value);
492
493 /**
494 * Write boolean property
495 * @param key property name
496 * @param value value to write
497 * @return status of operation
498 */
499 status_t prop_bool(const LSPString *key, bool value);
500
501 /**
502 * Write string in UTF-8
503 * @param key UTF-8 property name
504 * @param value value to write, NULL will be interpreted as NULL value
505 * @return status of operation
506 */
507 status_t prop_string(const char *key, const char *value);
508
509 /**
510 * Write string value
511 * @param key UTF-8 property name
512 * @param value value to write, NULL will be interpreted as NULL value
513 * @return status of operation
514 */
515 status_t prop_string(const char *key, const LSPString *value);
516
517 /**
518 * Write string in UTF-8
519 * @param key UTF-8 property name
520 * @param value value to write, NULL will be interpreted as NULL value
521 * @return status of operation
522 */
523 status_t prop_string(const LSPString *key, const char *value);
524
525 /**
526 * Write string value
527 * @param key UTF-8 property name
528 * @param value value to write, NULL will be interpreted as NULL value
529 * @return status of operation
530 */
531 status_t prop_string(const LSPString *key, const LSPString *value);
532
533 /**
534 * Write null property
535 * @param key UTF-8 property name
536 * @return status of operation
537 */
538 status_t prop_null(const char *key);
539
540 /**
541 * Write null property
542 * @param key property name
543 * @return status of operation
544 */
545 status_t prop_null(const LSPString *key);
546
547 /**
548 * Write beginning of the object
549 * @return status of operation
550 */
551 status_t start_object();
552
553 /**
554 * Write the end of the object
555 * @return status of operation
556 */
557 status_t end_object();
558
559 /**
560 * Write beginning of the array
561 * @return status of operation
562 */
563 status_t start_array();
564
565 /**
566 * Write the end of the array
567 * @return status of operation
568 */
569 status_t end_array();
570
571 public:
572 /**
573 * Get multiline flag
574 * @return multiline flag
575 */
576 inline bool get_multiline() const { return sSettings.multiline; }
577
578 /**
579 * Get multiline flag
580 * @return multiline flag
581 */
582 inline bool set_multiline(bool multiline)
583 {
584 bool prev = sSettings.multiline;
585 sSettings.multiline = multiline;
586 return prev;
587 }
588 };
589
590 } /* namespace json */
591 } /* namespace lsp */
592
593 #endif /* LSP_PLUG_IN_FMT_JSON_SERIALIZER_H_ */