GNUnet  0.11.x
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 
29 json_t *
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 
59 {
60  struct GNUNET_JSON_PackSpec ps = {
61  .field_name = NULL
62  };
63 
64  return ps;
65 }
66 
67 
70 {
71  in.allow_null = true;
72  return in;
73 }
74 
75 
77 GNUNET_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 
90 GNUNET_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 
103 GNUNET_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 
121 GNUNET_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 
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  {
154  "Expected JSON object for field `%s'\n",
155  name);
156  GNUNET_assert (0);
157  }
158  return ps;
159 }
160 
161 
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  {
177  "Expected JSON object for field `%s'\n",
178  name);
179  GNUNET_assert (0);
180  }
181  return ps;
182 }
183 
184 
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  {
199  "Expected JSON array for field `%s'\n",
200  name);
201  GNUNET_assert (0);
202  }
203  return ps;
204 }
205 
206 
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  {
222  "Expected JSON array for field `%s'\n",
223  name);
224  GNUNET_assert (0);
225  }
226  return ps;
227 }
228 
229 
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 
248 GNUNET_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  {
258  GNUNET_assert (NULL != ps.object);
259  }
260  else
261  {
262  ps.object = NULL;
263  }
264  return ps;
265 }
266 
267 
270  struct GNUNET_TIME_AbsoluteNBO at)
271 {
274 }
275 
276 
278 GNUNET_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);
286  json);
287 }
288 
289 
292  struct GNUNET_TIME_RelativeNBO rt)
293 {
296 }
297 
298 
301  const struct GNUNET_CRYPTO_RsaPublicKey *pk)
302 {
303  struct GNUNET_JSON_PackSpec ps = {
304  .field_name = name,
306  };
307 
308  return ps;
309 }
310 
311 
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 */
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
#define GNUNET_log(kind,...)
functions to parse JSON objects into GNUnet objects
json_t * GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
Convert relative timestamp to a json string.
json_t * GNUNET_JSON_from_time_abs(struct GNUNET_TIME_Absolute stamp)
Convert absolute timestamp to a json string.
json_t * GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
Convert RSA signature to JSON.
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_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.
#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:454
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:542
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:278
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_(void)
Do not use directly.
Definition: json_pack.c:58
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:186
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:231
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:77
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:141
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:208
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:69
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_abs(const char *name, struct GNUNET_TIME_Absolute at)
Generate packer instruction for a JSON field of type absolute time.
Definition: json_pack.c:248
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:103
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_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:163
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:313
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:300
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:121
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:291
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_abs_nbo(const char *name, struct GNUNET_TIME_AbsoluteNBO at)
Generate packer instruction for a JSON field of type absolute time in network byte order.
Definition: json_pack.c:269
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:90
const char * name
The public information of an RSA key pair.
Definition: crypto_rsa.c:52
an RSA signature
Definition: crypto_rsa.c:64
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 absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds and in network byte order.
Time for relative time used by GNUnet, in microseconds.