diff options
Diffstat (limited to 'src/json')
-rw-r--r-- | src/json/json_generator.c | 122 | ||||
-rw-r--r-- | src/json/json_helper.c | 167 | ||||
-rw-r--r-- | src/json/json_pack.c | 16 | ||||
-rw-r--r-- | src/json/test_json.c | 69 |
4 files changed, 226 insertions, 148 deletions
diff --git a/src/json/json_generator.c b/src/json/json_generator.c index 0c513ca9d..5421b9527 100644 --- a/src/json/json_generator.c +++ b/src/json/json_generator.c | |||
@@ -27,14 +27,6 @@ | |||
27 | #include "gnunet_json_lib.h" | 27 | #include "gnunet_json_lib.h" |
28 | 28 | ||
29 | 29 | ||
30 | /** | ||
31 | * Convert binary data to a JSON string | ||
32 | * with the base32crockford encoding. | ||
33 | * | ||
34 | * @param data binary data | ||
35 | * @param size size of @a data in bytes | ||
36 | * @return json string that encodes @a data | ||
37 | */ | ||
38 | json_t * | 30 | json_t * |
39 | GNUNET_JSON_from_data (const void *data, | 31 | GNUNET_JSON_from_data (const void *data, |
40 | size_t size) | 32 | size_t size) |
@@ -57,30 +49,30 @@ GNUNET_JSON_from_data (const void *data, | |||
57 | } | 49 | } |
58 | 50 | ||
59 | 51 | ||
60 | /** | ||
61 | * Convert absolute timestamp to a json string. | ||
62 | * | ||
63 | * @param stamp the time stamp | ||
64 | * @return a json string with the timestamp in @a stamp | ||
65 | */ | ||
66 | json_t * | 52 | json_t * |
67 | GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp) | 53 | GNUNET_JSON_from_timestamp (struct GNUNET_TIME_Timestamp stamp) |
68 | { | 54 | { |
69 | json_t *j; | 55 | json_t *j; |
70 | 56 | ||
71 | GNUNET_assert (GNUNET_OK == | ||
72 | GNUNET_TIME_round_abs (&stamp)); | ||
73 | |||
74 | j = json_object (); | 57 | j = json_object (); |
75 | if (NULL == j) | 58 | if (NULL == j) |
76 | { | 59 | { |
77 | GNUNET_break (0); | 60 | GNUNET_break (0); |
78 | return NULL; | 61 | return NULL; |
79 | } | 62 | } |
80 | if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) | 63 | if (GNUNET_TIME_absolute_is_never (stamp.abs_time)) |
81 | { | 64 | { |
82 | if (0 != | 65 | if (0 != |
83 | json_object_set_new (j, | 66 | json_object_set_new (j, |
67 | "t_s", | ||
68 | json_string ("never"))) | ||
69 | { | ||
70 | GNUNET_break (0); | ||
71 | json_decref (j); | ||
72 | return NULL; | ||
73 | } | ||
74 | if (0 != | ||
75 | json_object_set_new (j, | ||
84 | "t_ms", | 76 | "t_ms", |
85 | json_string ("never"))) | 77 | json_string ("never"))) |
86 | { | 78 | { |
@@ -90,11 +82,29 @@ GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp) | |||
90 | } | 82 | } |
91 | return j; | 83 | return j; |
92 | } | 84 | } |
85 | GNUNET_assert ( | ||
86 | 0 == | ||
87 | (stamp.abs_time.abs_value_us | ||
88 | % GNUNET_TIME_UNIT_SECONDS.rel_value_us)); | ||
93 | if (0 != | 89 | if (0 != |
94 | json_object_set_new (j, | 90 | json_object_set_new ( |
95 | "t_ms", | 91 | j, |
96 | json_integer ((json_int_t) (stamp.abs_value_us | 92 | "t_s", |
97 | / 1000LL)))) | 93 | json_integer ( |
94 | (json_int_t) (stamp.abs_time.abs_value_us | ||
95 | / GNUNET_TIME_UNIT_SECONDS.rel_value_us)))) | ||
96 | { | ||
97 | GNUNET_break (0); | ||
98 | json_decref (j); | ||
99 | return NULL; | ||
100 | } | ||
101 | if (0 != | ||
102 | json_object_set_new ( | ||
103 | j, | ||
104 | "t_ms", | ||
105 | json_integer ( | ||
106 | (json_int_t) (stamp.abs_time.abs_value_us | ||
107 | / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us)))) | ||
98 | { | 108 | { |
99 | GNUNET_break (0); | 109 | GNUNET_break (0); |
100 | json_decref (j); | 110 | json_decref (j); |
@@ -104,43 +114,37 @@ GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp) | |||
104 | } | 114 | } |
105 | 115 | ||
106 | 116 | ||
107 | /** | ||
108 | * Convert absolute timestamp to a json string. | ||
109 | * | ||
110 | * @param stamp the time stamp | ||
111 | * @return a json string with the timestamp in @a stamp | ||
112 | */ | ||
113 | json_t * | 117 | json_t * |
114 | GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp) | 118 | GNUNET_JSON_from_timestamp_nbo (struct GNUNET_TIME_TimestampNBO stamp) |
115 | { | 119 | { |
116 | return GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (stamp)); | 120 | return GNUNET_JSON_from_timestamp (GNUNET_TIME_timestamp_ntoh (stamp)); |
117 | } | 121 | } |
118 | 122 | ||
119 | 123 | ||
120 | /** | ||
121 | * Convert relative timestamp to a json string. | ||
122 | * | ||
123 | * @param stamp the time stamp | ||
124 | * @return a json string with the timestamp in @a stamp | ||
125 | */ | ||
126 | json_t * | 124 | json_t * |
127 | GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp) | 125 | GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp) |
128 | { | 126 | { |
129 | json_t *j; | 127 | json_t *j; |
130 | 128 | ||
131 | GNUNET_assert (GNUNET_OK == | ||
132 | GNUNET_TIME_round_rel (&stamp)); | ||
133 | |||
134 | j = json_object (); | 129 | j = json_object (); |
135 | if (NULL == j) | 130 | if (NULL == j) |
136 | { | 131 | { |
137 | GNUNET_break (0); | 132 | GNUNET_break (0); |
138 | return NULL; | 133 | return NULL; |
139 | } | 134 | } |
140 | if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) | 135 | if (GNUNET_TIME_relative_is_forever (stamp)) |
141 | { | 136 | { |
142 | if (0 != | 137 | if (0 != |
143 | json_object_set_new (j, | 138 | json_object_set_new (j, |
139 | "d_us", | ||
140 | json_string ("forever"))) | ||
141 | { | ||
142 | GNUNET_break (0); | ||
143 | json_decref (j); | ||
144 | return NULL; | ||
145 | } | ||
146 | if (0 != | ||
147 | json_object_set_new (j, | ||
144 | "d_ms", | 148 | "d_ms", |
145 | json_string ("forever"))) | 149 | json_string ("forever"))) |
146 | { | 150 | { |
@@ -150,11 +154,27 @@ GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp) | |||
150 | } | 154 | } |
151 | return j; | 155 | return j; |
152 | } | 156 | } |
157 | if (stamp.rel_value_us >= (1LLU << 53)) | ||
158 | { | ||
159 | /* value is larger than allowed */ | ||
160 | GNUNET_break (0); | ||
161 | return NULL; | ||
162 | } | ||
153 | if (0 != | 163 | if (0 != |
154 | json_object_set_new (j, | 164 | json_object_set_new ( |
155 | "d_ms", | 165 | j, |
156 | json_integer ((json_int_t) (stamp.rel_value_us | 166 | "d_us", |
157 | / 1000LL)))) | 167 | json_integer ((json_int_t) stamp.rel_value_us))) |
168 | { | ||
169 | GNUNET_break (0); | ||
170 | json_decref (j); | ||
171 | return NULL; | ||
172 | } | ||
173 | if (0 != | ||
174 | json_object_set_new ( | ||
175 | j, | ||
176 | "d_ms", | ||
177 | json_integer (((json_int_t) stamp.rel_value_us)/1000LL))) | ||
158 | { | 178 | { |
159 | GNUNET_break (0); | 179 | GNUNET_break (0); |
160 | json_decref (j); | 180 | json_decref (j); |
@@ -164,12 +184,6 @@ GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp) | |||
164 | } | 184 | } |
165 | 185 | ||
166 | 186 | ||
167 | /** | ||
168 | * Convert RSA public key to JSON. | ||
169 | * | ||
170 | * @param pk public key to convert | ||
171 | * @return corresponding JSON encoding | ||
172 | */ | ||
173 | json_t * | 187 | json_t * |
174 | GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk) | 188 | GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk) |
175 | { | 189 | { |
@@ -186,12 +200,6 @@ GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk) | |||
186 | } | 200 | } |
187 | 201 | ||
188 | 202 | ||
189 | /** | ||
190 | * Convert RSA signature to JSON. | ||
191 | * | ||
192 | * @param sig signature to convert | ||
193 | * @return corresponding JSON encoding | ||
194 | */ | ||
195 | json_t * | 203 | json_t * |
196 | GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig) | 204 | GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig) |
197 | { | 205 | { |
diff --git a/src/json/json_helper.c b/src/json/json_helper.c index 3a11f205c..c0513c529 100644 --- a/src/json/json_helper.c +++ b/src/json/json_helper.c | |||
@@ -49,7 +49,7 @@ GNUNET_JSON_spec_end () | |||
49 | * @param[out] spec where to write the data | 49 | * @param[out] spec where to write the data |
50 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | 50 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error |
51 | */ | 51 | */ |
52 | static int | 52 | static enum GNUNET_GenericReturnValue |
53 | parse_fixed_data (void *cls, | 53 | parse_fixed_data (void *cls, |
54 | json_t *root, | 54 | json_t *root, |
55 | struct GNUNET_JSON_Specification *spec) | 55 | struct GNUNET_JSON_Specification *spec) |
@@ -579,19 +579,20 @@ GNUNET_JSON_spec_int64 (const char *name, | |||
579 | /* ************ GNUnet-specific parser specifications ******************* */ | 579 | /* ************ GNUnet-specific parser specifications ******************* */ |
580 | 580 | ||
581 | /** | 581 | /** |
582 | * Parse given JSON object to absolute time. | 582 | * Parse given JSON object to a timestamp. |
583 | * | 583 | * |
584 | * @param cls closure, NULL | 584 | * @param cls closure, NULL |
585 | * @param root the json object representing data | 585 | * @param root the json object representing data |
586 | * @param[out] spec where to write the data | 586 | * @param[out] spec where to write the data |
587 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | 587 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error |
588 | */ | 588 | */ |
589 | static int | 589 | static enum GNUNET_GenericReturnValue |
590 | parse_abs_time (void *cls, | 590 | parse_timestamp (void *cls, |
591 | json_t *root, | 591 | json_t *root, |
592 | struct GNUNET_JSON_Specification *spec) | 592 | struct GNUNET_JSON_Specification *spec) |
593 | { | 593 | { |
594 | struct GNUNET_TIME_Absolute *abs = spec->ptr; | 594 | struct GNUNET_TIME_Timestamp *ts = spec->ptr; |
595 | json_t *json_t_s; | ||
595 | json_t *json_t_ms; | 596 | json_t *json_t_ms; |
596 | unsigned long long int tval; | 597 | unsigned long long int tval; |
597 | 598 | ||
@@ -600,13 +601,49 @@ parse_abs_time (void *cls, | |||
600 | GNUNET_break_op (0); | 601 | GNUNET_break_op (0); |
601 | return GNUNET_SYSERR; | 602 | return GNUNET_SYSERR; |
602 | } | 603 | } |
603 | json_t_ms = json_object_get (root, "t_ms"); | 604 | json_t_s = json_object_get (root, |
605 | "t_s"); | ||
606 | if (json_is_integer (json_t_s)) | ||
607 | { | ||
608 | tval = json_integer_value (json_t_s); | ||
609 | /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ | ||
610 | ts->abs_time.abs_value_us | ||
611 | = tval * GNUNET_TIME_UNIT_SECONDS.rel_value_us; | ||
612 | if (ts->abs_time.abs_value_us | ||
613 | / GNUNET_TIME_UNIT_SECONDS.rel_value_us | ||
614 | != tval) | ||
615 | { | ||
616 | /* Integer overflow */ | ||
617 | GNUNET_break_op (0); | ||
618 | return GNUNET_SYSERR; | ||
619 | } | ||
620 | return GNUNET_OK; | ||
621 | } | ||
622 | if (json_is_string (json_t_s)) | ||
623 | { | ||
624 | const char *val; | ||
625 | |||
626 | val = json_string_value (json_t_s); | ||
627 | if ((0 == strcasecmp (val, | ||
628 | "never"))) | ||
629 | { | ||
630 | ts->abs_time = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
631 | return GNUNET_OK; | ||
632 | } | ||
633 | GNUNET_break_op (0); | ||
634 | return GNUNET_SYSERR; | ||
635 | } | ||
636 | json_t_ms = json_object_get (root, | ||
637 | "t_ms"); | ||
604 | if (json_is_integer (json_t_ms)) | 638 | if (json_is_integer (json_t_ms)) |
605 | { | 639 | { |
606 | tval = json_integer_value (json_t_ms); | 640 | tval = json_integer_value (json_t_ms); |
607 | /* Time is in milliseconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ | 641 | GNUNET_break_op (0 == tval % 1000); |
608 | abs->abs_value_us = tval * GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us; | 642 | tval -= tval % 1000; |
609 | if ((abs->abs_value_us) | 643 | /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ |
644 | ts->abs_time.abs_value_us | ||
645 | = tval * GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us; | ||
646 | if (ts->abs_time.abs_value_us | ||
610 | / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us | 647 | / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us |
611 | != tval) | 648 | != tval) |
612 | { | 649 | { |
@@ -621,9 +658,10 @@ parse_abs_time (void *cls, | |||
621 | const char *val; | 658 | const char *val; |
622 | 659 | ||
623 | val = json_string_value (json_t_ms); | 660 | val = json_string_value (json_t_ms); |
624 | if ((0 == strcasecmp (val, "never"))) | 661 | if ((0 == strcasecmp (val, |
662 | "never"))) | ||
625 | { | 663 | { |
626 | *abs = GNUNET_TIME_UNIT_FOREVER_ABS; | 664 | ts->abs_time = GNUNET_TIME_UNIT_FOREVER_ABS; |
627 | return GNUNET_OK; | 665 | return GNUNET_OK; |
628 | } | 666 | } |
629 | GNUNET_break_op (0); | 667 | GNUNET_break_op (0); |
@@ -635,17 +673,14 @@ parse_abs_time (void *cls, | |||
635 | 673 | ||
636 | 674 | ||
637 | struct GNUNET_JSON_Specification | 675 | struct GNUNET_JSON_Specification |
638 | GNUNET_JSON_spec_absolute_time (const char *name, | 676 | GNUNET_JSON_spec_timestamp (const char *name, |
639 | struct GNUNET_TIME_Absolute *at) | 677 | struct GNUNET_TIME_Timestamp *t) |
640 | { | 678 | { |
641 | struct GNUNET_JSON_Specification ret = { | 679 | struct GNUNET_JSON_Specification ret = { |
642 | .parser = &parse_abs_time, | 680 | .parser = &parse_timestamp, |
643 | .cleaner = NULL, | ||
644 | .cls = NULL, | ||
645 | .field = name, | 681 | .field = name, |
646 | .ptr = at, | 682 | .ptr = t, |
647 | .ptr_size = sizeof(struct GNUNET_TIME_Absolute), | 683 | .ptr_size = sizeof(struct GNUNET_TIME_Timestamp) |
648 | .size_ptr = NULL | ||
649 | }; | 684 | }; |
650 | 685 | ||
651 | return ret; | 686 | return ret; |
@@ -660,40 +695,37 @@ GNUNET_JSON_spec_absolute_time (const char *name, | |||
660 | * @param[out] spec where to write the data | 695 | * @param[out] spec where to write the data |
661 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | 696 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error |
662 | */ | 697 | */ |
663 | static int | 698 | static enum GNUNET_GenericReturnValue |
664 | parse_abs_time_nbo (void *cls, | 699 | parse_timestamp_nbo (void *cls, |
665 | json_t *root, | 700 | json_t *root, |
666 | struct GNUNET_JSON_Specification *spec) | 701 | struct GNUNET_JSON_Specification *spec) |
667 | { | 702 | { |
668 | struct GNUNET_TIME_AbsoluteNBO *abs = spec->ptr; | 703 | struct GNUNET_TIME_TimestampNBO *ts = spec->ptr; |
669 | struct GNUNET_TIME_Absolute a; | 704 | struct GNUNET_TIME_Timestamp a; |
670 | struct GNUNET_JSON_Specification ispec; | 705 | struct GNUNET_JSON_Specification ispec; |
671 | 706 | ||
672 | ispec = *spec; | 707 | ispec = *spec; |
673 | ispec.parser = &parse_abs_time; | 708 | ispec.parser = &parse_timestamp; |
674 | ispec.ptr = &a; | 709 | ispec.ptr = &a; |
675 | if (GNUNET_OK != | 710 | if (GNUNET_OK != |
676 | parse_abs_time (NULL, | 711 | parse_timestamp (NULL, |
677 | root, | 712 | root, |
678 | &ispec)) | 713 | &ispec)) |
679 | return GNUNET_SYSERR; | 714 | return GNUNET_SYSERR; |
680 | *abs = GNUNET_TIME_absolute_hton (a); | 715 | *ts = GNUNET_TIME_timestamp_hton (a); |
681 | return GNUNET_OK; | 716 | return GNUNET_OK; |
682 | } | 717 | } |
683 | 718 | ||
684 | 719 | ||
685 | struct GNUNET_JSON_Specification | 720 | struct GNUNET_JSON_Specification |
686 | GNUNET_JSON_spec_absolute_time_nbo (const char *name, | 721 | GNUNET_JSON_spec_timestamp_nbo (const char *name, |
687 | struct GNUNET_TIME_AbsoluteNBO *at) | 722 | struct GNUNET_TIME_TimestampNBO *at) |
688 | { | 723 | { |
689 | struct GNUNET_JSON_Specification ret = { | 724 | struct GNUNET_JSON_Specification ret = { |
690 | .parser = &parse_abs_time_nbo, | 725 | .parser = &parse_timestamp_nbo, |
691 | .cleaner = NULL, | ||
692 | .cls = NULL, | ||
693 | .field = name, | 726 | .field = name, |
694 | .ptr = at, | 727 | .ptr = at, |
695 | .ptr_size = sizeof(struct GNUNET_TIME_AbsoluteNBO), | 728 | .ptr_size = sizeof(struct GNUNET_TIME_TimestampNBO) |
696 | .size_ptr = NULL | ||
697 | }; | 729 | }; |
698 | 730 | ||
699 | return ret; | 731 | return ret; |
@@ -708,12 +740,13 @@ GNUNET_JSON_spec_absolute_time_nbo (const char *name, | |||
708 | * @param[out] spec where to write the data | 740 | * @param[out] spec where to write the data |
709 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | 741 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error |
710 | */ | 742 | */ |
711 | static int | 743 | static enum GNUNET_GenericReturnValue |
712 | parse_rel_time (void *cls, | 744 | parse_rel_time (void *cls, |
713 | json_t *root, | 745 | json_t *root, |
714 | struct GNUNET_JSON_Specification *spec) | 746 | struct GNUNET_JSON_Specification *spec) |
715 | { | 747 | { |
716 | struct GNUNET_TIME_Relative *rel = spec->ptr; | 748 | struct GNUNET_TIME_Relative *rel = spec->ptr; |
749 | json_t *json_d_us; | ||
717 | json_t *json_d_ms; | 750 | json_t *json_d_ms; |
718 | unsigned long long int tval; | 751 | unsigned long long int tval; |
719 | 752 | ||
@@ -722,25 +755,52 @@ parse_rel_time (void *cls, | |||
722 | GNUNET_break_op (0); | 755 | GNUNET_break_op (0); |
723 | return GNUNET_SYSERR; | 756 | return GNUNET_SYSERR; |
724 | } | 757 | } |
725 | json_d_ms = json_object_get (root, "d_ms"); | 758 | json_d_us = json_object_get (root, |
726 | if (json_is_integer (json_d_ms)) | 759 | "d_us"); |
760 | if (json_is_integer (json_d_us)) | ||
727 | { | 761 | { |
728 | tval = json_integer_value (json_d_ms); | 762 | tval = json_integer_value (json_d_us); |
729 | /* Time is in milliseconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ | 763 | if (tval >= (1LLU << 53)) |
730 | rel->rel_value_us = tval * 1000LL; | ||
731 | if ((rel->rel_value_us) / 1000LL != tval) | ||
732 | { | 764 | { |
733 | /* Integer overflow */ | 765 | /* value is larger than allowed */ |
734 | GNUNET_break_op (0); | 766 | GNUNET_break_op (0); |
735 | return GNUNET_SYSERR; | 767 | return GNUNET_SYSERR; |
736 | } | 768 | } |
769 | rel->rel_value_us = tval; | ||
770 | return GNUNET_OK; | ||
771 | } | ||
772 | if (json_is_string (json_d_us)) | ||
773 | { | ||
774 | const char *val; | ||
775 | |||
776 | val = json_string_value (json_d_us); | ||
777 | if ((0 == strcasecmp (val, | ||
778 | "forever"))) | ||
779 | { | ||
780 | *rel = GNUNET_TIME_UNIT_FOREVER_REL; | ||
781 | return GNUNET_OK; | ||
782 | } | ||
783 | GNUNET_break_op (0); | ||
784 | return GNUNET_SYSERR; | ||
785 | } | ||
786 | |||
787 | json_d_ms = json_object_get (root, | ||
788 | "d_ms"); | ||
789 | if (json_is_integer (json_d_ms)) | ||
790 | { | ||
791 | tval = json_integer_value (json_d_ms); | ||
792 | *rel = GNUNET_TIME_relative_multiply ( | ||
793 | GNUNET_TIME_UNIT_MILLISECONDS, | ||
794 | tval); | ||
737 | return GNUNET_OK; | 795 | return GNUNET_OK; |
738 | } | 796 | } |
739 | if (json_is_string (json_d_ms)) | 797 | if (json_is_string (json_d_ms)) |
740 | { | 798 | { |
741 | const char *val; | 799 | const char *val; |
800 | |||
742 | val = json_string_value (json_d_ms); | 801 | val = json_string_value (json_d_ms); |
743 | if ((0 == strcasecmp (val, "forever"))) | 802 | if ((0 == strcasecmp (val, |
803 | "forever"))) | ||
744 | { | 804 | { |
745 | *rel = GNUNET_TIME_UNIT_FOREVER_REL; | 805 | *rel = GNUNET_TIME_UNIT_FOREVER_REL; |
746 | return GNUNET_OK; | 806 | return GNUNET_OK; |
@@ -759,12 +819,9 @@ GNUNET_JSON_spec_relative_time (const char *name, | |||
759 | { | 819 | { |
760 | struct GNUNET_JSON_Specification ret = { | 820 | struct GNUNET_JSON_Specification ret = { |
761 | .parser = &parse_rel_time, | 821 | .parser = &parse_rel_time, |
762 | .cleaner = NULL, | ||
763 | .cls = NULL, | ||
764 | .field = name, | 822 | .field = name, |
765 | .ptr = rt, | 823 | .ptr = rt, |
766 | .ptr_size = sizeof(struct GNUNET_TIME_Relative), | 824 | .ptr_size = sizeof(struct GNUNET_TIME_Relative) |
767 | .size_ptr = NULL | ||
768 | }; | 825 | }; |
769 | 826 | ||
770 | return ret; | 827 | return ret; |
@@ -779,7 +836,7 @@ GNUNET_JSON_spec_relative_time (const char *name, | |||
779 | * @param[out] spec where to write the data | 836 | * @param[out] spec where to write the data |
780 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | 837 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error |
781 | */ | 838 | */ |
782 | static int | 839 | static enum GNUNET_GenericReturnValue |
783 | parse_rsa_public_key (void *cls, | 840 | parse_rsa_public_key (void *cls, |
784 | json_t *root, | 841 | json_t *root, |
785 | struct GNUNET_JSON_Specification *spec) | 842 | struct GNUNET_JSON_Specification *spec) |
@@ -864,7 +921,7 @@ GNUNET_JSON_spec_rsa_public_key (const char *name, | |||
864 | * @param[out] spec where to write the data | 921 | * @param[out] spec where to write the data |
865 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | 922 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error |
866 | */ | 923 | */ |
867 | static int | 924 | static enum GNUNET_GenericReturnValue |
868 | parse_rsa_signature (void *cls, | 925 | parse_rsa_signature (void *cls, |
869 | json_t *root, | 926 | json_t *root, |
870 | struct GNUNET_JSON_Specification *spec) | 927 | struct GNUNET_JSON_Specification *spec) |
@@ -952,7 +1009,7 @@ GNUNET_JSON_spec_rsa_signature (const char *name, | |||
952 | * @param[out] spec where to write the data | 1009 | * @param[out] spec where to write the data |
953 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | 1010 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error |
954 | */ | 1011 | */ |
955 | static int | 1012 | static enum GNUNET_GenericReturnValue |
956 | parse_boolean (void *cls, | 1013 | parse_boolean (void *cls, |
957 | json_t *root, | 1014 | json_t *root, |
958 | struct GNUNET_JSON_Specification *spec) | 1015 | struct GNUNET_JSON_Specification *spec) |
diff --git a/src/json/json_pack.c b/src/json/json_pack.c index 92f8b4535..296f56104 100644 --- a/src/json/json_pack.c +++ b/src/json/json_pack.c | |||
@@ -245,16 +245,16 @@ GNUNET_JSON_pack_data_varsize (const char *name, | |||
245 | 245 | ||
246 | 246 | ||
247 | struct GNUNET_JSON_PackSpec | 247 | struct GNUNET_JSON_PackSpec |
248 | GNUNET_JSON_pack_time_abs (const char *name, | 248 | GNUNET_JSON_pack_timestamp (const char *name, |
249 | struct GNUNET_TIME_Absolute at) | 249 | struct GNUNET_TIME_Timestamp t) |
250 | { | 250 | { |
251 | struct GNUNET_JSON_PackSpec ps = { | 251 | struct GNUNET_JSON_PackSpec ps = { |
252 | .field_name = name | 252 | .field_name = name |
253 | }; | 253 | }; |
254 | 254 | ||
255 | if (0 != at.abs_value_us) | 255 | if (! GNUNET_TIME_absolute_is_zero (t.abs_time)) |
256 | { | 256 | { |
257 | ps.object = GNUNET_JSON_from_time_abs (at); | 257 | ps.object = GNUNET_JSON_from_timestamp (t); |
258 | GNUNET_assert (NULL != ps.object); | 258 | GNUNET_assert (NULL != ps.object); |
259 | } | 259 | } |
260 | else | 260 | else |
@@ -266,11 +266,11 @@ GNUNET_JSON_pack_time_abs (const char *name, | |||
266 | 266 | ||
267 | 267 | ||
268 | struct GNUNET_JSON_PackSpec | 268 | struct GNUNET_JSON_PackSpec |
269 | GNUNET_JSON_pack_time_abs_nbo (const char *name, | 269 | GNUNET_JSON_pack_timestamp_nbo (const char *name, |
270 | struct GNUNET_TIME_AbsoluteNBO at) | 270 | struct GNUNET_TIME_TimestampNBO at) |
271 | { | 271 | { |
272 | return GNUNET_JSON_pack_time_abs (name, | 272 | return GNUNET_JSON_pack_timestamp (name, |
273 | GNUNET_TIME_absolute_ntoh (at)); | 273 | GNUNET_TIME_timestamp_ntoh (at)); |
274 | } | 274 | } |
275 | 275 | ||
276 | 276 | ||
diff --git a/src/json/test_json.c b/src/json/test_json.c index 4485a37a4..d6c372cf3 100644 --- a/src/json/test_json.c +++ b/src/json/test_json.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet | 2 | This file is part of GNUnet |
3 | (C) 2015, 2016 GNUnet e.V. | 3 | (C) 2015, 2016, 2021 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -34,33 +34,44 @@ | |||
34 | * @return 0 on success | 34 | * @return 0 on success |
35 | */ | 35 | */ |
36 | static int | 36 | static int |
37 | test_abs_time () | 37 | test_timestamp (void) |
38 | { | 38 | { |
39 | json_t *j; | 39 | json_t *j; |
40 | struct GNUNET_TIME_Absolute a1; | 40 | struct GNUNET_TIME_Absolute a1; |
41 | struct GNUNET_TIME_Absolute a2; | 41 | struct GNUNET_TIME_Timestamp t1; |
42 | struct GNUNET_JSON_Specification s1[] = { GNUNET_JSON_spec_absolute_time ( | 42 | struct GNUNET_TIME_Timestamp t2; |
43 | NULL, | 43 | struct GNUNET_JSON_Specification s1[] = { |
44 | &a2), | 44 | GNUNET_JSON_spec_timestamp (NULL, |
45 | GNUNET_JSON_spec_end () }; | 45 | &t2), |
46 | struct GNUNET_JSON_Specification s2[] = { GNUNET_JSON_spec_absolute_time ( | 46 | GNUNET_JSON_spec_end () |
47 | NULL, | 47 | }; |
48 | &a2), | 48 | struct GNUNET_JSON_Specification s2[] = { |
49 | GNUNET_JSON_spec_end () }; | 49 | GNUNET_JSON_spec_timestamp (NULL, |
50 | &t2), | ||
51 | GNUNET_JSON_spec_end () | ||
52 | }; | ||
50 | 53 | ||
51 | a1 = GNUNET_TIME_absolute_get (); | 54 | a1 = GNUNET_TIME_absolute_get (); |
52 | GNUNET_TIME_round_abs (&a1); | 55 | t1 = GNUNET_TIME_absolute_to_timestamp (a1); |
53 | j = GNUNET_JSON_from_time_abs (a1); | 56 | j = GNUNET_JSON_from_timestamp (t1); |
54 | GNUNET_assert (NULL != j); | 57 | GNUNET_assert (NULL != j); |
55 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL)); | 58 | GNUNET_assert (GNUNET_OK == |
56 | GNUNET_assert (a1.abs_value_us == a2.abs_value_us); | 59 | GNUNET_JSON_parse (j, |
60 | s1, | ||
61 | NULL, | ||
62 | NULL)); | ||
63 | GNUNET_assert (GNUNET_TIME_timestamp_cmp (t1, ==, t2)); | ||
57 | json_decref (j); | 64 | json_decref (j); |
58 | 65 | ||
59 | a1 = GNUNET_TIME_UNIT_FOREVER_ABS; | 66 | a1 = GNUNET_TIME_UNIT_FOREVER_ABS; |
60 | j = GNUNET_JSON_from_time_abs (a1); | 67 | j = GNUNET_JSON_from_timestamp (t1); |
61 | GNUNET_assert (NULL != j); | 68 | GNUNET_assert (NULL != j); |
62 | GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL)); | 69 | GNUNET_assert (GNUNET_OK == |
63 | GNUNET_assert (a1.abs_value_us == a2.abs_value_us); | 70 | GNUNET_JSON_parse (j, |
71 | s2, | ||
72 | NULL, | ||
73 | NULL)); | ||
74 | GNUNET_assert (GNUNET_TIME_timestamp_cmp (t1, ==, t2)); | ||
64 | json_decref (j); | 75 | json_decref (j); |
65 | return 0; | 76 | return 0; |
66 | } | 77 | } |
@@ -72,19 +83,21 @@ test_abs_time () | |||
72 | * @return 0 on success | 83 | * @return 0 on success |
73 | */ | 84 | */ |
74 | static int | 85 | static int |
75 | test_rel_time () | 86 | test_rel_time (void) |
76 | { | 87 | { |
77 | json_t *j; | 88 | json_t *j; |
78 | struct GNUNET_TIME_Relative r1; | 89 | struct GNUNET_TIME_Relative r1; |
79 | struct GNUNET_TIME_Relative r2; | 90 | struct GNUNET_TIME_Relative r2; |
80 | struct GNUNET_JSON_Specification s1[] = { GNUNET_JSON_spec_relative_time ( | 91 | struct GNUNET_JSON_Specification s1[] = { |
81 | NULL, | 92 | GNUNET_JSON_spec_relative_time (NULL, |
82 | &r2), | 93 | &r2), |
83 | GNUNET_JSON_spec_end () }; | 94 | GNUNET_JSON_spec_end () |
84 | struct GNUNET_JSON_Specification s2[] = { GNUNET_JSON_spec_relative_time ( | 95 | }; |
85 | NULL, | 96 | struct GNUNET_JSON_Specification s2[] = { |
86 | &r2), | 97 | GNUNET_JSON_spec_relative_time (NULL, |
87 | GNUNET_JSON_spec_end () }; | 98 | &r2), |
99 | GNUNET_JSON_spec_end () | ||
100 | }; | ||
88 | 101 | ||
89 | r1 = GNUNET_TIME_UNIT_SECONDS; | 102 | r1 = GNUNET_TIME_UNIT_SECONDS; |
90 | j = GNUNET_JSON_from_time_rel (r1); | 103 | j = GNUNET_JSON_from_time_rel (r1); |
@@ -211,7 +224,7 @@ int | |||
211 | main (int argc, const char *const argv[]) | 224 | main (int argc, const char *const argv[]) |
212 | { | 225 | { |
213 | GNUNET_log_setup ("test-json", "WARNING", NULL); | 226 | GNUNET_log_setup ("test-json", "WARNING", NULL); |
214 | if (0 != test_abs_time ()) | 227 | if (0 != test_timestamp ()) |
215 | return 1; | 228 | return 1; |
216 | if (0 != test_rel_time ()) | 229 | if (0 != test_rel_time ()) |
217 | return 1; | 230 | return 1; |