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.c325
1 files changed, 0 insertions, 325 deletions
diff --git a/src/json/json_pack.c b/src/json/json_pack.c
deleted file mode 100644
index 92f8b4535..000000000
--- a/src/json/json_pack.c
+++ /dev/null
@@ -1,325 +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 ret = json_object ();
35 GNUNET_assert (NULL != ret);
36 for (unsigned int i = 0;
37 NULL != spec[i].field_name;
38 i++)
39 {
40 if (NULL == spec[i].object)
41 {
42 GNUNET_assert (spec[i].allow_null);
43 }
44 else
45 {
46 GNUNET_assert (0 ==
47 json_object_set_new (ret,
48 spec[i].field_name,
49 spec[i].object));
50 spec[i].object = NULL;
51 }
52 }
53 return ret;
54}
55
56
57struct GNUNET_JSON_PackSpec
58GNUNET_JSON_pack_end_ (void)
59{
60 struct GNUNET_JSON_PackSpec ps = {
61 .field_name = NULL
62 };
63
64 return ps;
65}
66
67
68struct GNUNET_JSON_PackSpec
69GNUNET_JSON_pack_allow_null (struct GNUNET_JSON_PackSpec in)
70{
71 in.allow_null = true;
72 return in;
73}
74
75
76struct GNUNET_JSON_PackSpec
77GNUNET_JSON_pack_bool (const char *name,
78 bool b)
79{
80 struct GNUNET_JSON_PackSpec ps = {
81 .field_name = name,
82 .object = json_boolean (b)
83 };
84
85 return ps;
86}
87
88
89struct GNUNET_JSON_PackSpec
90GNUNET_JSON_pack_string (const char *name,
91 const char *s)
92{
93 struct GNUNET_JSON_PackSpec ps = {
94 .field_name = name,
95 .object = json_string (s)
96 };
97
98 return ps;
99}
100
101
102struct GNUNET_JSON_PackSpec
103GNUNET_JSON_pack_uint64 (const char *name,
104 uint64_t num)
105{
106 struct GNUNET_JSON_PackSpec ps = {
107 .field_name = name,
108 .object = json_integer ((json_int_t) num)
109 };
110
111#if JSON_INTEGER_IS_LONG_LONG
112 GNUNET_assert (num <= LLONG_MAX);
113#else
114 GNUNET_assert (num <= LONG_MAX);
115#endif
116 return ps;
117}
118
119
120struct GNUNET_JSON_PackSpec
121GNUNET_JSON_pack_int64 (const char *name,
122 int64_t num)
123{
124 struct GNUNET_JSON_PackSpec ps = {
125 .field_name = name,
126 .object = json_integer ((json_int_t) num)
127 };
128
129#if JSON_INTEGER_IS_LONG_LONG
130 GNUNET_assert (num <= LLONG_MAX);
131 GNUNET_assert (num >= LLONG_MIN);
132#else
133 GNUNET_assert (num <= LONG_MAX);
134 GNUNET_assert (num >= LONG_MIN);
135#endif
136 return ps;
137}
138
139
140struct GNUNET_JSON_PackSpec
141GNUNET_JSON_pack_object_steal (const char *name,
142 json_t *o)
143{
144 struct GNUNET_JSON_PackSpec ps = {
145 .field_name = name,
146 .object = o
147 };
148
149 if (NULL == o)
150 return ps;
151 if (! json_is_object (o))
152 {
153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
154 "Expected JSON object for field `%s'\n",
155 name);
156 GNUNET_assert (0);
157 }
158 return ps;
159}
160
161
162struct GNUNET_JSON_PackSpec
163GNUNET_JSON_pack_object_incref (const char *name,
164 json_t *o)
165{
166 struct GNUNET_JSON_PackSpec ps = {
167 .field_name = name,
168 .object = o
169 };
170
171 if (NULL == o)
172 return ps;
173 (void) json_incref (o);
174 if (! json_is_object (o))
175 {
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
177 "Expected JSON object for field `%s'\n",
178 name);
179 GNUNET_assert (0);
180 }
181 return ps;
182}
183
184
185struct GNUNET_JSON_PackSpec
186GNUNET_JSON_pack_array_steal (const char *name,
187 json_t *a)
188{
189 struct GNUNET_JSON_PackSpec ps = {
190 .field_name = name,
191 .object = a
192 };
193
194 if (NULL == a)
195 return ps;
196 if (! json_is_array (a))
197 {
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
199 "Expected JSON array for field `%s'\n",
200 name);
201 GNUNET_assert (0);
202 }
203 return ps;
204}
205
206
207struct GNUNET_JSON_PackSpec
208GNUNET_JSON_pack_array_incref (const char *name,
209 json_t *a)
210{
211 struct GNUNET_JSON_PackSpec ps = {
212 .field_name = name,
213 .object = a
214 };
215
216 if (NULL == a)
217 return ps;
218 (void) json_incref (a);
219 if (! json_is_array (a))
220 {
221 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
222 "Expected JSON array for field `%s'\n",
223 name);
224 GNUNET_assert (0);
225 }
226 return ps;
227}
228
229
230struct GNUNET_JSON_PackSpec
231GNUNET_JSON_pack_data_varsize (const char *name,
232 const void *blob,
233 size_t blob_size)
234{
235 struct GNUNET_JSON_PackSpec ps = {
236 .field_name = name,
237 .object = (NULL != blob)
238 ? GNUNET_JSON_from_data (blob,
239 blob_size)
240 : NULL
241 };
242
243 return ps;
244}
245
246
247struct GNUNET_JSON_PackSpec
248GNUNET_JSON_pack_time_abs (const char *name,
249 struct GNUNET_TIME_Absolute at)
250{
251 struct GNUNET_JSON_PackSpec ps = {
252 .field_name = name
253 };
254
255 if (0 != at.abs_value_us)
256 {
257 ps.object = GNUNET_JSON_from_time_abs (at);
258 GNUNET_assert (NULL != ps.object);
259 }
260 else
261 {
262 ps.object = NULL;
263 }
264 return ps;
265}
266
267
268struct GNUNET_JSON_PackSpec
269GNUNET_JSON_pack_time_abs_nbo (const char *name,
270 struct GNUNET_TIME_AbsoluteNBO at)
271{
272 return GNUNET_JSON_pack_time_abs (name,
273 GNUNET_TIME_absolute_ntoh (at));
274}
275
276
277struct GNUNET_JSON_PackSpec
278GNUNET_JSON_pack_time_rel (const char *name,
279 struct GNUNET_TIME_Relative rt)
280{
281 json_t *json;
282
283 json = GNUNET_JSON_from_time_rel (rt);
284 GNUNET_assert (NULL != json);
285 return GNUNET_JSON_pack_object_steal (name,
286 json);
287}
288
289
290struct GNUNET_JSON_PackSpec
291GNUNET_JSON_pack_time_rel_nbo (const char *name,
292 struct GNUNET_TIME_RelativeNBO rt)
293{
294 return GNUNET_JSON_pack_time_rel (name,
295 GNUNET_TIME_relative_ntoh (rt));
296}
297
298
299struct GNUNET_JSON_PackSpec
300GNUNET_JSON_pack_rsa_public_key (const char *name,
301 const struct GNUNET_CRYPTO_RsaPublicKey *pk)
302{
303 struct GNUNET_JSON_PackSpec ps = {
304 .field_name = name,
305 .object = GNUNET_JSON_from_rsa_public_key (pk)
306 };
307
308 return ps;
309}
310
311
312struct GNUNET_JSON_PackSpec
313GNUNET_JSON_pack_rsa_signature (const char *name,
314 const struct GNUNET_CRYPTO_RsaSignature *sig)
315{
316 struct GNUNET_JSON_PackSpec ps = {
317 .field_name = name,
318 .object = GNUNET_JSON_from_rsa_signature (sig)
319 };
320
321 return ps;
322}
323
324
325/* end of json_pack.c */