aboutsummaryrefslogtreecommitdiff
path: root/src/json/json_generator.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/json/json_generator.c')
-rw-r--r--src/json/json_generator.c145
1 files changed, 74 insertions, 71 deletions
diff --git a/src/json/json_generator.c b/src/json/json_generator.c
index 61993c86a..72ee05dfc 100644
--- a/src/json/json_generator.c
+++ b/src/json/json_generator.c
@@ -36,15 +36,15 @@
36 * @return json string that encodes @a data 36 * @return json string that encodes @a data
37 */ 37 */
38json_t * 38json_t *
39GNUNET_JSON_from_data(const void *data, 39GNUNET_JSON_from_data (const void *data,
40 size_t size) 40 size_t size)
41{ 41{
42 char *buf; 42 char *buf;
43 json_t *json; 43 json_t *json;
44 44
45 buf = GNUNET_STRINGS_data_to_string_alloc(data, size); 45 buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
46 json = json_string(buf); 46 json = json_string (buf);
47 GNUNET_free(buf); 47 GNUNET_free (buf);
48 return json; 48 return json;
49} 49}
50 50
@@ -56,22 +56,23 @@ GNUNET_JSON_from_data(const void *data,
56 * @return a json string with the timestamp in @a stamp 56 * @return a json string with the timestamp in @a stamp
57 */ 57 */
58json_t * 58json_t *
59GNUNET_JSON_from_time_abs(struct GNUNET_TIME_Absolute stamp) 59GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
60{ 60{
61 json_t *j; 61 json_t *j;
62 char *mystr; 62 char *mystr;
63 int ret; 63 int ret;
64 64
65 GNUNET_assert(GNUNET_OK == 65 GNUNET_assert (GNUNET_OK ==
66 GNUNET_TIME_round_abs(&stamp)); 66 GNUNET_TIME_round_abs (&stamp));
67 if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) 67 if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
68 return json_string("/never/"); 68 return json_string ("/never/");
69 ret = GNUNET_asprintf(&mystr, 69 ret = GNUNET_asprintf (&mystr,
70 "/Date(%llu)/", 70 "/Date(%llu)/",
71 (unsigned long long)(stamp.abs_value_us / (1000LL * 1000LL))); 71 (unsigned long long) (stamp.abs_value_us / (1000LL
72 GNUNET_assert(ret > 0); 72 * 1000LL)));
73 j = json_string(mystr); 73 GNUNET_assert (ret > 0);
74 GNUNET_free(mystr); 74 j = json_string (mystr);
75 GNUNET_free (mystr);
75 return j; 76 return j;
76} 77}
77 78
@@ -83,9 +84,9 @@ GNUNET_JSON_from_time_abs(struct GNUNET_TIME_Absolute stamp)
83 * @return a json string with the timestamp in @a stamp 84 * @return a json string with the timestamp in @a stamp
84 */ 85 */
85json_t * 86json_t *
86GNUNET_JSON_from_time_abs_nbo(struct GNUNET_TIME_AbsoluteNBO stamp) 87GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp)
87{ 88{
88 return GNUNET_JSON_from_time_abs(GNUNET_TIME_absolute_ntoh(stamp)); 89 return GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (stamp));
89} 90}
90 91
91 92
@@ -96,22 +97,23 @@ GNUNET_JSON_from_time_abs_nbo(struct GNUNET_TIME_AbsoluteNBO stamp)
96 * @return a json string with the timestamp in @a stamp 97 * @return a json string with the timestamp in @a stamp
97 */ 98 */
98json_t * 99json_t *
99GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp) 100GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
100{ 101{
101 json_t *j; 102 json_t *j;
102 char *mystr; 103 char *mystr;
103 int ret; 104 int ret;
104 105
105 GNUNET_assert(GNUNET_OK == 106 GNUNET_assert (GNUNET_OK ==
106 GNUNET_TIME_round_rel(&stamp)); 107 GNUNET_TIME_round_rel (&stamp));
107 if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 108 if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
108 return json_string("/forever/"); 109 return json_string ("/forever/");
109 ret = GNUNET_asprintf(&mystr, 110 ret = GNUNET_asprintf (&mystr,
110 "/Delay(%llu)/", 111 "/Delay(%llu)/",
111 (unsigned long long)(stamp.rel_value_us / (1000LL * 1000LL))); 112 (unsigned long long) (stamp.rel_value_us / (1000LL
112 GNUNET_assert(ret > 0); 113 * 1000LL)));
113 j = json_string(mystr); 114 GNUNET_assert (ret > 0);
114 GNUNET_free(mystr); 115 j = json_string (mystr);
116 GNUNET_free (mystr);
115 return j; 117 return j;
116} 118}
117 119
@@ -123,17 +125,17 @@ GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
123 * @return corresponding JSON encoding 125 * @return corresponding JSON encoding
124 */ 126 */
125json_t * 127json_t *
126GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk) 128GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
127{ 129{
128 char *buf; 130 char *buf;
129 size_t buf_len; 131 size_t buf_len;
130 json_t *ret; 132 json_t *ret;
131 133
132 buf_len = GNUNET_CRYPTO_rsa_public_key_encode(pk, 134 buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
133 &buf); 135 &buf);
134 ret = GNUNET_JSON_from_data(buf, 136 ret = GNUNET_JSON_from_data (buf,
135 buf_len); 137 buf_len);
136 GNUNET_free(buf); 138 GNUNET_free (buf);
137 return ret; 139 return ret;
138} 140}
139 141
@@ -145,17 +147,17 @@ GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
145 * @return corresponding JSON encoding 147 * @return corresponding JSON encoding
146 */ 148 */
147json_t * 149json_t *
148GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig) 150GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
149{ 151{
150 char *buf; 152 char *buf;
151 size_t buf_len; 153 size_t buf_len;
152 json_t *ret; 154 json_t *ret;
153 155
154 buf_len = GNUNET_CRYPTO_rsa_signature_encode(sig, 156 buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
155 &buf); 157 &buf);
156 ret = GNUNET_JSON_from_data(buf, 158 ret = GNUNET_JSON_from_data (buf,
157 buf_len); 159 buf_len);
158 GNUNET_free(buf); 160 GNUNET_free (buf);
159 return ret; 161 return ret;
160} 162}
161 163
@@ -167,9 +169,9 @@ GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
167 * @return corresponding JSON encoding 169 * @return corresponding JSON encoding
168 */ 170 */
169json_t * 171json_t *
170GNUNET_JSON_from_gnsrecord(const char* rname, 172GNUNET_JSON_from_gnsrecord (const char*rname,
171 const struct GNUNET_GNSRECORD_Data *rd, 173 const struct GNUNET_GNSRECORD_Data *rd,
172 unsigned int rd_count) 174 unsigned int rd_count)
173{ 175{
174 struct GNUNET_TIME_Absolute expiration_time; 176 struct GNUNET_TIME_Absolute expiration_time;
175 const char *expiration_time_str; 177 const char *expiration_time_str;
@@ -179,36 +181,37 @@ GNUNET_JSON_from_gnsrecord(const char* rname,
179 json_t *record; 181 json_t *record;
180 json_t *records; 182 json_t *records;
181 183
182 data = json_object(); 184 data = json_object ();
183 json_object_set_new(data, 185 json_object_set_new (data,
184 "record_name", 186 "record_name",
185 json_string(rname)); 187 json_string (rname));
186 records = json_array(); 188 records = json_array ();
187 for (int i = 0; i < rd_count; i++) 189 for (int i = 0; i < rd_count; i++)
188 { 190 {
189 value_str = GNUNET_GNSRECORD_value_to_string(rd[i].record_type, 191 value_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
190 rd[i].data, 192 rd[i].data,
191 rd[i].data_size); 193 rd[i].data_size);
192 expiration_time = GNUNET_GNSRECORD_record_get_expiration_time(1, &rd[i]); 194 expiration_time = GNUNET_GNSRECORD_record_get_expiration_time (1, &rd[i]);
193 expiration_time_str = GNUNET_STRINGS_absolute_time_to_string(expiration_time); 195 expiration_time_str = GNUNET_STRINGS_absolute_time_to_string (
194 record_type_str = GNUNET_GNSRECORD_number_to_typename(rd[i].record_type); 196 expiration_time);
195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 197 record_type_str = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
196 "Packing %s %s %s %d\n", 198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 value_str, record_type_str, expiration_time_str, rd[i].flags); 199 "Packing %s %s %s %d\n",
198 record = json_pack("{s:s,s:s,s:s,s:i}", 200 value_str, record_type_str, expiration_time_str, rd[i].flags);
199 "value", 201 record = json_pack ("{s:s,s:s,s:s,s:i}",
200 value_str, 202 "value",
201 "record_type", 203 value_str,
202 record_type_str, 204 "record_type",
203 "expiration_time", 205 record_type_str,
204 expiration_time_str, 206 "expiration_time",
205 "flag", 207 expiration_time_str,
206 rd[i].flags); 208 "flag",
207 GNUNET_assert(NULL != record); 209 rd[i].flags);
208 GNUNET_free(value_str); 210 GNUNET_assert (NULL != record);
209 json_array_append_new(records, record); 211 GNUNET_free (value_str);
210 } 212 json_array_append_new (records, record);
211 json_object_set_new(data, "data", records); 213 }
214 json_object_set_new (data, "data", records);
212 return data; 215 return data;
213} 216}
214 217