diff options
Diffstat (limited to 'src/include/gnunet_json_lib.h')
-rw-r--r-- | src/include/gnunet_json_lib.h | 143 |
1 files changed, 138 insertions, 5 deletions
diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h index 2002a0130..203976b5c 100644 --- a/src/include/gnunet_json_lib.h +++ b/src/include/gnunet_json_lib.h | |||
@@ -27,6 +27,7 @@ | |||
27 | #ifndef GNUNET_JSON_LIB_H | 27 | #ifndef GNUNET_JSON_LIB_H |
28 | #define GNUNET_JSON_LIB_H | 28 | #define GNUNET_JSON_LIB_H |
29 | 29 | ||
30 | |||
30 | #include "gnunet_util_lib.h" | 31 | #include "gnunet_util_lib.h" |
31 | #include <jansson.h> | 32 | #include <jansson.h> |
32 | #include <microhttpd.h> | 33 | #include <microhttpd.h> |
@@ -129,7 +130,7 @@ struct GNUNET_JSON_Specification | |||
129 | * @param root the JSON node to start the navigation at. | 130 | * @param root the JSON node to start the navigation at. |
130 | * @param spec parse specification array | 131 | * @param spec parse specification array |
131 | * @param[out] error_json_name which JSON field was problematic | 132 | * @param[out] error_json_name which JSON field was problematic |
132 | * @param[out] which index into @a spec did we encounter an error | 133 | * @param[out] error_line which index into @a spec did we encounter an error |
133 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 134 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
134 | */ | 135 | */ |
135 | enum GNUNET_GenericReturnValue | 136 | enum GNUNET_GenericReturnValue |
@@ -141,7 +142,13 @@ GNUNET_JSON_parse (const json_t *root, | |||
141 | 142 | ||
142 | /** | 143 | /** |
143 | * Frees all elements allocated during a #GNUNET_JSON_parse() | 144 | * Frees all elements allocated during a #GNUNET_JSON_parse() |
144 | * operation. | 145 | * operation. Convenience function to be called if cleaning |
146 | * up all heap-allocated data from a #GNUNET_JSON_parse() is | ||
147 | * desired. The function does not have to be called if no data | ||
148 | * was heap-allocated (e.g. only integers, strings and fixed-sized | ||
149 | * data was used), or if the application calls the respective | ||
150 | * code to free the heap (not always #GNUNET_free(), depends | ||
151 | * on the data type!) on the returned heap-allocated data itself. | ||
145 | * | 152 | * |
146 | * @param spec specification of the parse operation | 153 | * @param spec specification of the parse operation |
147 | */ | 154 | */ |
@@ -197,6 +204,29 @@ GNUNET_JSON_spec_fixed (const char *name, | |||
197 | 204 | ||
198 | 205 | ||
199 | /** | 206 | /** |
207 | * Variable size object (in network byte order, encoded using base64 encoding). | ||
208 | * | ||
209 | * @param name name of the JSON field | ||
210 | * @param[out] obj pointer where to write the data, must have @a size bytes | ||
211 | * @param size number of bytes expected in @a obj | ||
212 | */ | ||
213 | struct GNUNET_JSON_Specification | ||
214 | GNUNET_JSON_spec_fixed64 (const char *name, | ||
215 | void *obj, | ||
216 | size_t size); | ||
217 | |||
218 | |||
219 | /** | ||
220 | * Fixed size object (in network byte order, encoded using base64 encoding). | ||
221 | * | ||
222 | * @param name name of the JSON field | ||
223 | * @param obj pointer where to write the data (type of `*obj` will determine size) | ||
224 | */ | ||
225 | #define GNUNET_JSON_spec_fixed64_auto(name, obj) \ | ||
226 | GNUNET_JSON_spec_fixed (name, obj, sizeof(*obj)) | ||
227 | |||
228 | |||
229 | /** | ||
200 | * Variable size object (in network byte order, encoded using | 230 | * Variable size object (in network byte order, encoded using |
201 | * Crockford Base32hex encoding). | 231 | * Crockford Base32hex encoding). |
202 | * | 232 | * |
@@ -222,7 +252,8 @@ GNUNET_JSON_spec_string (const char *name, | |||
222 | 252 | ||
223 | 253 | ||
224 | /** | 254 | /** |
225 | * JSON object. | 255 | * JSON object or array. Reference counter is |
256 | * incremented. | ||
226 | * | 257 | * |
227 | * @param name name of the JSON field | 258 | * @param name name of the JSON field |
228 | * @param[out] jsonp where to store the JSON found under @a name | 259 | * @param[out] jsonp where to store the JSON found under @a name |
@@ -233,6 +264,28 @@ GNUNET_JSON_spec_json (const char *name, | |||
233 | 264 | ||
234 | 265 | ||
235 | /** | 266 | /** |
267 | * JSON object, reference counter not incremented. | ||
268 | * | ||
269 | * @param name name of the JSON field | ||
270 | * @param[out] jsonp where to store the JSON found under @a name | ||
271 | */ | ||
272 | struct GNUNET_JSON_Specification | ||
273 | GNUNET_JSON_spec_object_const (const char *name, | ||
274 | const json_t **jsonp); | ||
275 | |||
276 | |||
277 | /** | ||
278 | * JSON array, reference counter not incremented. | ||
279 | * | ||
280 | * @param name name of the JSON field | ||
281 | * @param[out] jsonp where to store the JSON found under @a name | ||
282 | */ | ||
283 | struct GNUNET_JSON_Specification | ||
284 | GNUNET_JSON_spec_array_const (const char *name, | ||
285 | const json_t **jsonp); | ||
286 | |||
287 | |||
288 | /** | ||
236 | * boolean. | 289 | * boolean. |
237 | * | 290 | * |
238 | * @param name name of the JSON field | 291 | * @param name name of the JSON field |
@@ -244,6 +297,17 @@ GNUNET_JSON_spec_bool (const char *name, | |||
244 | 297 | ||
245 | 298 | ||
246 | /** | 299 | /** |
300 | * double. | ||
301 | * | ||
302 | * @param name name of the JSON field | ||
303 | * @param[out] f where to store the double found under @a name | ||
304 | */ | ||
305 | struct GNUNET_JSON_Specification | ||
306 | GNUNET_JSON_spec_double (const char *name, | ||
307 | double *f); | ||
308 | |||
309 | |||
310 | /** | ||
247 | * 8-bit integer. | 311 | * 8-bit integer. |
248 | * | 312 | * |
249 | * @param name name of the JSON field | 313 | * @param name name of the JSON field |
@@ -315,7 +379,7 @@ GNUNET_JSON_spec_boolean (const char *name, | |||
315 | * Timestamp. | 379 | * Timestamp. |
316 | * | 380 | * |
317 | * @param name name of the JSON field | 381 | * @param name name of the JSON field |
318 | * @param[out] at where to store the absolute time found under @a name | 382 | * @param[out] t at where to store the absolute time found under @a name |
319 | */ | 383 | */ |
320 | struct GNUNET_JSON_Specification | 384 | struct GNUNET_JSON_Specification |
321 | GNUNET_JSON_spec_timestamp (const char *name, | 385 | GNUNET_JSON_spec_timestamp (const char *name, |
@@ -378,7 +442,21 @@ GNUNET_JSON_spec_rsa_signature (const char *name, | |||
378 | * @return json string that encodes @a data | 442 | * @return json string that encodes @a data |
379 | */ | 443 | */ |
380 | json_t * | 444 | json_t * |
381 | GNUNET_JSON_from_data (const void *data, size_t size); | 445 | GNUNET_JSON_from_data (const void *data, |
446 | size_t size); | ||
447 | |||
448 | |||
449 | /** | ||
450 | * Convert binary data to a JSON string with base64 | ||
451 | * encoding. | ||
452 | * | ||
453 | * @param data binary data | ||
454 | * @param size size of @a data in bytes | ||
455 | * @return json string that encodes @a data | ||
456 | */ | ||
457 | json_t * | ||
458 | GNUNET_JSON_from_data64 (const void *data, | ||
459 | size_t size); | ||
382 | 460 | ||
383 | 461 | ||
384 | /** | 462 | /** |
@@ -393,6 +471,17 @@ GNUNET_JSON_from_data (const void *data, size_t size); | |||
393 | 471 | ||
394 | 472 | ||
395 | /** | 473 | /** |
474 | * Convert binary data to a JSON string with base64 | ||
475 | * encoding. | ||
476 | * | ||
477 | * @param ptr binary data, sizeof (*ptr) must yield correct size | ||
478 | * @return json string that encodes @a data | ||
479 | */ | ||
480 | #define GNUNET_JSON_from_data64_auto(ptr) \ | ||
481 | GNUNET_JSON_from_data64 (ptr, sizeof(*ptr)) | ||
482 | |||
483 | |||
484 | /** | ||
396 | * Convert timestamp to a json string. | 485 | * Convert timestamp to a json string. |
397 | * | 486 | * |
398 | * @param stamp the time stamp | 487 | * @param stamp the time stamp |
@@ -632,6 +721,19 @@ GNUNET_JSON_pack_bool (const char *name, | |||
632 | 721 | ||
633 | /** | 722 | /** |
634 | * Generate packer instruction for a JSON field of type | 723 | * Generate packer instruction for a JSON field of type |
724 | * double. | ||
725 | * | ||
726 | * @param name name of the field to add to the object | ||
727 | * @param f double value | ||
728 | * @return json pack specification | ||
729 | */ | ||
730 | struct GNUNET_JSON_PackSpec | ||
731 | GNUNET_JSON_pack_double (const char *name, | ||
732 | double f); | ||
733 | |||
734 | |||
735 | /** | ||
736 | * Generate packer instruction for a JSON field of type | ||
635 | * string. | 737 | * string. |
636 | * | 738 | * |
637 | * @param name name of the field to add to the object | 739 | * @param name name of the field to add to the object |
@@ -755,6 +857,37 @@ GNUNET_JSON_pack_data_varsize (const char *name, | |||
755 | 857 | ||
756 | /** | 858 | /** |
757 | * Generate packer instruction for a JSON field of type | 859 | * Generate packer instruction for a JSON field of type |
860 | * variable size binary blob. | ||
861 | * Use base64-encoding, instead of the more common | ||
862 | * Crockford base32-encoding. | ||
863 | * | ||
864 | * @param name name of the field to add to the object | ||
865 | * @param blob binary data to pack | ||
866 | * @param blob_size number of bytes in @a blob | ||
867 | * @return json pack specification | ||
868 | */ | ||
869 | struct GNUNET_JSON_PackSpec | ||
870 | GNUNET_JSON_pack_data64_varsize (const char *name, | ||
871 | const void *blob, | ||
872 | size_t blob_size); | ||
873 | |||
874 | |||
875 | /** | ||
876 | * Generate packer instruction for a JSON field where the | ||
877 | * size is automatically determined from the argument. | ||
878 | * Use base64-encoding, instead of the more common | ||
879 | * Crockford base32-encoding. | ||
880 | * | ||
881 | * @param name name of the field to add to the object | ||
882 | * @param blob data to pack, must not be an array | ||
883 | * @return json pack specification | ||
884 | */ | ||
885 | #define GNUNET_JSON_pack_data64_auto(name,blob) \ | ||
886 | GNUNET_JSON_pack_data64_varsize (name, blob, sizeof (*blob)) | ||
887 | |||
888 | |||
889 | /** | ||
890 | * Generate packer instruction for a JSON field of type | ||
758 | * timestamp. | 891 | * timestamp. |
759 | * | 892 | * |
760 | * @param name name of the field to add to the object | 893 | * @param name name of the field to add to the object |