aboutsummaryrefslogtreecommitdiff
path: root/src/json/json_helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/json/json_helper.c')
-rw-r--r--src/json/json_helper.c96
1 files changed, 89 insertions, 7 deletions
diff --git a/src/json/json_helper.c b/src/json/json_helper.c
index aadc6804d..61257e62a 100644
--- a/src/json/json_helper.c
+++ b/src/json/json_helper.c
@@ -301,9 +301,9 @@ GNUNET_JSON_spec_string (const char *name,
301 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 301 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
302 */ 302 */
303static enum GNUNET_GenericReturnValue 303static enum GNUNET_GenericReturnValue
304parse_object (void *cls, 304parse_json (void *cls,
305 json_t *root, 305 json_t *root,
306 struct GNUNET_JSON_Specification *spec) 306 struct GNUNET_JSON_Specification *spec)
307{ 307{
308 if (! (json_is_object (root) || json_is_array (root))) 308 if (! (json_is_object (root) || json_is_array (root)))
309 { 309 {
@@ -323,8 +323,8 @@ parse_object (void *cls,
323 * @param[out] spec where to free the data 323 * @param[out] spec where to free the data
324 */ 324 */
325static void 325static void
326clean_object (void *cls, 326clean_json (void *cls,
327 struct GNUNET_JSON_Specification *spec) 327 struct GNUNET_JSON_Specification *spec)
328{ 328{
329 json_t **ptr = (json_t **) spec->ptr; 329 json_t **ptr = (json_t **) spec->ptr;
330 330
@@ -341,8 +341,90 @@ GNUNET_JSON_spec_json (const char *name,
341 json_t **jsonp) 341 json_t **jsonp)
342{ 342{
343 struct GNUNET_JSON_Specification ret = { 343 struct GNUNET_JSON_Specification ret = {
344 .parser = &parse_object, 344 .parser = &parse_json,
345 .cleaner = &clean_object, 345 .cleaner = &clean_json,
346 .cls = NULL,
347 .field = name,
348 .ptr = jsonp,
349 .ptr_size = 0,
350 .size_ptr = NULL
351 };
352
353 *jsonp = NULL;
354 return ret;
355}
356
357
358/**
359 * Parse given JSON object to a JSON object.
360 *
361 * @param cls closure, NULL
362 * @param root the json object representing data
363 * @param[out] spec where to write the data
364 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
365 */
366static enum GNUNET_GenericReturnValue
367parse_object_const (void *cls,
368 json_t *root,
369 struct GNUNET_JSON_Specification *spec)
370{
371 if (! json_is_object (root))
372 {
373 GNUNET_break_op (0);
374 return GNUNET_SYSERR;
375 }
376 *(const json_t **) spec->ptr = (const json_t *) root;
377 return GNUNET_OK;
378}
379
380
381struct GNUNET_JSON_Specification
382GNUNET_JSON_spec_object_const (const char *name,
383 const json_t **jsonp)
384{
385 struct GNUNET_JSON_Specification ret = {
386 .parser = &parse_object_const,
387 .cls = NULL,
388 .field = name,
389 .ptr = jsonp,
390 .ptr_size = 0,
391 .size_ptr = NULL
392 };
393
394 *jsonp = NULL;
395 return ret;
396}
397
398
399/**
400 * Parse given JSON to a JSON array.
401 *
402 * @param cls closure, NULL
403 * @param root the json object representing data
404 * @param[out] spec where to write the data
405 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
406 */
407static enum GNUNET_GenericReturnValue
408parse_array_const (void *cls,
409 json_t *root,
410 struct GNUNET_JSON_Specification *spec)
411{
412 if (! json_is_array (root))
413 {
414 GNUNET_break_op (0);
415 return GNUNET_SYSERR;
416 }
417 *(const json_t **) spec->ptr = (const json_t *) root;
418 return GNUNET_OK;
419}
420
421
422struct GNUNET_JSON_Specification
423GNUNET_JSON_spec_array_const (const char *name,
424 const json_t **jsonp)
425{
426 struct GNUNET_JSON_Specification ret = {
427 .parser = &parse_array_const,
346 .cls = NULL, 428 .cls = NULL,
347 .field = name, 429 .field = name,
348 .ptr = jsonp, 430 .ptr = jsonp,