GNUnet 0.21.1
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_double (const char *name, double f)
 Generate packer instruction for a JSON field of type double. 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_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. 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 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}
static int ret
Final status code.
Definition: gnunet-arm.c:94
#define GNUNET_log(kind,...)
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
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_ERROR_TYPE_ERROR, GNUNET_log, 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 70 of file json_pack.c.

71{
72 struct GNUNET_JSON_PackSpec ps = {
73 .field_name = NULL
74 };
75
76 return ps;
77}
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
Element in the array to give to the packer.

References ps.

◆ 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 81 of file json_pack.c.

82{
83 in.allow_null = true;
84 return in;
85}
bool allow_null
True if a NULL (or 0) argument is allowed.

References GNUNET_JSON_PackSpec::allow_null.

◆ 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 89 of file json_pack.c.

91{
92 struct GNUNET_JSON_PackSpec ps = {
93 .field_name = name,
94 .object = json_boolean (b)
95 };
96
97 return ps;
98}
static char * name
Name (label) of the records to list.

References name, and ps.

◆ GNUNET_JSON_pack_double()

struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_double ( const char *  name,
double  f 
)

Generate packer instruction for a JSON field of type double.

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

Definition at line 102 of file json_pack.c.

104{
105 struct GNUNET_JSON_PackSpec ps = {
106 .field_name = name,
107 .object = json_real (f)
108 };
109
110 return ps;
111}

References removetrailingwhitespace::f, name, and ps.

◆ 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 115 of file json_pack.c.

117{
118 struct GNUNET_JSON_PackSpec ps = {
119 .field_name = name,
120 .object = json_string (s)
121 };
122
123 return ps;
124}

References name, and ps.

◆ 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 128 of file json_pack.c.

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}

References GNUNET_assert, name, and ps.

◆ 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 146 of file json_pack.c.

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}

References GNUNET_assert, name, and ps.

◆ 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 166 of file json_pack.c.

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}

References GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, name, and ps.

Referenced by GNUNET_JSON_pack_time_rel().

Here is the caller graph for this function:

◆ 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 188 of file json_pack.c.

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}

References GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, name, and ps.

◆ 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 211 of file json_pack.c.

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}

References GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, name, and ps.

◆ 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 233 of file json_pack.c.

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}

References GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, name, and ps.

◆ 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 256 of file json_pack.c.

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

References GNUNET_JSON_from_data(), name, and ps.

Here is the call graph for this function:

◆ GNUNET_JSON_pack_data64_varsize()

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.

Use base64-encoding, instead of the more common Crockford base32-encoding.

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 273 of file json_pack.c.

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

References GNUNET_JSON_from_data64(), name, and ps.

Here is the call graph for this function:

◆ 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 290 of file json_pack.c.

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}
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:844

References GNUNET_assert, GNUNET_JSON_from_timestamp(), GNUNET_TIME_absolute_is_zero(), name, ps, and t.

Referenced by GNUNET_JSON_pack_timestamp_nbo().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 311 of file json_pack.c.

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

References GNUNET_JSON_pack_timestamp(), GNUNET_TIME_timestamp_ntoh(), and name.

Here is the call graph for this function:

◆ 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 320 of file json_pack.c.

322{
323 json_t *json;
324
325 json = GNUNET_JSON_from_time_rel (rt);
326 GNUNET_assert (NULL != json);
328 json);
329}
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:166

References GNUNET_assert, GNUNET_JSON_from_time_rel(), GNUNET_JSON_pack_object_steal(), and name.

Referenced by GNUNET_JSON_pack_time_rel_nbo().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 333 of file json_pack.c.

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

References GNUNET_JSON_pack_time_rel(), GNUNET_TIME_relative_ntoh(), and name.

Here is the call graph for this function:

◆ 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 342 of file json_pack.c.

344{
345 struct GNUNET_JSON_PackSpec ps = {
346 .field_name = name,
348 };
349
350 return ps;
351}
struct GNUNET_CRYPTO_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.

References GNUNET_JSON_from_rsa_public_key(), name, pk, and ps.

Here is the call graph for this function:

◆ 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 355 of file json_pack.c.

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

References GNUNET_JSON_from_rsa_signature(), name, and ps.

Here is the call graph for this function: