aboutsummaryrefslogtreecommitdiff
path: root/src/json
diff options
context:
space:
mode:
authorChristian Grothoff <grothoff@gnunet.org>2023-10-13 23:51:45 +0200
committerChristian Grothoff <grothoff@gnunet.org>2023-10-13 23:52:20 +0200
commite1a3225cda96b8fd99d437ba3c53a9badd341693 (patch)
tree4e5882811caf4c05da9e74ba46ad96d1d049497e /src/json
parent5db905d02e078e7b85992bd5ddbdadfae9ca5c08 (diff)
downloadgnunet-e1a3225cda96b8fd99d437ba3c53a9badd341693.tar.gz
gnunet-e1a3225cda96b8fd99d437ba3c53a9badd341693.zip
NEWS: add support for encoding/decoding double values as part of JSON to libgnunetjson
Diffstat (limited to 'src/json')
-rw-r--r--src/json/json_helper.c43
-rw-r--r--src/json/json_pack.c13
2 files changed, 56 insertions, 0 deletions
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 */
500static enum GNUNET_GenericReturnValue
501parse_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
517struct GNUNET_JSON_Specification
518GNUNET_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
101struct GNUNET_JSON_PackSpec 101struct GNUNET_JSON_PackSpec
102GNUNET_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
114struct GNUNET_JSON_PackSpec
102GNUNET_JSON_pack_string (const char *name, 115GNUNET_JSON_pack_string (const char *name,
103 const char *s) 116 const char *s)
104{ 117{