GNUnet 0.21.1
json_pack.c
Go to the documentation of this file.
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 */
25#include "platform.h"
26#include "gnunet_json_lib.h"
27
28
29json_t *
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 {
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
71{
72 struct GNUNET_JSON_PackSpec ps = {
73 .field_name = NULL
74 };
75
76 return ps;
77}
78
79
82{
83 in.allow_null = true;
84 return in;
85}
86
87
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
103 double f)
104{
105 struct GNUNET_JSON_PackSpec ps = {
106 .field_name = name,
107 .object = json_real (f)
108 };
109
110 return ps;
111}
112
113
116 const char *s)
117{
118 struct GNUNET_JSON_PackSpec ps = {
119 .field_name = name,
120 .object = json_string (s)
121 };
122
123 return ps;
124}
125
126
129 uint64_t num)
130{
131 struct GNUNET_JSON_PackSpec ps = {
132 .field_name = name,
133 .object = json_integer ((json_int_t) num)
134 };
135
136#if JSON_INTEGER_IS_LONG_LONG
137 GNUNET_assert (num <= LLONG_MAX);
138#else
139 GNUNET_assert (num <= LONG_MAX);
140#endif
141 return ps;
142}
143
144
147 int64_t num)
148{
149 struct GNUNET_JSON_PackSpec ps = {
150 .field_name = name,
151 .object = json_integer ((json_int_t) num)
152 };
153
154#if JSON_INTEGER_IS_LONG_LONG
155 GNUNET_assert (num <= LLONG_MAX);
156 GNUNET_assert (num >= LLONG_MIN);
157#else
158 GNUNET_assert (num <= LONG_MAX);
159 GNUNET_assert (num >= LONG_MIN);
160#endif
161 return ps;
162}
163
164
167 json_t *o)
168{
169 struct GNUNET_JSON_PackSpec ps = {
170 .field_name = name,
171 .object = o
172 };
173
174 if (NULL == o)
175 return ps;
176 if (! json_is_object (o))
177 {
179 "Expected JSON object for field `%s'\n",
180 name);
181 GNUNET_assert (0);
182 }
183 return ps;
184}
185
186
189 json_t *o)
190{
191 struct GNUNET_JSON_PackSpec ps = {
192 .field_name = name,
193 .object = o
194 };
195
196 if (NULL == o)
197 return ps;
198 (void) json_incref (o);
199 if (! json_is_object (o))
200 {
202 "Expected JSON object for field `%s'\n",
203 name);
204 GNUNET_assert (0);
205 }
206 return ps;
207}
208
209
212 json_t *a)
213{
214 struct GNUNET_JSON_PackSpec ps = {
215 .field_name = name,
216 .object = a
217 };
218
219 if (NULL == a)
220 return ps;
221 if (! json_is_array (a))
222 {
224 "Expected JSON array for field `%s'\n",
225 name);
226 GNUNET_assert (0);
227 }
228 return ps;
229}
230
231
234 json_t *a)
235{
236 struct GNUNET_JSON_PackSpec ps = {
237 .field_name = name,
238 .object = a
239 };
240
241 if (NULL == a)
242 return ps;
243 (void) json_incref (a);
244 if (! json_is_array (a))
245 {
247 "Expected JSON array for field `%s'\n",
248 name);
249 GNUNET_assert (0);
250 }
251 return ps;
252}
253
254
257 const void *blob,
258 size_t blob_size)
259{
260 struct GNUNET_JSON_PackSpec ps = {
261 .field_name = name,
262 .object = (NULL != blob)
263 ? GNUNET_JSON_from_data (blob,
264 blob_size)
265 : NULL
266 };
267
268 return ps;
269}
270
271
274 const void *blob,
275 size_t blob_size)
276{
277 struct GNUNET_JSON_PackSpec ps = {
278 .field_name = name,
279 .object = (NULL != blob)
281 blob_size)
282 : NULL
283 };
284
285 return ps;
286}
287
288
292{
293 struct GNUNET_JSON_PackSpec ps = {
294 .field_name = name
295 };
296
297 if (! GNUNET_TIME_absolute_is_zero (t.abs_time))
298 {
300 GNUNET_assert (NULL != ps.object);
301 }
302 else
303 {
304 ps.object = NULL;
305 }
306 return ps;
307}
308
309
312 struct GNUNET_TIME_TimestampNBO at)
313{
316}
317
318
321 struct GNUNET_TIME_Relative rt)
322{
323 json_t *json;
324
325 json = GNUNET_JSON_from_time_rel (rt);
326 GNUNET_assert (NULL != json);
328 json);
329}
330
331
334 struct GNUNET_TIME_RelativeNBO rt)
335{
338}
339
340
343 const struct GNUNET_CRYPTO_RsaPublicKey *pk)
344{
345 struct GNUNET_JSON_PackSpec ps = {
346 .field_name = name,
348 };
349
350 return ps;
351}
352
353
356 const struct GNUNET_CRYPTO_RsaSignature *sig)
357{
358 struct GNUNET_JSON_PackSpec ps = {
359 .field_name = name,
360 .object = GNUNET_JSON_from_rsa_signature (sig)
361 };
362
363 return ps;
364}
365
366
367/* end of json_pack.c */
static int ret
Final status code.
Definition: gnunet-arm.c:94
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
struct GNUNET_CRYPTO_PrivateKey pk
Private key from command line option, or NULL.
static char * name
Name (label) of the records to list.
static struct GNUNET_SCHEDULER_Task * t
Main task.
functions to parse JSON objects into GNUnet objects
json_t * GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Convert RSA public key to JSON.
json_t * GNUNET_JSON_from_timestamp(struct GNUNET_TIME_Timestamp stamp)
Convert timestamp to a json string.
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.
json_t * GNUNET_JSON_from_data64(const void *data, size_t size)
Convert binary data to a JSON string with base64 encoding.
json_t * GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
Convert RSA signature to JSON.
json_t * GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
Convert relative timestamp to a json string.
#define GNUNET_log(kind,...)
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a)
Convert relative time from network byte order.
Definition: time.c:628
bool GNUNET_TIME_absolute_is_zero(struct GNUNET_TIME_Absolute abs)
Test if abs is truly zero.
Definition: time.c:844
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_ntoh(struct GNUNET_TIME_TimestampNBO tn)
Convert timestamp from network byte order.
Definition: time.c:101
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel(const char *name, struct GNUNET_TIME_Relative rt)
Generate packer instruction for a JSON field of type relative time.
Definition: json_pack.c:320
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_(void)
Do not use directly.
Definition: json_pack.c:70
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_array_steal(const char *name, json_t *a)
Generate packer instruction for a JSON field of type JSON array where the reference is taken over by ...
Definition: json_pack.c:211
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_data_varsize(const char *name, const void *blob, size_t blob_size)
Generate packer instruction for a JSON field of type variable size binary blob.
Definition: json_pack.c:256
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_bool(const char *name, bool b)
Generate packer instruction for a JSON field of type bool.
Definition: json_pack.c:89
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp(const char *name, struct GNUNET_TIME_Timestamp t)
Generate packer instruction for a JSON field of type timestamp.
Definition: json_pack.c:290
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_steal(const char *name, json_t *o)
Generate packer instruction for a JSON field of type JSON object where the reference is taken over by...
Definition: json_pack.c:166
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_array_incref(const char *name, json_t *a)
Generate packer instruction for a JSON field of type JSON array where the reference counter is increm...
Definition: json_pack.c:233
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_allow_null(struct GNUNET_JSON_PackSpec in)
Modify packer instruction to allow NULL as a value.
Definition: json_pack.c:81
json_t * GNUNET_JSON_pack_(struct GNUNET_JSON_PackSpec spec[])
Pack a JSON object from a spec.
Definition: json_pack.c:30
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_double(const char *name, double f)
Generate packer instruction for a JSON field of type double.
Definition: json_pack.c:102
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp_nbo(const char *name, struct GNUNET_TIME_TimestampNBO at)
Generate packer instruction for a JSON field of type timestamp in network byte order.
Definition: json_pack.c:311
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_uint64(const char *name, uint64_t num)
Generate packer instruction for a JSON field of type unsigned integer.
Definition: json_pack.c:128
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_incref(const char *name, json_t *o)
Generate packer instruction for a JSON field of type JSON object where the reference counter is incre...
Definition: json_pack.c:188
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_rsa_signature(const char *name, const struct GNUNET_CRYPTO_RsaSignature *sig)
Generate packer instruction for a JSON field of type RSA signature.
Definition: json_pack.c:355
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_data64_varsize(const char *name, const void *blob, size_t blob_size)
Generate packer instruction for a JSON field of type variable size binary blob.
Definition: json_pack.c:273
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_rsa_public_key(const char *name, const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Generate packer instruction for a JSON field of type RSA public key.
Definition: json_pack.c:342
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_int64(const char *name, int64_t num)
Generate packer instruction for a JSON field of type signed integer.
Definition: json_pack.c:146
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel_nbo(const char *name, struct GNUNET_TIME_RelativeNBO rt)
Generate packer instruction for a JSON field of type relative time in network byte order.
Definition: json_pack.c:333
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_string(const char *name, const char *s)
Generate packer instruction for a JSON field of type string.
Definition: json_pack.c:115
The public information of an RSA key pair.
Definition: crypto_rsa.c:53
an RSA signature
Definition: crypto_rsa.c:65
Element in the array to give to the packer.
bool allow_null
True if a NULL (or 0) argument is allowed.
json_t * object
Object to pack.
const char * field_name
Name of the field to pack.
Time for relative time used by GNUnet, in microseconds and in network byte order.
Time for relative time used by GNUnet, in microseconds.
Time for timestamps used by GNUnet, in seconds and in network byte order.
Rounded time for timestamps used by GNUnet, in seconds.