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  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  GNUNET_assert (spec[i].allow_null);
49  }
50  else
51  {
52  GNUNET_assert (0 ==
53  json_object_set_new (ret,
54  spec[i].field_name,
55  spec[i].object));
56  spec[i].object = NULL;
57  }
58  }
59  return ret;
60 }
61 
62 
65 {
66  struct GNUNET_JSON_PackSpec ps = {
67  .field_name = NULL
68  };
69 
70  return ps;
71 }
72 
73 
76 {
77  in.allow_null = true;
78  return in;
79 }
80 
81 
83 GNUNET_JSON_pack_bool (const char *name,
84  bool b)
85 {
86  struct GNUNET_JSON_PackSpec ps = {
87  .field_name = name,
88  .object = json_boolean (b)
89  };
90 
91  return ps;
92 }
93 
94 
96 GNUNET_JSON_pack_string (const char *name,
97  const char *s)
98 {
99  struct GNUNET_JSON_PackSpec ps = {
100  .field_name = name,
101  .object = json_string (s)
102  };
103 
104  return ps;
105 }
106 
107 
109 GNUNET_JSON_pack_uint64 (const char *name,
110  uint64_t num)
111 {
112  struct GNUNET_JSON_PackSpec ps = {
113  .field_name = name,
114  .object = json_integer ((json_int_t) num)
115  };
116 
117 #if JSON_INTEGER_IS_LONG_LONG
118  GNUNET_assert (num <= LLONG_MAX);
119 #else
120  GNUNET_assert (num <= LONG_MAX);
121 #endif
122  return ps;
123 }
124 
125 
127 GNUNET_JSON_pack_int64 (const char *name,
128  int64_t num)
129 {
130  struct GNUNET_JSON_PackSpec ps = {
131  .field_name = name,
132  .object = json_integer ((json_int_t) num)
133  };
134 
135 #if JSON_INTEGER_IS_LONG_LONG
136  GNUNET_assert (num <= LLONG_MAX);
137  GNUNET_assert (num >= LLONG_MIN);
138 #else
139  GNUNET_assert (num <= LONG_MAX);
140  GNUNET_assert (num >= LONG_MIN);
141 #endif
142  return ps;
143 }
144 
145 
148  json_t *o)
149 {
150  struct GNUNET_JSON_PackSpec ps = {
151  .field_name = name,
152  .object = o
153  };
154 
155  if (NULL == o)
156  return ps;
157  if (! json_is_object (o))
158  {
160  "Expected JSON object for field `%s'\n",
161  name);
162  GNUNET_assert (0);
163  }
164  return ps;
165 }
166 
167 
170  json_t *o)
171 {
172  struct GNUNET_JSON_PackSpec ps = {
173  .field_name = name,
174  .object = o
175  };
176 
177  if (NULL == o)
178  return ps;
179  (void) json_incref (o);
180  if (! json_is_object (o))
181  {
183  "Expected JSON object for field `%s'\n",
184  name);
185  GNUNET_assert (0);
186  }
187  return ps;
188 }
189 
190 
193  json_t *a)
194 {
195  struct GNUNET_JSON_PackSpec ps = {
196  .field_name = name,
197  .object = a
198  };
199 
200  if (NULL == a)
201  return ps;
202  if (! json_is_array (a))
203  {
205  "Expected JSON array for field `%s'\n",
206  name);
207  GNUNET_assert (0);
208  }
209  return ps;
210 }
211 
212 
215  json_t *a)
216 {
217  struct GNUNET_JSON_PackSpec ps = {
218  .field_name = name,
219  .object = a
220  };
221 
222  if (NULL == a)
223  return ps;
224  (void) json_incref (a);
225  if (! json_is_array (a))
226  {
228  "Expected JSON array for field `%s'\n",
229  name);
230  GNUNET_assert (0);
231  }
232  return ps;
233 }
234 
235 
238  const void *blob,
239  size_t blob_size)
240 {
241  struct GNUNET_JSON_PackSpec ps = {
242  .field_name = name,
243  .object = (NULL != blob)
244  ? GNUNET_JSON_from_data (blob,
245  blob_size)
246  : NULL
247  };
248 
249  return ps;
250 }
251 
252 
254 GNUNET_JSON_pack_timestamp (const char *name,
255  struct GNUNET_TIME_Timestamp t)
256 {
257  struct GNUNET_JSON_PackSpec ps = {
258  .field_name = name
259  };
260 
261  if (! GNUNET_TIME_absolute_is_zero (t.abs_time))
262  {
264  GNUNET_assert (NULL != ps.object);
265  }
266  else
267  {
268  ps.object = NULL;
269  }
270  return ps;
271 }
272 
273 
276  struct GNUNET_TIME_TimestampNBO at)
277 {
280 }
281 
282 
284 GNUNET_JSON_pack_time_rel (const char *name,
285  struct GNUNET_TIME_Relative rt)
286 {
287  json_t *json;
288 
289  json = GNUNET_JSON_from_time_rel (rt);
290  GNUNET_assert (NULL != json);
292  json);
293 }
294 
295 
298  struct GNUNET_TIME_RelativeNBO rt)
299 {
302 }
303 
304 
307  const struct GNUNET_CRYPTO_RsaPublicKey *pk)
308 {
309  struct GNUNET_JSON_PackSpec ps = {
310  .field_name = name,
312  };
313 
314  return ps;
315 }
316 
317 
320  const struct GNUNET_CRYPTO_RsaSignature *sig)
321 {
322  struct GNUNET_JSON_PackSpec ps = {
323  .field_name = name,
324  .object = GNUNET_JSON_from_rsa_signature (sig)
325  };
326 
327  return ps;
328 }
329 
330 
331 /* 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.
static struct GNUNET_SCHEDULER_Task * t
Main task.
#define GNUNET_log(kind,...)
functions to parse JSON objects into GNUnet objects
json_t * GNUNET_JSON_from_timestamp(struct GNUNET_TIME_Timestamp stamp)
Convert timestamp to a json string.
json_t * GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
Convert relative 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:627
bool GNUNET_TIME_absolute_is_zero(struct GNUNET_TIME_Absolute abs)
Test if abs is truly zero.
Definition: time.c:843
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_ntoh(struct GNUNET_TIME_TimestampNBO tn)
Convert timestamp from network byte order.
Definition: time.c:100
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:284
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_(void)
Do not use directly.
Definition: json_pack.c:64
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:192
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:237
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:83
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:254
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:147
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:214
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:75
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:275
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:109
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:169
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:319
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:306
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:127
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:297
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:96
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 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.