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_timestamp (const char *name, struct GNUNET_TIME_Timestamp t)
 Generate packer instruction for a JSON field of type timestamp. More...
 
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. 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  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 }
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.

65 {
66  struct GNUNET_JSON_PackSpec ps = {
67  .field_name = NULL
68  };
69 
70  return ps;
71 }
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.

76 {
77  in.allow_null = true;
78  return in;
79 }
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.

85 {
86  struct GNUNET_JSON_PackSpec ps = {
87  .field_name = name,
88  .object = json_boolean (b)
89  };
90 
91  return ps;
92 }
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.

98 {
99  struct GNUNET_JSON_PackSpec ps = {
100  .field_name = name,
101  .object = json_string (s)
102  };
103 
104  return ps;
105 }

◆ 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.

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 }

◆ 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.

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 }

◆ 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.

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 }
#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.

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 }

◆ 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.

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 }

◆ 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.

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 }

◆ 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.

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 }
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_timestamp()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp ( const char *  name,
struct GNUNET_TIME_Timestamp  at 
)

Generate packer instruction for a JSON field of type timestamp.

Parameters
namename of the field to add to the object
attimestamp 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.

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 }
static struct GNUNET_SCHEDULER_Task * t
Main task.
json_t * GNUNET_JSON_from_timestamp(struct GNUNET_TIME_Timestamp stamp)
Convert timestamp to a json string.
bool GNUNET_TIME_absolute_is_zero(struct GNUNET_TIME_Absolute abs)
Test if abs is truly zero.
Definition: time.c:843

◆ GNUNET_JSON_pack_timestamp_nbo()

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.

Parameters
namename of the field to add to the object
attimestamp 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.

277 {
280 }
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_timestamp(const char *name, struct GNUNET_TIME_Timestamp t)
Generate packer instruction for a JSON field of type timestamp.
Definition: json_pack.c:254

◆ 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.

286 {
287  json_t *json;
288 
289  json = GNUNET_JSON_from_time_rel (rt);
290  GNUNET_assert (NULL != json);
292  json);
293 }
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:147

◆ 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.

299 {
302 }
struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a)
Convert relative time from network byte order.
Definition: time.c:627
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

◆ 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.

308 {
309  struct GNUNET_JSON_PackSpec ps = {
310  .field_name = name,
312  };
313 
314  return ps;
315 }
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.

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