aboutsummaryrefslogtreecommitdiff
path: root/src/json/json_pack.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/json/json_pack.c')
-rw-r--r--src/json/json_pack.c354
1 files changed, 0 insertions, 354 deletions
diff --git a/src/json/json_pack.c b/src/json/json_pack.c
deleted file mode 100644
index 816373eaf..000000000
--- a/src/json/json_pack.c
+++ /dev/null
@@ -1,354 +0,0 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file json/json_pack.c
22 * @brief functions to pack JSON objects
23 * @author Christian Grothoff
24 */
25#include "platform.h"
26#include "gnunet_json_lib.h"
27
28
29json_t *
30GNUNET_JSON_pack_ (struct GNUNET_JSON_PackSpec spec[])
31{
32 json_t *ret;
33
34 if (NULL == spec[0].field_name)
35 {
36 ret = spec[0].object;
37 spec[0].object = NULL;
38 return ret;
39 }
40 ret = json_object ();
41 GNUNET_assert (NULL != ret);
42 for (unsigned int i = 0;
43 NULL != spec[i].field_name;
44 i++)
45 {
46 if (NULL == spec[i].object)
47 {
48 if (! spec[i].allow_null)
49 {
50 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
51 "NULL not allowed for `%s'\n",
52 spec[i].field_name);
53 GNUNET_assert (0);
54 }
55 }
56 else
57 {
58 GNUNET_assert (0 ==
59 json_object_set_new (ret,
60 spec[i].field_name,
61 spec[i].object));
62 spec[i].object = NULL;
63 }
64 }
65 return ret;
66}
67
68
69struct GNUNET_JSON_PackSpec
70GNUNET_JSON_pack_end_ (void)
71{
72 struct GNUNET_JSON_PackSpec ps = {
73 .field_name = NULL
74 };
75
76 return ps;
77}
78
79
80struct GNUNET_JSON_PackSpec
81GNUNET_JSON_pack_allow_null (struct GNUNET_JSON_PackSpec in)
82{
83 in.allow_null = true;
84 return in;
85}
86
87
88struct GNUNET_JSON_PackSpec
89GNUNET_JSON_pack_bool (const char *name,
90 bool b)
91{
92 struct GNUNET_JSON_PackSpec ps = {
93 .field_name = name,
94 .object = json_boolean (b)
95 };
96
97 return ps;
98}
99
100
101struct GNUNET_JSON_PackSpec
102GNUNET_JSON_pack_string (const char *name,
103 const char *s)
104{
105 struct GNUNET_JSON_PackSpec ps = {
106 .field_name = name,
107 .object = json_string (s)
108 };
109
110 return ps;
111}
112
113
114struct GNUNET_JSON_PackSpec
115GNUNET_JSON_pack_uint64 (const char *name,
116 uint64_t num)
117{
118 struct GNUNET_JSON_PackSpec ps = {
119 .field_name = name,
120 .object = json_integer ((json_int_t) num)
121 };
122
123#if JSON_INTEGER_IS_LONG_LONG
124 GNUNET_assert (num <= LLONG_MAX);
125#else
126 GNUNET_assert (num <= LONG_MAX);
127#endif
128 return ps;
129}
130
131
132struct GNUNET_JSON_PackSpec
133GNUNET_JSON_pack_int64 (const char *name,
134 int64_t num)
135{
136 struct GNUNET_JSON_PackSpec ps = {
137 .field_name = name,
138 .object = json_integer ((json_int_t) num)
139 };
140
141#if JSON_INTEGER_IS_LONG_LONG
142 GNUNET_assert (num <= LLONG_MAX);
143 GNUNET_assert (num >= LLONG_MIN);
144#else
145 GNUNET_assert (num <= LONG_MAX);
146 GNUNET_assert (num >= LONG_MIN);
147#endif
148 return ps;
149}
150
151
152struct GNUNET_JSON_PackSpec
153GNUNET_JSON_pack_object_steal (const char *name,
154 json_t *o)
155{
156 struct GNUNET_JSON_PackSpec ps = {
157 .field_name = name,
158 .object = o
159 };
160
161 if (NULL == o)
162 return ps;
163 if (! json_is_object (o))
164 {
165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
166 "Expected JSON object for field `%s'\n",
167 name);
168 GNUNET_assert (0);
169 }
170 return ps;
171}
172
173
174struct GNUNET_JSON_PackSpec
175GNUNET_JSON_pack_object_incref (const char *name,
176 json_t *o)
177{
178 struct GNUNET_JSON_PackSpec ps = {
179 .field_name = name,
180 .object = o
181 };
182
183 if (NULL == o)
184 return ps;
185 (void) json_incref (o);
186 if (! json_is_object (o))
187 {
188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189 "Expected JSON object for field `%s'\n",
190 name);
191 GNUNET_assert (0);
192 }
193 return ps;
194}
195
196
197struct GNUNET_JSON_PackSpec
198GNUNET_JSON_pack_array_steal (const char *name,
199 json_t *a)
200{
201 struct GNUNET_JSON_PackSpec ps = {
202 .field_name = name,
203 .object = a
204 };
205
206 if (NULL == a)
207 return ps;
208 if (! json_is_array (a))
209 {
210 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
211 "Expected JSON array for field `%s'\n",
212 name);
213 GNUNET_assert (0);
214 }
215 return ps;
216}
217
218
219struct GNUNET_JSON_PackSpec
220GNUNET_JSON_pack_array_incref (const char *name,
221 json_t *a)
222{
223 struct GNUNET_JSON_PackSpec ps = {
224 .field_name = name,
225 .object = a
226 };
227
228 if (NULL == a)
229 return ps;
230 (void) json_incref (a);
231 if (! json_is_array (a))
232 {
233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
234 "Expected JSON array for field `%s'\n",
235 name);
236 GNUNET_assert (0);
237 }
238 return ps;
239}
240
241
242struct GNUNET_JSON_PackSpec
243GNUNET_JSON_pack_data_varsize (const char *name,
244 const void *blob,
245 size_t blob_size)
246{
247 struct GNUNET_JSON_PackSpec ps = {
248 .field_name = name,
249 .object = (NULL != blob)
250 ? GNUNET_JSON_from_data (blob,
251 blob_size)
252 : NULL
253 };
254
255 return ps;
256}
257
258
259struct GNUNET_JSON_PackSpec
260GNUNET_JSON_pack_data64_varsize (const char *name,
261 const void *blob,
262 size_t blob_size)
263{
264 struct GNUNET_JSON_PackSpec ps = {
265 .field_name = name,
266 .object = (NULL != blob)
267 ? GNUNET_JSON_from_data64 (blob,
268 blob_size)
269 : NULL
270 };
271
272 return ps;
273}
274
275
276struct GNUNET_JSON_PackSpec
277GNUNET_JSON_pack_timestamp (const char *name,
278 struct GNUNET_TIME_Timestamp t)
279{
280 struct GNUNET_JSON_PackSpec ps = {
281 .field_name = name
282 };
283
284 if (! GNUNET_TIME_absolute_is_zero (t.abs_time))
285 {
286 ps.object = GNUNET_JSON_from_timestamp (t);
287 GNUNET_assert (NULL != ps.object);
288 }
289 else
290 {
291 ps.object = NULL;
292 }
293 return ps;
294}
295
296
297struct GNUNET_JSON_PackSpec
298GNUNET_JSON_pack_timestamp_nbo (const char *name,
299 struct GNUNET_TIME_TimestampNBO at)
300{
301 return GNUNET_JSON_pack_timestamp (name,
302 GNUNET_TIME_timestamp_ntoh (at));
303}
304
305
306struct GNUNET_JSON_PackSpec
307GNUNET_JSON_pack_time_rel (const char *name,
308 struct GNUNET_TIME_Relative rt)
309{
310 json_t *json;
311
312 json = GNUNET_JSON_from_time_rel (rt);
313 GNUNET_assert (NULL != json);
314 return GNUNET_JSON_pack_object_steal (name,
315 json);
316}
317
318
319struct GNUNET_JSON_PackSpec
320GNUNET_JSON_pack_time_rel_nbo (const char *name,
321 struct GNUNET_TIME_RelativeNBO rt)
322{
323 return GNUNET_JSON_pack_time_rel (name,
324 GNUNET_TIME_relative_ntoh (rt));
325}
326
327
328struct GNUNET_JSON_PackSpec
329GNUNET_JSON_pack_rsa_public_key (const char *name,
330 const struct GNUNET_CRYPTO_RsaPublicKey *pk)
331{
332 struct GNUNET_JSON_PackSpec ps = {
333 .field_name = name,
334 .object = GNUNET_JSON_from_rsa_public_key (pk)
335 };
336
337 return ps;
338}
339
340
341struct GNUNET_JSON_PackSpec
342GNUNET_JSON_pack_rsa_signature (const char *name,
343 const struct GNUNET_CRYPTO_RsaSignature *sig)
344{
345 struct GNUNET_JSON_PackSpec ps = {
346 .field_name = name,
347 .object = GNUNET_JSON_from_rsa_signature (sig)
348 };
349
350 return ps;
351}
352
353
354/* end of json_pack.c */