2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
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
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file json/json_generator.c
22 * @brief helper functions for generating JSON from GNUnet data structures
23 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
26 #include "gnunet_util_lib.h"
27 #include "gnunet_json_lib.h"
31 * Convert binary data to a JSON string
32 * with the base32crockford encoding.
34 * @param data binary data
35 * @param size size of @a data in bytes
36 * @return json string that encodes @a data
39 GNUNET_JSON_from_data (const void *data,
45 buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
46 json = json_string (buf);
53 * Convert absolute timestamp to a json string.
55 * @param stamp the time stamp
56 * @return a json string with the timestamp in @a stamp
59 GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
65 GNUNET_assert (GNUNET_OK ==
66 GNUNET_TIME_round_abs (&stamp));
67 if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
68 return json_string ("/never/");
69 ret = GNUNET_asprintf (&mystr,
71 (unsigned long long) (stamp.abs_value_us / (1000LL
73 GNUNET_assert (ret > 0);
74 j = json_string (mystr);
81 * Convert absolute timestamp to a json string.
83 * @param stamp the time stamp
84 * @return a json string with the timestamp in @a stamp
87 GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp)
89 return GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (stamp));
94 * Convert relative timestamp to a json string.
96 * @param stamp the time stamp
97 * @return a json string with the timestamp in @a stamp
100 GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
106 GNUNET_assert (GNUNET_OK ==
107 GNUNET_TIME_round_rel (&stamp));
108 if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
109 return json_string ("/forever/");
110 ret = GNUNET_asprintf (&mystr,
112 (unsigned long long) (stamp.rel_value_us / (1000LL
114 GNUNET_assert (ret > 0);
115 j = json_string (mystr);
122 * Convert RSA public key to JSON.
124 * @param pk public key to convert
125 * @return corresponding JSON encoding
128 GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
134 buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
136 ret = GNUNET_JSON_from_data (buf,
144 * Convert RSA signature to JSON.
146 * @param sig signature to convert
147 * @return corresponding JSON encoding
150 GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
156 buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
158 ret = GNUNET_JSON_from_data (buf,
166 * Convert GNS record to JSON.
168 * @param rname name of record
169 * @param rd record data
170 * @return corresponding JSON encoding
173 GNUNET_JSON_from_gnsrecord (const char*rname,
174 const struct GNUNET_GNSRECORD_Data *rd,
175 unsigned int rd_count)
177 struct GNUNET_TIME_Absolute expiration_time;
178 const char *expiration_time_str;
179 const char *record_type_str;
185 data = json_object ();
186 json_object_set_new (data,
188 json_string (rname));
189 records = json_array ();
190 for (int i = 0; i < rd_count; i++)
192 value_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
195 expiration_time = GNUNET_GNSRECORD_record_get_expiration_time (1, &rd[i]);
196 expiration_time_str = GNUNET_STRINGS_absolute_time_to_string (
198 record_type_str = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Packing %s %s %s %d\n",
201 value_str, record_type_str, expiration_time_str, rd[i].flags);
202 record = json_pack ("{s:s,s:s,s:s,s:i}",
211 GNUNET_assert (NULL != record);
212 GNUNET_free (value_str);
213 json_array_append_new (records, record);
215 json_object_set_new (data, "data", records);
220 /* End of json/json_generator.c */