1*592efe25SPierre Pronchery /*
2*592efe25SPierre Pronchery * SPDX-License-Identifier: BSD-2-Clause
3*592efe25SPierre Pronchery *
4*592efe25SPierre Pronchery * Copyright (c) 2025 The FreeBSD Foundation
5*592efe25SPierre Pronchery *
6*592efe25SPierre Pronchery * Portions of this software were developed by
7*592efe25SPierre Pronchery * Tuukka Pasanen <tuukka.pasanen@ilmi.fi> under sponsorship from
8*592efe25SPierre Pronchery * the FreeBSD Foundation
9*592efe25SPierre Pronchery *
10*592efe25SPierre Pronchery * Copyright (C) 2026 Elizabeth Ashford.
11*592efe25SPierre Pronchery */
12*592efe25SPierre Pronchery
13*592efe25SPierre Pronchery #include <stdlib.h>
14*592efe25SPierre Pronchery #include <string.h>
15*592efe25SPierre Pronchery #include "util.h"
16*592efe25SPierre Pronchery
17*592efe25SPierre Pronchery #ifndef CLI__SPDXTOOL__SERIALIZE_H
18*592efe25SPierre Pronchery #define CLI__SPDXTOOL__SERIALIZE_H
19*592efe25SPierre Pronchery
20*592efe25SPierre Pronchery #ifdef __cplusplus
21*592efe25SPierre Pronchery extern "C" {
22*592efe25SPierre Pronchery #endif
23*592efe25SPierre Pronchery
24*592efe25SPierre Pronchery typedef enum spdxtool_serialize_type_
25*592efe25SPierre Pronchery {
26*592efe25SPierre Pronchery SPDXTOOL_SERIALIZE_TYPE_STRING, // JSON string type
27*592efe25SPierre Pronchery SPDXTOOL_SERIALIZE_TYPE_INT, // JSON number type (int)
28*592efe25SPierre Pronchery SPDXTOOL_SERIALIZE_TYPE_BOOL, // JSON bool type
29*592efe25SPierre Pronchery SPDXTOOL_SERIALIZE_TYPE_NULL, // JSON null type
30*592efe25SPierre Pronchery SPDXTOOL_SERIALIZE_TYPE_OBJECT, // JSON object type
31*592efe25SPierre Pronchery SPDXTOOL_SERIALIZE_TYPE_ARRAY // JSON array type
32*592efe25SPierre Pronchery } spdxtool_serialize_type_t;
33*592efe25SPierre Pronchery
34*592efe25SPierre Pronchery typedef struct spdxtool_serialize_value_ {
35*592efe25SPierre Pronchery spdxtool_serialize_type_t type;
36*592efe25SPierre Pronchery union {
37*592efe25SPierre Pronchery char *s;
38*592efe25SPierre Pronchery int i;
39*592efe25SPierre Pronchery bool b;
40*592efe25SPierre Pronchery struct spdxtool_serialize_object_list_ *o;
41*592efe25SPierre Pronchery struct spdxtool_serialize_array_ *a;
42*592efe25SPierre Pronchery } value;
43*592efe25SPierre Pronchery } spdxtool_serialize_value_t;
44*592efe25SPierre Pronchery
45*592efe25SPierre Pronchery typedef struct spdxtool_serialize_object_ {
46*592efe25SPierre Pronchery char *key;
47*592efe25SPierre Pronchery spdxtool_serialize_value_t *value;
48*592efe25SPierre Pronchery } spdxtool_serialize_object_t;
49*592efe25SPierre Pronchery
50*592efe25SPierre Pronchery typedef struct spdxtool_serialize_object_list_ {
51*592efe25SPierre Pronchery pkgconf_list_t entries;
52*592efe25SPierre Pronchery } spdxtool_serialize_object_list_t;
53*592efe25SPierre Pronchery
54*592efe25SPierre Pronchery typedef struct spdxtool_serialize_array_ {
55*592efe25SPierre Pronchery pkgconf_list_t items;
56*592efe25SPierre Pronchery } spdxtool_serialize_array_t;
57*592efe25SPierre Pronchery
58*592efe25SPierre Pronchery bool
59*592efe25SPierre Pronchery spdxtool_serialize_value_to_buf(pkgconf_buffer_t *buffer, spdxtool_serialize_value_t *value, unsigned int indent);
60*592efe25SPierre Pronchery
61*592efe25SPierre Pronchery spdxtool_serialize_value_t *
62*592efe25SPierre Pronchery spdxtool_serialize_value_dup(const spdxtool_serialize_value_t *value);
63*592efe25SPierre Pronchery
64*592efe25SPierre Pronchery spdxtool_serialize_value_t *
65*592efe25SPierre Pronchery spdxtool_serialize_object_add_take(spdxtool_serialize_object_list_t *object_list, const char *key, spdxtool_serialize_value_t* value);
66*592efe25SPierre Pronchery
67*592efe25SPierre Pronchery spdxtool_serialize_object_list_t *
68*592efe25SPierre Pronchery spdxtool_serialize_object_list_new(void);
69*592efe25SPierre Pronchery
70*592efe25SPierre Pronchery spdxtool_serialize_array_t *
71*592efe25SPierre Pronchery spdxtool_serialize_array_new(void);
72*592efe25SPierre Pronchery
73*592efe25SPierre Pronchery spdxtool_serialize_value_t *
74*592efe25SPierre Pronchery spdxtool_serialize_array_add_take(spdxtool_serialize_array_t *array, spdxtool_serialize_value_t* value);
75*592efe25SPierre Pronchery
76*592efe25SPierre Pronchery void
77*592efe25SPierre Pronchery spdxtool_serialize_value_free(spdxtool_serialize_value_t *value);
78*592efe25SPierre Pronchery
79*592efe25SPierre Pronchery void
80*592efe25SPierre Pronchery spdxtool_serialize_object_list_free(spdxtool_serialize_object_list_t *object_list);
81*592efe25SPierre Pronchery
82*592efe25SPierre Pronchery void
83*592efe25SPierre Pronchery spdxtool_serialize_object_free(spdxtool_serialize_object_t *object);
84*592efe25SPierre Pronchery
85*592efe25SPierre Pronchery void
86*592efe25SPierre Pronchery spdxtool_serialize_array_free(spdxtool_serialize_array_t *array);
87*592efe25SPierre Pronchery
88*592efe25SPierre Pronchery /*
89*592efe25SPierre Pronchery * !doc
90*592efe25SPierre Pronchery *
91*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t * spdxtool_serialize_value_string(const char *s)
92*592efe25SPierre Pronchery *
93*592efe25SPierre Pronchery * Construct a JSON string value. The string is copied internally.
94*592efe25SPierre Pronchery * If this return value is not stolen, it must be freed with spdxtool_serialize_value_free().
95*592efe25SPierre Pronchery *
96*592efe25SPierre Pronchery * :param const char *s: String to copy. May be NULL, in which case the value holds NULL.
97*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_STRING.
98*592efe25SPierre Pronchery */
99*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_value_string(const char * s)100*592efe25SPierre Pronchery spdxtool_serialize_value_string(const char *s)
101*592efe25SPierre Pronchery {
102*592efe25SPierre Pronchery if (!s)
103*592efe25SPierre Pronchery return NULL;
104*592efe25SPierre Pronchery
105*592efe25SPierre Pronchery char *sv = strdup(s);
106*592efe25SPierre Pronchery if (!sv)
107*592efe25SPierre Pronchery return NULL;
108*592efe25SPierre Pronchery
109*592efe25SPierre Pronchery spdxtool_serialize_value_t *value = calloc(1, sizeof(spdxtool_serialize_value_t));
110*592efe25SPierre Pronchery if (!value)
111*592efe25SPierre Pronchery {
112*592efe25SPierre Pronchery free(sv);
113*592efe25SPierre Pronchery return NULL;
114*592efe25SPierre Pronchery }
115*592efe25SPierre Pronchery
116*592efe25SPierre Pronchery value->type = SPDXTOOL_SERIALIZE_TYPE_STRING;
117*592efe25SPierre Pronchery value->value.s = sv;
118*592efe25SPierre Pronchery return value;
119*592efe25SPierre Pronchery }
120*592efe25SPierre Pronchery
121*592efe25SPierre Pronchery /*
122*592efe25SPierre Pronchery * !doc
123*592efe25SPierre Pronchery *
124*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_value_int(int d)
125*592efe25SPierre Pronchery *
126*592efe25SPierre Pronchery * Construct a JSON integer value.
127*592efe25SPierre Pronchery * If this return value is not stolen, it must be freed with spdxtool_serialize_value_free().
128*592efe25SPierre Pronchery *
129*592efe25SPierre Pronchery * :param int d: int value.
130*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_INT.
131*592efe25SPierre Pronchery */
132*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_value_int(int i)133*592efe25SPierre Pronchery spdxtool_serialize_value_int(int i)
134*592efe25SPierre Pronchery {
135*592efe25SPierre Pronchery spdxtool_serialize_value_t *value = calloc(1, sizeof(spdxtool_serialize_value_t));
136*592efe25SPierre Pronchery if (!value)
137*592efe25SPierre Pronchery return NULL;
138*592efe25SPierre Pronchery
139*592efe25SPierre Pronchery value->type = SPDXTOOL_SERIALIZE_TYPE_INT;
140*592efe25SPierre Pronchery value->value.i = i;
141*592efe25SPierre Pronchery return value;
142*592efe25SPierre Pronchery }
143*592efe25SPierre Pronchery
144*592efe25SPierre Pronchery /*
145*592efe25SPierre Pronchery * !doc
146*592efe25SPierre Pronchery *
147*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_value_bool(bool b)
148*592efe25SPierre Pronchery *
149*592efe25SPierre Pronchery * Construct a JSON boolean value.
150*592efe25SPierre Pronchery * If this return value is not stolen, it must be freed with spdxtool_serialize_value_free().
151*592efe25SPierre Pronchery *
152*592efe25SPierre Pronchery * :param bool b: Boolean value.
153*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_BOOL.
154*592efe25SPierre Pronchery */
155*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_value_bool(bool b)156*592efe25SPierre Pronchery spdxtool_serialize_value_bool(bool b)
157*592efe25SPierre Pronchery {
158*592efe25SPierre Pronchery spdxtool_serialize_value_t *value = calloc(1, sizeof(spdxtool_serialize_value_t));
159*592efe25SPierre Pronchery if (!value)
160*592efe25SPierre Pronchery return NULL;
161*592efe25SPierre Pronchery
162*592efe25SPierre Pronchery value->type = SPDXTOOL_SERIALIZE_TYPE_BOOL;
163*592efe25SPierre Pronchery value->value.b = b;
164*592efe25SPierre Pronchery return value;
165*592efe25SPierre Pronchery }
166*592efe25SPierre Pronchery
167*592efe25SPierre Pronchery /*
168*592efe25SPierre Pronchery * !doc
169*592efe25SPierre Pronchery *
170*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_value_null(void)
171*592efe25SPierre Pronchery *
172*592efe25SPierre Pronchery * Construct a JSON null value.
173*592efe25SPierre Pronchery * If this return value is not stolen, it must be freed with spdxtool_serialize_value_free().
174*592efe25SPierre Pronchery *
175*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_NULL.
176*592efe25SPierre Pronchery */
177*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_value_null(void)178*592efe25SPierre Pronchery spdxtool_serialize_value_null(void)
179*592efe25SPierre Pronchery {
180*592efe25SPierre Pronchery spdxtool_serialize_value_t *value = calloc(1, sizeof(spdxtool_serialize_value_t));
181*592efe25SPierre Pronchery if (!value)
182*592efe25SPierre Pronchery return NULL;
183*592efe25SPierre Pronchery
184*592efe25SPierre Pronchery value->type = SPDXTOOL_SERIALIZE_TYPE_NULL;
185*592efe25SPierre Pronchery return value;
186*592efe25SPierre Pronchery }
187*592efe25SPierre Pronchery
188*592efe25SPierre Pronchery
189*592efe25SPierre Pronchery /*
190*592efe25SPierre Pronchery * !doc
191*592efe25SPierre Pronchery *
192*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_value_object(spdxtool_serialize_object_list_t *object_list)
193*592efe25SPierre Pronchery *
194*592efe25SPierre Pronchery * Construct a JSON object value wrapping an existing object list.
195*592efe25SPierre Pronchery * The returned value takes ownership of the object list.
196*592efe25SPierre Pronchery * If this return value is not stolen, it must be freed with spdxtool_serialize_value_free().
197*592efe25SPierre Pronchery *
198*592efe25SPierre Pronchery * :param spdxtool_serialize_object_list_t *object_list: Object list to wrap. Ownership transfers to the returned value.
199*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_OBJECT.
200*592efe25SPierre Pronchery */
201*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_value_object(spdxtool_serialize_object_list_t * object_list)202*592efe25SPierre Pronchery spdxtool_serialize_value_object(spdxtool_serialize_object_list_t *object_list)
203*592efe25SPierre Pronchery {
204*592efe25SPierre Pronchery spdxtool_serialize_value_t *value = calloc(1, sizeof(spdxtool_serialize_value_t));
205*592efe25SPierre Pronchery if (!value)
206*592efe25SPierre Pronchery {
207*592efe25SPierre Pronchery spdxtool_serialize_object_list_free(object_list);
208*592efe25SPierre Pronchery return NULL;
209*592efe25SPierre Pronchery }
210*592efe25SPierre Pronchery
211*592efe25SPierre Pronchery value->type = SPDXTOOL_SERIALIZE_TYPE_OBJECT;
212*592efe25SPierre Pronchery value->value.o = object_list;
213*592efe25SPierre Pronchery return value;
214*592efe25SPierre Pronchery }
215*592efe25SPierre Pronchery
216*592efe25SPierre Pronchery /*
217*592efe25SPierre Pronchery * !doc
218*592efe25SPierre Pronchery *
219*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_value_array(spdxtool_serialize_array_t *array)
220*592efe25SPierre Pronchery *
221*592efe25SPierre Pronchery * Construct a JSON array value wrapping an existing array.
222*592efe25SPierre Pronchery * The returned value takes ownership of the array.
223*592efe25SPierre Pronchery * If this return value is not stolen, it must be freed with spdxtool_serialize_value_free().
224*592efe25SPierre Pronchery *
225*592efe25SPierre Pronchery * :param spdxtool_serialize_array_t *array: Array to wrap. Ownership transfers to the returned value.
226*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_ARRAY.
227*592efe25SPierre Pronchery */
228*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_value_array(spdxtool_serialize_array_t * array)229*592efe25SPierre Pronchery spdxtool_serialize_value_array(spdxtool_serialize_array_t *array)
230*592efe25SPierre Pronchery {
231*592efe25SPierre Pronchery spdxtool_serialize_value_t *value = calloc(1, sizeof(spdxtool_serialize_value_t));
232*592efe25SPierre Pronchery if (!value)
233*592efe25SPierre Pronchery {
234*592efe25SPierre Pronchery spdxtool_serialize_array_free(array);
235*592efe25SPierre Pronchery return NULL;
236*592efe25SPierre Pronchery }
237*592efe25SPierre Pronchery
238*592efe25SPierre Pronchery value->type = SPDXTOOL_SERIALIZE_TYPE_ARRAY;
239*592efe25SPierre Pronchery value->value.a = array;
240*592efe25SPierre Pronchery return value;
241*592efe25SPierre Pronchery }
242*592efe25SPierre Pronchery
243*592efe25SPierre Pronchery /*
244*592efe25SPierre Pronchery * !doc
245*592efe25SPierre Pronchery *
246*592efe25SPierre Pronchery * .. c:function:: void spdxtool_serialize_object_add_string(spdxtool_serialize_object_list_t *object_list, const char *key, const char *value)
247*592efe25SPierre Pronchery *
248*592efe25SPierre Pronchery * Add a string key-value pair to a JSON object. The string is copied internally.
249*592efe25SPierre Pronchery * Unconditionally adds the key even if value is NULL.
250*592efe25SPierre Pronchery *
251*592efe25SPierre Pronchery * :param spdxtool_serialize_object_list_t *object_list: Object list to add to.
252*592efe25SPierre Pronchery * :param const char *key: Key string.
253*592efe25SPierre Pronchery * :param const char *value: String value to copy.
254*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_STRING, located in the object.
255*592efe25SPierre Pronchery * This object is not owned by the caller.
256*592efe25SPierre Pronchery */
257*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_object_add_string(spdxtool_serialize_object_list_t * object_list,const char * key,const char * value)258*592efe25SPierre Pronchery spdxtool_serialize_object_add_string(spdxtool_serialize_object_list_t *object_list, const char *key, const char *value)
259*592efe25SPierre Pronchery {
260*592efe25SPierre Pronchery return spdxtool_serialize_object_add_take(object_list, key, spdxtool_serialize_value_string(value));
261*592efe25SPierre Pronchery }
262*592efe25SPierre Pronchery
263*592efe25SPierre Pronchery /*
264*592efe25SPierre Pronchery * !doc
265*592efe25SPierre Pronchery *
266*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_object_add_string_opt(spdxtool_serialize_object_list_t *object_list, const char *key, const char *value)
267*592efe25SPierre Pronchery *
268*592efe25SPierre Pronchery * Add a string key-value pair to a JSON object only if value is non-NULL.
269*592efe25SPierre Pronchery * Use this for optional fields that should be omitted entirely when absent.
270*592efe25SPierre Pronchery *
271*592efe25SPierre Pronchery * :param spdxtool_serialize_object_list_t *object_list: Object list to add to.
272*592efe25SPierre Pronchery * :param const char *key: Key string.
273*592efe25SPierre Pronchery * :param const char *value: String value to copy, or NULL to skip.
274*592efe25SPierre Pronchery * :return: If value is set: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_STRING, located in the object.
275*592efe25SPierre Pronchery * This object is not owned by the caller.
276*592efe25SPierre Pronchery * If value is not set: NULL.
277*592efe25SPierre Pronchery */
278*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_object_add_string_opt(spdxtool_serialize_object_list_t * object_list,const char * key,const char * value)279*592efe25SPierre Pronchery spdxtool_serialize_object_add_string_opt(spdxtool_serialize_object_list_t *object_list, const char *key, const char *value)
280*592efe25SPierre Pronchery {
281*592efe25SPierre Pronchery if (value)
282*592efe25SPierre Pronchery return spdxtool_serialize_object_add_take(object_list, key, spdxtool_serialize_value_string(value));
283*592efe25SPierre Pronchery
284*592efe25SPierre Pronchery return NULL;
285*592efe25SPierre Pronchery }
286*592efe25SPierre Pronchery
287*592efe25SPierre Pronchery /*
288*592efe25SPierre Pronchery * !doc
289*592efe25SPierre Pronchery *
290*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_object_add_int(spdxtool_serialize_object_list_t *object_list, const char *key, int value)
291*592efe25SPierre Pronchery *
292*592efe25SPierre Pronchery * Add a int key-value pair to a JSON object.
293*592efe25SPierre Pronchery *
294*592efe25SPierre Pronchery * :param spdxtool_serialize_object_list_t *object_list: Object list to add to.
295*592efe25SPierre Pronchery * :param const char *key: Key string.
296*592efe25SPierre Pronchery * :param int value: Integer value.
297*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_INT, located in the object.
298*592efe25SPierre Pronchery * This object is not owned by the caller.
299*592efe25SPierre Pronchery */
300*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_object_add_int(spdxtool_serialize_object_list_t * object_list,const char * key,int value)301*592efe25SPierre Pronchery spdxtool_serialize_object_add_int(spdxtool_serialize_object_list_t *object_list, const char *key, int value)
302*592efe25SPierre Pronchery {
303*592efe25SPierre Pronchery return spdxtool_serialize_object_add_take(object_list, key, spdxtool_serialize_value_int(value));
304*592efe25SPierre Pronchery }
305*592efe25SPierre Pronchery
306*592efe25SPierre Pronchery /*
307*592efe25SPierre Pronchery * !doc
308*592efe25SPierre Pronchery *
309*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_object_add_bool(spdxtool_serialize_object_list_t *object_list, const char *key, bool value)
310*592efe25SPierre Pronchery *
311*592efe25SPierre Pronchery * Add a boolean key-value pair to a JSON object.
312*592efe25SPierre Pronchery *
313*592efe25SPierre Pronchery * :param spdxtool_serialize_object_list_t *object_list: Object list to add to.
314*592efe25SPierre Pronchery * :param const char *key: Key string.
315*592efe25SPierre Pronchery * :param bool value: Boolean value.
316*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_BOOL, located in the object.
317*592efe25SPierre Pronchery * This object is not owned by the caller.
318*592efe25SPierre Pronchery */
319*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_object_add_bool(spdxtool_serialize_object_list_t * object_list,const char * key,bool value)320*592efe25SPierre Pronchery spdxtool_serialize_object_add_bool(spdxtool_serialize_object_list_t *object_list, const char *key, bool value)
321*592efe25SPierre Pronchery {
322*592efe25SPierre Pronchery return spdxtool_serialize_object_add_take(object_list, key, spdxtool_serialize_value_bool(value));
323*592efe25SPierre Pronchery }
324*592efe25SPierre Pronchery
325*592efe25SPierre Pronchery /*
326*592efe25SPierre Pronchery * !doc
327*592efe25SPierre Pronchery *
328*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_object_add_null(spdxtool_serialize_object_list_t *object_list, const char *key)
329*592efe25SPierre Pronchery *
330*592efe25SPierre Pronchery * Add a null key-value pair to a JSON object.
331*592efe25SPierre Pronchery *
332*592efe25SPierre Pronchery * :param spdxtool_serialize_object_list_t *object_list: Object list to add to.
333*592efe25SPierre Pronchery * :param const char *key: Key string.
334*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_NULL, located in the object.
335*592efe25SPierre Pronchery * This object is not owned by the caller.
336*592efe25SPierre Pronchery */
337*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_object_add_null(spdxtool_serialize_object_list_t * object_list,const char * key)338*592efe25SPierre Pronchery spdxtool_serialize_object_add_null(spdxtool_serialize_object_list_t *object_list, const char *key)
339*592efe25SPierre Pronchery {
340*592efe25SPierre Pronchery return spdxtool_serialize_object_add_take(object_list, key, spdxtool_serialize_value_null());
341*592efe25SPierre Pronchery }
342*592efe25SPierre Pronchery
343*592efe25SPierre Pronchery /*
344*592efe25SPierre Pronchery * !doc
345*592efe25SPierre Pronchery *
346*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_object_add_object(spdxtool_serialize_object_list_t *object_list, const char *key, spdxtool_serialize_object_list_t *value)
347*592efe25SPierre Pronchery *
348*592efe25SPierre Pronchery * Add an object key-value pair to a JSON object.
349*592efe25SPierre Pronchery * This takes ownership of the object in value unconditionally, freeing on failure.
350*592efe25SPierre Pronchery *
351*592efe25SPierre Pronchery * :param spdxtool_serialize_object_list_t *object_list: Object list to add to.
352*592efe25SPierre Pronchery * :param const char *key: Key string.
353*592efe25SPierre Pronchery * :param spdxtool_serialize_object_list_t *value: Object value to add.
354*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_OBJECT, located in the object.
355*592efe25SPierre Pronchery * This object is not owned by the caller.
356*592efe25SPierre Pronchery */
357*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_object_add_object(spdxtool_serialize_object_list_t * object_list,const char * key,spdxtool_serialize_object_list_t * value)358*592efe25SPierre Pronchery spdxtool_serialize_object_add_object(spdxtool_serialize_object_list_t *object_list, const char *key, spdxtool_serialize_object_list_t *value)
359*592efe25SPierre Pronchery {
360*592efe25SPierre Pronchery return spdxtool_serialize_object_add_take(object_list, key, spdxtool_serialize_value_object(value));
361*592efe25SPierre Pronchery }
362*592efe25SPierre Pronchery
363*592efe25SPierre Pronchery /*
364*592efe25SPierre Pronchery * !doc
365*592efe25SPierre Pronchery *
366*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_object_add_array(spdxtool_serialize_object_list_t *object_list, const char *key, spdxtool_serialize_array_t *value)
367*592efe25SPierre Pronchery *
368*592efe25SPierre Pronchery * Add an array key-value pair to a JSON object.
369*592efe25SPierre Pronchery * This takes ownership of the array in value unconditionally, freeing on failure.
370*592efe25SPierre Pronchery *
371*592efe25SPierre Pronchery * :param spdxtool_serialize_object_list_t *object_list: Object list to add to.
372*592efe25SPierre Pronchery * :param const char *key: Key string.
373*592efe25SPierre Pronchery * :param spdxtool_serialize_array_t *value: Array value to add.
374*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_ARRAY, located in the object.
375*592efe25SPierre Pronchery * This object is not owned by the caller.
376*592efe25SPierre Pronchery */
377*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_object_add_array(spdxtool_serialize_object_list_t * object_list,const char * key,spdxtool_serialize_array_t * value)378*592efe25SPierre Pronchery spdxtool_serialize_object_add_array(spdxtool_serialize_object_list_t *object_list, const char *key, spdxtool_serialize_array_t *value)
379*592efe25SPierre Pronchery {
380*592efe25SPierre Pronchery return spdxtool_serialize_object_add_take(object_list, key, spdxtool_serialize_value_array(value));
381*592efe25SPierre Pronchery }
382*592efe25SPierre Pronchery
383*592efe25SPierre Pronchery /*
384*592efe25SPierre Pronchery * !doc
385*592efe25SPierre Pronchery *
386*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_array_add_string(spdxtool_serialize_array_t *array, const char *value)
387*592efe25SPierre Pronchery *
388*592efe25SPierre Pronchery * Append a string value to a JSON array. The string is copied internally.
389*592efe25SPierre Pronchery * Unconditionally appends even if value is NULL.
390*592efe25SPierre Pronchery *
391*592efe25SPierre Pronchery * :param spdxtool_serialize_array_t *array: Array to append to.
392*592efe25SPierre Pronchery * :param const char *value: String value to copy.
393*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_STRING, located in the array.
394*592efe25SPierre Pronchery * This object is not owned by the caller.
395*592efe25SPierre Pronchery */
396*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_array_add_string(spdxtool_serialize_array_t * array,const char * value)397*592efe25SPierre Pronchery spdxtool_serialize_array_add_string(spdxtool_serialize_array_t *array, const char *value)
398*592efe25SPierre Pronchery {
399*592efe25SPierre Pronchery return spdxtool_serialize_array_add_take(array, spdxtool_serialize_value_string(value));
400*592efe25SPierre Pronchery }
401*592efe25SPierre Pronchery
402*592efe25SPierre Pronchery /*
403*592efe25SPierre Pronchery * !doc
404*592efe25SPierre Pronchery *
405*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_array_add_string_opt(spdxtool_serialize_array_t *a, const char *value)
406*592efe25SPierre Pronchery *
407*592efe25SPierre Pronchery * Append a string value to a JSON array only if value is non-NULL.
408*592efe25SPierre Pronchery * Use this for optional array entries that should be omitted when absent.
409*592efe25SPierre Pronchery *
410*592efe25SPierre Pronchery * :param spdxtool_serialize_array_t *a: Array to append to.
411*592efe25SPierre Pronchery * :param const char *value: String value to copy, or NULL to skip.
412*592efe25SPierre Pronchery * :return: If value is set: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_STRING, located in the array.
413*592efe25SPierre Pronchery * This object is not owned by the caller.
414*592efe25SPierre Pronchery * If value is not set: NULL.
415*592efe25SPierre Pronchery */
416*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_array_add_string_opt(spdxtool_serialize_array_t * array,const char * value)417*592efe25SPierre Pronchery spdxtool_serialize_array_add_string_opt(spdxtool_serialize_array_t *array, const char *value)
418*592efe25SPierre Pronchery {
419*592efe25SPierre Pronchery if (value)
420*592efe25SPierre Pronchery return spdxtool_serialize_array_add_take(array, spdxtool_serialize_value_string(value));
421*592efe25SPierre Pronchery
422*592efe25SPierre Pronchery return NULL;
423*592efe25SPierre Pronchery }
424*592efe25SPierre Pronchery
425*592efe25SPierre Pronchery /*
426*592efe25SPierre Pronchery * !doc
427*592efe25SPierre Pronchery *
428*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_array_add_int(spdxtool_serialize_array_t *array, int value)
429*592efe25SPierre Pronchery *
430*592efe25SPierre Pronchery * Append a int value to a JSON array.
431*592efe25SPierre Pronchery *
432*592efe25SPierre Pronchery * :param spdxtool_serialize_array_t *array: Array to append to.
433*592efe25SPierre Pronchery * :param int value: integer value.
434*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_INT, located in the array.
435*592efe25SPierre Pronchery * This object is not owned by the caller.
436*592efe25SPierre Pronchery */
437*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_array_add_int(spdxtool_serialize_array_t * array,int value)438*592efe25SPierre Pronchery spdxtool_serialize_array_add_int(spdxtool_serialize_array_t *array, int value)
439*592efe25SPierre Pronchery {
440*592efe25SPierre Pronchery return spdxtool_serialize_array_add_take(array, spdxtool_serialize_value_int(value));
441*592efe25SPierre Pronchery }
442*592efe25SPierre Pronchery
443*592efe25SPierre Pronchery /*
444*592efe25SPierre Pronchery * !doc
445*592efe25SPierre Pronchery *
446*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_array_add_bool(spdxtool_serialize_array_t *array, bool value)
447*592efe25SPierre Pronchery *
448*592efe25SPierre Pronchery * Append a boolean value to a JSON array.
449*592efe25SPierre Pronchery *
450*592efe25SPierre Pronchery * :param spdxtool_serialize_array_t *array: Array to append to.
451*592efe25SPierre Pronchery * :param bool value: Boolean value.
452*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_BOOL, located in the array.
453*592efe25SPierre Pronchery * This object is not owned by the caller.
454*592efe25SPierre Pronchery */
455*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_array_add_bool(spdxtool_serialize_array_t * array,bool value)456*592efe25SPierre Pronchery spdxtool_serialize_array_add_bool(spdxtool_serialize_array_t *array, bool value)
457*592efe25SPierre Pronchery {
458*592efe25SPierre Pronchery return spdxtool_serialize_array_add_take(array, spdxtool_serialize_value_bool(value));
459*592efe25SPierre Pronchery }
460*592efe25SPierre Pronchery
461*592efe25SPierre Pronchery /*
462*592efe25SPierre Pronchery * !doc
463*592efe25SPierre Pronchery *
464*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_array_add_null(spdxtool_serialize_array_t *array)
465*592efe25SPierre Pronchery *
466*592efe25SPierre Pronchery * Append a null value to a JSON array.
467*592efe25SPierre Pronchery *
468*592efe25SPierre Pronchery * :param spdxtool_serialize_array_t *array: Array to append to.
469*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_NULL, located in the array.
470*592efe25SPierre Pronchery * This object is not owned by the caller.
471*592efe25SPierre Pronchery */
472*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_array_add_null(spdxtool_serialize_array_t * array)473*592efe25SPierre Pronchery spdxtool_serialize_array_add_null(spdxtool_serialize_array_t *array)
474*592efe25SPierre Pronchery {
475*592efe25SPierre Pronchery return spdxtool_serialize_array_add_take(array, spdxtool_serialize_value_null());
476*592efe25SPierre Pronchery }
477*592efe25SPierre Pronchery
478*592efe25SPierre Pronchery /*
479*592efe25SPierre Pronchery * !doc
480*592efe25SPierre Pronchery *
481*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_array_add_object(spdxtool_serialize_array_t *array, spdxtool_serialize_object_list_t *value)
482*592efe25SPierre Pronchery *
483*592efe25SPierre Pronchery * Append an object value to a JSON array.
484*592efe25SPierre Pronchery * This takes ownership of the object in value unconditionally, freeing on failure.
485*592efe25SPierre Pronchery *
486*592efe25SPierre Pronchery * :param spdxtool_serialize_array_t *array: Array to append to.
487*592efe25SPierre Pronchery * :param spdxtool_serialize_object_list_t *value: Object value.
488*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_OBJECT, located in the array.
489*592efe25SPierre Pronchery * This object is not owned by the caller.
490*592efe25SPierre Pronchery */
491*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_array_add_object(spdxtool_serialize_array_t * array,spdxtool_serialize_object_list_t * value)492*592efe25SPierre Pronchery spdxtool_serialize_array_add_object(spdxtool_serialize_array_t *array, spdxtool_serialize_object_list_t *value)
493*592efe25SPierre Pronchery {
494*592efe25SPierre Pronchery if (!value)
495*592efe25SPierre Pronchery return NULL;
496*592efe25SPierre Pronchery
497*592efe25SPierre Pronchery spdxtool_serialize_value_t *ret = spdxtool_serialize_value_object(value);
498*592efe25SPierre Pronchery if (!ret)
499*592efe25SPierre Pronchery {
500*592efe25SPierre Pronchery // Since we take possession of the pointer unconditionally, clean up.
501*592efe25SPierre Pronchery spdxtool_serialize_object_list_free(value);
502*592efe25SPierre Pronchery return NULL;
503*592efe25SPierre Pronchery }
504*592efe25SPierre Pronchery
505*592efe25SPierre Pronchery return spdxtool_serialize_array_add_take(array, ret);
506*592efe25SPierre Pronchery }
507*592efe25SPierre Pronchery
508*592efe25SPierre Pronchery /*
509*592efe25SPierre Pronchery * !doc
510*592efe25SPierre Pronchery *
511*592efe25SPierre Pronchery * .. c:function:: spdxtool_serialize_value_t *spdxtool_serialize_array_add_array(spdxtool_serialize_array_t *array, spdxtool_serialize_array_t *value)
512*592efe25SPierre Pronchery *
513*592efe25SPierre Pronchery * Append an array value to a JSON array.
514*592efe25SPierre Pronchery * This takes ownership of the array in value unconditionally, freeing on failure.
515*592efe25SPierre Pronchery *
516*592efe25SPierre Pronchery * :param spdxtool_serialize_array_t *array: Array to append to.
517*592efe25SPierre Pronchery * :param spdxtool_serialize_array_t *value: Array value.
518*592efe25SPierre Pronchery * :return: spdxtool_serialize_value_t * of type SPDXTOOL_SERIALIZE_TYPE_ARRAY, located in the array.
519*592efe25SPierre Pronchery * This object is not owned by the caller.
520*592efe25SPierre Pronchery */
521*592efe25SPierre Pronchery static inline spdxtool_serialize_value_t *
spdxtool_serialize_array_add_array(spdxtool_serialize_array_t * array,spdxtool_serialize_array_t * value)522*592efe25SPierre Pronchery spdxtool_serialize_array_add_array(spdxtool_serialize_array_t *array, spdxtool_serialize_array_t *value)
523*592efe25SPierre Pronchery {
524*592efe25SPierre Pronchery if (!value)
525*592efe25SPierre Pronchery return NULL;
526*592efe25SPierre Pronchery
527*592efe25SPierre Pronchery spdxtool_serialize_value_t *ret = spdxtool_serialize_value_array(value);
528*592efe25SPierre Pronchery if (!ret)
529*592efe25SPierre Pronchery {
530*592efe25SPierre Pronchery // Since we take possession of the pointer unconditionally, clean up.
531*592efe25SPierre Pronchery spdxtool_serialize_array_free(value);
532*592efe25SPierre Pronchery return NULL;
533*592efe25SPierre Pronchery }
534*592efe25SPierre Pronchery
535*592efe25SPierre Pronchery return spdxtool_serialize_array_add_take(array, ret);
536*592efe25SPierre Pronchery }
537*592efe25SPierre Pronchery
538*592efe25SPierre Pronchery spdxtool_serialize_value_t *
539*592efe25SPierre Pronchery spdxtool_serialize_sbom(pkgconf_client_t *client, spdxtool_core_agent_t *agent, spdxtool_core_creation_info_t *creation, spdxtool_core_spdx_document_t *spdx);
540*592efe25SPierre Pronchery
541*592efe25SPierre Pronchery #ifdef __cplusplus
542*592efe25SPierre Pronchery }
543*592efe25SPierre Pronchery #endif
544*592efe25SPierre Pronchery
545*592efe25SPierre Pronchery #endif
546