diff options
author | Christian Grothoff <grothoff@gnunet.org> | 2023-10-13 23:51:45 +0200 |
---|---|---|
committer | Christian Grothoff <grothoff@gnunet.org> | 2023-10-13 23:52:20 +0200 |
commit | e1a3225cda96b8fd99d437ba3c53a9badd341693 (patch) | |
tree | 4e5882811caf4c05da9e74ba46ad96d1d049497e /src | |
parent | 5db905d02e078e7b85992bd5ddbdadfae9ca5c08 (diff) | |
download | gnunet-e1a3225cda96b8fd99d437ba3c53a9badd341693.tar.gz gnunet-e1a3225cda96b8fd99d437ba3c53a9badd341693.zip |
NEWS: add support for encoding/decoding double values as part of JSON to libgnunetjson
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_json_lib.h | 24 | ||||
-rw-r--r-- | src/json/json_helper.c | 43 | ||||
-rw-r--r-- | src/json/json_pack.c | 13 |
3 files changed, 80 insertions, 0 deletions
diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h index 26c68fed3..c3204026b 100644 --- a/src/include/gnunet_json_lib.h +++ b/src/include/gnunet_json_lib.h | |||
@@ -291,6 +291,17 @@ GNUNET_JSON_spec_bool (const char *name, | |||
291 | 291 | ||
292 | 292 | ||
293 | /** | 293 | /** |
294 | * double. | ||
295 | * | ||
296 | * @param name name of the JSON field | ||
297 | * @param[out] f where to store the double found under @a name | ||
298 | */ | ||
299 | struct GNUNET_JSON_Specification | ||
300 | GNUNET_JSON_spec_double (const char *name, | ||
301 | double *f); | ||
302 | |||
303 | |||
304 | /** | ||
294 | * 8-bit integer. | 305 | * 8-bit integer. |
295 | * | 306 | * |
296 | * @param name name of the JSON field | 307 | * @param name name of the JSON field |
@@ -704,6 +715,19 @@ GNUNET_JSON_pack_bool (const char *name, | |||
704 | 715 | ||
705 | /** | 716 | /** |
706 | * Generate packer instruction for a JSON field of type | 717 | * Generate packer instruction for a JSON field of type |
718 | * double. | ||
719 | * | ||
720 | * @param name name of the field to add to the object | ||
721 | * @param f double value | ||
722 | * @return json pack specification | ||
723 | */ | ||
724 | struct GNUNET_JSON_PackSpec | ||
725 | GNUNET_JSON_pack_double (const char *name, | ||
726 | double f); | ||
727 | |||
728 | |||
729 | /** | ||
730 | * Generate packer instruction for a JSON field of type | ||
707 | * string. | 731 | * string. |
708 | * | 732 | * |
709 | * @param name name of the field to add to the object | 733 | * @param name name of the field to add to the object |
diff --git a/src/json/json_helper.c b/src/json/json_helper.c index 515cc4c7f..4795285f4 100644 --- a/src/json/json_helper.c +++ b/src/json/json_helper.c | |||
@@ -490,6 +490,49 @@ GNUNET_JSON_spec_bool (const char *name, | |||
490 | 490 | ||
491 | 491 | ||
492 | /** | 492 | /** |
493 | * Parse given JSON object to a double. | ||
494 | * | ||
495 | * @param cls closure, NULL | ||
496 | * @param root the json object representing data | ||
497 | * @param[out] spec where to write the data | ||
498 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | ||
499 | */ | ||
500 | static enum GNUNET_GenericReturnValue | ||
501 | parse_double (void *cls, | ||
502 | json_t *root, | ||
503 | struct GNUNET_JSON_Specification *spec) | ||
504 | { | ||
505 | double *f = spec->ptr; | ||
506 | |||
507 | if (! json_is_real (root)) | ||
508 | { | ||
509 | GNUNET_break_op (0); | ||
510 | return GNUNET_SYSERR; | ||
511 | } | ||
512 | *f = json_real_value (root); | ||
513 | return GNUNET_OK; | ||
514 | } | ||
515 | |||
516 | |||
517 | struct GNUNET_JSON_Specification | ||
518 | GNUNET_JSON_spec_double (const char *name, | ||
519 | double *f) | ||
520 | { | ||
521 | struct GNUNET_JSON_Specification ret = { | ||
522 | .parser = &parse_double, | ||
523 | .cleaner = NULL, | ||
524 | .cls = NULL, | ||
525 | .field = name, | ||
526 | .ptr = f, | ||
527 | .ptr_size = sizeof(double), | ||
528 | .size_ptr = NULL | ||
529 | }; | ||
530 | |||
531 | return ret; | ||
532 | } | ||
533 | |||
534 | |||
535 | /** | ||
493 | * Parse given JSON object to a uint8_t. | 536 | * Parse given JSON object to a uint8_t. |
494 | * | 537 | * |
495 | * @param cls closure, NULL | 538 | * @param cls closure, NULL |
diff --git a/src/json/json_pack.c b/src/json/json_pack.c index 816373eaf..18487c3f4 100644 --- a/src/json/json_pack.c +++ b/src/json/json_pack.c | |||
@@ -99,6 +99,19 @@ GNUNET_JSON_pack_bool (const char *name, | |||
99 | 99 | ||
100 | 100 | ||
101 | struct GNUNET_JSON_PackSpec | 101 | struct GNUNET_JSON_PackSpec |
102 | GNUNET_JSON_pack_double (const char *name, | ||
103 | double f) | ||
104 | { | ||
105 | struct GNUNET_JSON_PackSpec ps = { | ||
106 | .field_name = name, | ||
107 | .object = json_real (f) | ||
108 | }; | ||
109 | |||
110 | return ps; | ||
111 | } | ||
112 | |||
113 | |||
114 | struct GNUNET_JSON_PackSpec | ||
102 | GNUNET_JSON_pack_string (const char *name, | 115 | GNUNET_JSON_pack_string (const char *name, |
103 | const char *s) | 116 | const char *s) |
104 | { | 117 | { |