GNUnet  0.11.x
Functions
json_pack.c File Reference

functions to pack JSON objects More...

#include "platform.h"
#include "gnunet_json_lib.h"
Include dependency graph for json_pack.c:

Go to the source code of this file.

Functions

json_t * GNUNET_JSON_pack_ (struct GNUNET_JSON_PackSpec spec[])
 Pack a JSON object from a spec. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_ (void)
 Do not use directly. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_allow_null (struct GNUNET_JSON_PackSpec in)
 Modify packer instruction to allow NULL as a value. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_bool (const char *name, bool b)
 Generate packer instruction for a JSON field of type bool. More...
 
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_string (const char *name, const char *s)
 Generate packer instruction for a JSON field of type string. More...
 
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. More...
 
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. More...
 
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 the packer. More...
 
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 incremented by the packer. More...
 
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 the packer. More...
 
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 incremented by the packer. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 

Detailed Description

functions to pack JSON objects

Author
Christian Grothoff

Definition in file json_pack.c.

Function Documentation

◆ GNUNET_JSON_pack_()

json_t* GNUNET_JSON_pack_ ( struct GNUNET_JSON_PackSpec  spec[])

Pack a JSON object from a spec.

Aborts if packing fails.

Parameters
specspecification object
Returns
JSON object

Definition at line 30 of file json_pack.c.

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 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
json_t * object
Object to pack.
const char * field_name
Name of the field to pack.

References GNUNET_JSON_PackSpec::field_name, GNUNET_assert, GNUNET_JSON_PackSpec::object, and ret.

◆ GNUNET_JSON_pack_end_()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_ ( void  )

Do not use directly.

Use GNUNET_JSON_PACK.

Returns
array terminator

Definition at line 30 of file json_pack.c.

59 {
60  struct GNUNET_JSON_PackSpec ps = {
61  .field_name = NULL
62  };
63 
64  return ps;
65 }
Element in the array to give to the packer.

◆ GNUNET_JSON_pack_allow_null()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_allow_null ( struct GNUNET_JSON_PackSpec  in)

Modify packer instruction to allow NULL as a value.

Parameters
injson pack specification to modify
Returns
json pack specification

Definition at line 30 of file json_pack.c.

70 {
71  in.allow_null = true;
72  return in;
73 }
bool allow_null
True if a NULL (or 0) argument is allowed.

◆ GNUNET_JSON_pack_bool()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_bool ( const char *  name,
bool  b 
)

Generate packer instruction for a JSON field of type bool.

Parameters
namename of the field to add to the object
bboolean value
Returns
json pack specification

Definition at line 30 of file json_pack.c.

79 {
80  struct GNUNET_JSON_PackSpec ps = {
81  .field_name = name,
82  .object = json_boolean (b)
83  };
84 
85  return ps;
86 }
const char * name

◆ GNUNET_JSON_pack_string()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_string ( const char *  name,
const char *  s 
)

Generate packer instruction for a JSON field of type string.

Parameters
namename of the field to add to the object
sstring value
Returns
json pack specification

Definition at line 30 of file json_pack.c.

92 {
93  struct GNUNET_JSON_PackSpec ps = {
94  .field_name = name,
95  .object = json_string (s)
96  };
97 
98  return ps;
99 }

◆ GNUNET_JSON_pack_uint64()

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.

Note that the maximum allowed value is still limited by JSON and not UINT64_MAX.

Parameters
namename of the field to add to the object
numnumeric value
Returns
json pack specification

Definition at line 30 of file json_pack.c.

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 }

◆ GNUNET_JSON_pack_int64()

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.

Parameters
namename of the field to add to the object
numnumeric value
Returns
json pack specification

Definition at line 30 of file json_pack.c.

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 }

◆ GNUNET_JSON_pack_object_steal()

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 the packer.

Parameters
namename of the field to add to the object
oobject to steal
Returns
json pack specification

Definition at line 30 of file json_pack.c.

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 }
#define GNUNET_log(kind,...)
@ GNUNET_ERROR_TYPE_ERROR

◆ GNUNET_JSON_pack_object_incref()

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 incremented by the packer.

Note that a deep copy is not performed.

Parameters
namename of the field to add to the object
oobject to increment reference counter of
Returns
json pack specification

Definition at line 30 of file json_pack.c.

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 }

◆ GNUNET_JSON_pack_array_steal()

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 the packer.

Parameters
namename of the field to add to the object
aarray to steal
Returns
json pack specification

Definition at line 30 of file json_pack.c.

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 }

◆ GNUNET_JSON_pack_array_incref()

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 incremented by the packer.

Note that a deep copy is not performed.

Parameters
namename of the field to add to the object
aarray to increment reference counter of
Returns
json pack specification

Definition at line 30 of file json_pack.c.

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 }

◆ GNUNET_JSON_pack_data_varsize()

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.

Parameters
namename of the field to add to the object
blobbinary data to pack
blob_sizenumber of bytes in blob
Returns
json pack specification

Definition at line 30 of file json_pack.c.

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 }
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.

◆ GNUNET_JSON_pack_time_abs()

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.

Parameters
namename of the field to add to the object
atabsolute time to pack, a value of 0 is only allowed with GNUNET_JSON_pack_allow_null()!
Returns
json pack specification

Definition at line 30 of file json_pack.c.

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 }
json_t * GNUNET_JSON_from_time_abs(struct GNUNET_TIME_Absolute stamp)
Convert absolute timestamp to a json string.
uint64_t abs_value_us
The actual value.

◆ GNUNET_JSON_pack_time_abs_nbo()

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.

Parameters
namename of the field to add to the object
atabsolute time to pack, a value of 0 is only allowed with GNUNET_JSON_pack_allow_null()!
Returns
json pack specification

Definition at line 30 of file json_pack.c.

271 {
274 }
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_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

◆ GNUNET_JSON_pack_time_rel()

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.

Parameters
namename of the field to add to the object
rtrelative time to pack
Returns
json pack specification

Definition at line 30 of file json_pack.c.

280 {
281  json_t *json;
282 
283  json = GNUNET_JSON_from_time_rel (rt);
284  GNUNET_assert (NULL != json);
286  json);
287 }
json_t * GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
Convert relative timestamp to a json string.
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

◆ GNUNET_JSON_pack_time_rel_nbo()

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.

Parameters
namename of the field to add to the object
rtrelative time to pack
Returns
json pack specification

Definition at line 30 of file json_pack.c.

293 {
296 }
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_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

◆ GNUNET_JSON_pack_rsa_public_key()

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.

Parameters
namename of the field to add to the object
pkRSA public key
Returns
json pack specification

Definition at line 30 of file json_pack.c.

302 {
303  struct GNUNET_JSON_PackSpec ps = {
304  .field_name = name,
306  };
307 
308  return ps;
309 }
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
json_t * GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Convert RSA public key to JSON.

◆ GNUNET_JSON_pack_rsa_signature()

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.

Parameters
namename of the field to add to the object
sigRSA signature
Returns
json pack specification

Definition at line 30 of file json_pack.c.

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 }
json_t * GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
Convert RSA signature to JSON.